⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wuziqiview.cpp

📁 用c++完成的五子棋
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	
	tempcountkey=white.nCountkey;
	white.nCountkey=black.nCountkey;
	black.nCountkey=tempcountkey;
	
	for(count1=0;count1<=31;count1++)
	{
		temp=black.nKeydown[count1];
		black.nKeydown[count1]=white.nKeydown[count1];
		white.nKeydown[count1]=temp;
	}
	who=-who;


/*	CMainFrame* pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd;
	CStatusBar* pStatus=&pFrame->m_wndStatusBar;
	if(pStatus)
	{
		str.Format("white downed %d",white.nCountkey);
		pStatus->SetPaneText(1,str);
		str.Format("black downed %d",black.nCountkey);
		pStatus->SetPaneText(0,str);


	}*/

		Invalidate();

	if(connect==1)
	{//如果有连接,发送到对方电脑
		if(whoiam!=1)
		{
		whoiam=-whoiam;
			youcansendmessage=1;
			SEND[0]='9';
			AfxBeginThread(ClientSocketThreadProc, GetSafeHwnd());
		}
	else
	{
		whoiam=-whoiam;
		youcansendmessage=0;
	}}
	
}

void CWuziqiView::OnUpdateRuleChange(CCmdUI* pCmdUI) 
{
	if((black.nCountkey+white.nCountkey==5&&connect==0)||(black.nCountkey+white.nCountkey==5&&connect==1&&whoiam!=1))
		pCmdUI->Enable(1);

	else
		pCmdUI->Enable(0);	
}

void CWuziqiView::OnRButtonDown(UINT nFlags, CPoint point) 
{//左键下棋,试走,左键双击可以清除
	float temp;int com;
	int x,y;
	CRect rectClient;
	GetClientRect(rectClient);
	temp=point.x*16;
	temp=temp/rectClient.right;
	com=temp;
	if(temp-com>0.5)x=temp+1;
	else x=temp;

	temp=point.y*16;
	temp=temp/rectClient.bottom;
	com=temp;
	if(temp-com>0.5)y=temp+1;
	else y=temp;

	if(all.allqipan[x][y]==0)
	{
		if(all.qipantemp[x][y]!=0)
		{		
			switch(all.qipantemp[x][y])
			{
				case 1:
					all.qipantemp[x][y]=-1;
					break;
				case -1:
					all.qipantemp[x][y]=1;
					break;
			}
		}
		else
		{
			switch(whotemp)
			{
			case 1:
				all.qipantemp[x][y]=1;
				whotemp=-1;
				break;
			case -1:
				all.qipantemp[x][y]=-1;
				whotemp=1;
				break;
			}
		}

	}
	CView::OnRButtonDown(nFlags, point);
	Invalidate();

}

void CWuziqiView::OnRButtonDblClk(UINT nFlags, CPoint point) 
{//左键双击清楚
	float temp;int com;
	int x,y;
	CRect rectClient;
	GetClientRect(rectClient);
	temp=point.x*16;
	temp=temp/rectClient.right;
	com=temp;
	if(temp-com>0.5)x=temp+1;
	else x=temp;

	temp=point.y*16;
	temp=temp/rectClient.bottom;
	com=temp;
	if(temp-com>0.5)y=temp+1;
	else y=temp;

	if(all.qipantemp[x][y]!=0)
	{
		all.qipantemp[x][y]=0;
	}
	Invalidate();

	
	CView::OnRButtonDblClk(nFlags, point);
}

void CWuziqiView::OnRuleRestart() 
{
//从新开始
	all.cleartemp();
	all.clear();
	black.clearall();
	white.clearall();
	who=1;//connect=0;
	Invalidate();

}

void CWuziqiView::OnRule3344() 
{
//取消双三双四限制,没有写,不建议不用国际规则
	//HINSTANCE  n=ShellExecute(NULL, "open", "Readme.htm", NULL,NULL, SW_SHOWNORMAL);	//::WinExec("C:\Program Files\Internet Explorer\IEXPLORE.EXE c:\1.html",SW_SHOW);
	
	// TODO: Add your command handler code here
}

void CWuziqiView::OnRuleClean() 
{//清除当前所有试走
	//::PostMessage(GetSafeHwnd(),WM_CONNECT,0,0);	
	all.cleartemp();
	Invalidate();
	
}
LRESULT CWuziqiView::OnConnect(WPARAM wParam,LPARAM lParam)
{//的到对方电脑数据调用的程序
	ShowWindow(SW_SHOWNORMAL);//把窗口放到顶端
	BringWindowToTop();
	if(ACCEPT[0]=='1')//请求连接,国际规则
	{
		if(connect==1)
		{//如果已经有了连接,告诉对方
			SEND[0]='a';
			AfxBeginThread(ClientSocketThreadProc, GetSafeHwnd());
			return 0;
		}

		askconnect con;
		con.m_pro="connect with 3344";
		con.m_ip=CONADDR;
		int n=con.DoModal();
		if(n==IDOK)
		{
			all.clear();black.nCountkey=0;white.nCountkey=0;				Invalidate();

			SERVER=CONADDR;
			connect=whoiam=1;
			SEND[0]='5';
			AfxBeginThread(ClientSocketThreadProc, GetSafeHwnd());
			youcansendmessage=1;
		}
		else
		{
			SEND[0]='n';//发送拒绝消息
			AfxBeginThread(ClientSocketThreadProc, GetSafeHwnd());
		}		
	}
	if(ACCEPT[0]=='2')
	{//请求连接,普通规则,不建议使用
		if(connect==1)
		{
			SEND[0]='a';
			AfxBeginThread(ClientSocketThreadProc, GetSafeHwnd());
			return 0;
		}

			all.clear();black.nCountkey=0;white.nCountkey=0;				Invalidate();

		SERVER=CONADDR;
		askconnect con;
		con.m_pro="connect without 3344";
		con.m_ip=CONADDR;
		int n=con.DoModal();
		if(n==IDOK)
		{
			connect=whoiam=1;
			SEND[0]='5';
			AfxBeginThread(ClientSocketThreadProc, GetSafeHwnd());
			youcansendmessage=1;
		}
		else
		{
			SEND[0]='n';
			AfxBeginThread(ClientSocketThreadProc, GetSafeHwnd());
		}
	}
	if(ACCEPT[0]=='a')
	{//的到对方已经开始的消息
		AfxMessageBox("The Other player has already begin with others!Failed");
	}
	if(ACCEPT[0]=='n')
	{//得到对方拒绝消息
		AfxMessageBox("The Other player refused!");
	}
	if(ACCEPT[0]=='5')
	{//的到对方同意消息
		all.clear();black.nCountkey=0;white.nCountkey=0;				Invalidate();

		connect=1;
		youcansendmessage=0;
		whoiam=-1;
	}
/*	if(ACCEPT[0]=='6')
	{//该我走
		whoiam=-whoiam;
	}*/
	if(ACCEPT[0]=='!')
	{//对方取消了连接
		connect=0;
		AfxMessageBox("The other user disconnected!you can choose restart game from the menu to restart!");

	}
	if(ACCEPT[0]=='9')
	{//对方请求了交换
		OnRuleChange();
	}
	if(ACCEPT[0]=='3')//him black down,the same as OnLeftButtonDown
	{
		int x,y,who;
		x=ACCEPT[2];y=ACCEPT[3];who=1;whohasjinshou=1;		
		if(all.allqipan[x][y]==0)
		{		
			MessageBeep(MB_OK);

			black.keydown(x,y,who);
			all.allqipan[x][y]=1;			
			if(all.shuangsi(x,y)==1&&whohasjinshou==1)
			{
				Invalidate();
				resultwin lost;
				lost.m_Who.Format("black lost!double 4!");
				lost.DoModal();
				OnRuleRestart();youcansendmessage=0;whoiam=-1;
				return 0;
			}

			int win=all.winchanglian(x,y);
			if(win==1)
			{
				Invalidate();
				resultwin win;
				win.m_Who.Format("black win!");
				win.DoModal();
				OnRuleRestart();youcansendmessage=1;whoiam=1;
				return 0;	
			}		
			if(win==0&&whohasjinshou==1)
			{
				Invalidate();
				resultwin lost;
				lost.m_Who.Format("black lost!long!");
				lost.DoModal();
				OnRuleRestart();youcansendmessage=0;whoiam=-1;
				return 0;
			}

			if(all.shuangsan(x,y)==1&&whohasjinshou==1) 
			{
				Invalidate();
				resultwin fail;
				fail.m_Who.Format("Black lost!double 3!");
				fail.DoModal();
				OnRuleRestart();youcansendmessage=0;whoiam=-1;
				return 0;		
			}
		}
	
	youcansendmessage=1;
	Invalidate();
	//SEND MESSAGE

	}
	if(ACCEPT[0]=='4')//him white down
	{
		int x,y,who;
		x=ACCEPT[2];y=ACCEPT[3];who=-1;whohasjinshou=-1;		
		if(all.allqipan[x][y]==0)
		{		
					MessageBeep(MB_OK);

			white.keydown(x,y,who);
			all.allqipan[x][y]=-1;			
			if(all.shuangsi(x,y)==1&&whohasjinshou==1)
			{
				Invalidate();
				resultwin lost;
				lost.m_Who.Format("white lost!double 4!");
				lost.DoModal();
				OnRuleRestart();youcansendmessage=0;whoiam=-1;
				return 0;
			}

			int win=all.winchanglian(x,y);
			if(win==1)
			{
				Invalidate();
				resultwin win;
				win.m_Who.Format("white win!");
				win.DoModal();
				OnRuleRestart();youcansendmessage=1;whoiam=1;
				return 0;	
			}		
			if(win==0&&whohasjinshou==1)
			{
				Invalidate();
				resultwin lost;
				lost.m_Who.Format("white lost!long!");
				lost.DoModal();
				OnRuleRestart();youcansendmessage=0;whoiam=-1;
				return 0;
			}

			if(all.shuangsan(x,y)==1&&whohasjinshou==1) 
			{
				Invalidate();
				resultwin fail;
				fail.m_Who.Format("white lost!double 3!");
				fail.DoModal();
				OnRuleRestart();youcansendmessage=0;whoiam=-1;
				return 0;
			}
		}
	youcansendmessage=1;
	Invalidate();
	//SEND MESSAGE
	}
	

	return 0;
}
UINT ServerThreadProc(LPVOID pParam)
{//服务器进程
//	::PostMessage((HWND)pParam,WM_CONNECT,0,0);		
	CBlockingSocket sConnect;		 		 

	try
	{
		if(!g_sListen.Accept(sConnect,saClient))
		{
			return 0;
		}
		CONADDR=saClient.DottedDecimal();

		AfxBeginThread(ServerThreadProc, pParam, THREAD_PRIORITY_NORMAL);
	/*	if(CONADDR==INADDR_ANY)
		{如果连接到自己,但是不成功,所没是个bug
			AfxMessageBox("you cant't connect to yourself!");
			sConnect.Close();
			return 0;
		}*/

		sConnect.Receive(ACCEPT,8,10);
		::SendMessage((HWND)pParam,WM_CONNECT,0,0);		
		sConnect.Close();
		
	}
	catch(CBlockingSocketException* e)
	{
		e->Delete();
		AfxMessageBox("Connect problem!you can choose restart game from the menu to restart!");
		CFrameWnd* Fram=(CFrameWnd*)AfxGetApp()->m_pMainWnd;
		CWuziqiView* v=(CWuziqiView*)Fram->GetActiveView();
		v->connect=0;
		
	}
	return 0;
}

UINT ClientSocketThreadProc(LPVOID pParam)
 {//发送消息的进程
	CBlockingSocket sClient;
	 try
	 {

		 CSockAddr saServer;//(SERVER,84);
		 saServer=CBlockingSocket::GetHostByName(SERVER,84);
		 
		 sClient.Create();
		 //sClient.Bind(saServer);			 
		 sClient.Connect(saServer);
		 sClient.Write(SEND,8,10);
		 sClient.Close();

	 }
	 catch(CBlockingSocketException* e)
	 {
		 e->Delete();		 
		AfxMessageBox("Connect problem!you can choose restart game from the menu to restart!");

			CFrameWnd* Fram=(CFrameWnd*)AfxGetApp()->m_pMainWnd;
		CWuziqiView* v=(CWuziqiView*)Fram->GetActiveView();
		v->connect=0;
	 }
	 return 0;
 }

void CWuziqiView::OnConnect() 
{//请求连接
if(connect!=1)
	{
		connectto to;
		to.m_where="172.16.1.135";
		int n=to.DoModal();
		if(n==IDOK)
		{
			SERVER=to.m_where;	
			SEND[0]='1';	
			AfxBeginThread(ClientSocketThreadProc, GetSafeHwnd());
		}
	}
	else
	{//如果当前有活动连接
		AfxMessageBox("You must disconnect first!");
	}
	
}

void CWuziqiView::OnStartserver() 
{//开始监听,自动开始,没有写可以中断的程序
	if(!startserver)
	{
		try
		{
			CSockAddr saServer(INADDR_ANY,84);
			g_sListen.Create();
			g_sListen.Bind(saServer);
			g_sListen.Listen();
			AfxBeginThread(ServerThreadProc, GetSafeHwnd());
			startserver=1;

		}
		catch(CBlockingSocketException* e)
		{
		AfxMessageBox("Connect problem!you can choose restart game from the menu to restart!");
			g_sListen.Cleanup();
			e->Delete();
			startserver=0;
			CFrameWnd* Fram=(CFrameWnd*)AfxGetApp()->m_pMainWnd;
		CWuziqiView* v=(CWuziqiView*)Fram->GetActiveView();
		v->connect=1;
		}
	}
		
}

void CWuziqiView::OnUpdateStartserver(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(startserver);	
}

void CWuziqiView::OnDisconnect() 
{//取消连接
	SEND[0]='!';
	AfxBeginThread(ClientSocketThreadProc, GetSafeHwnd());
	connect=0;
	AfxMessageBox("disconnected!you can choose restart game from the menu to restart!");

	//OnRuleRestart();
}

void CWuziqiView::OnUpdateDisconnect(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(connect);
}

void CWuziqiView::OnUpdateRuleRestart(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(!connect);	
}



void CWuziqiView::OnFileOpen() 
{//打开一个棋谱文件
	CFileDialog dlg(TRUE,NULL,"*.wzq",NULL,"wuziqi Files (*.wzq)");
	if(dlg.DoModal()==IDOK)
	{
		char temp1[1],temp2[3];
		int nCountx,nCounty;
		CStdioFile file;
		if(!file.Open(dlg.GetPathName(),CFile::modeRead|CFile::typeText))
		{
			AfxMessageBox("Can't open file!");
			return;
		}	
		for(nCountx=0;nCountx<=17;nCountx++)
		{
			for(nCounty=0;nCounty<=17;nCounty++)
			{
				file.Read(temp1,1);				
				if(temp1[0]=='0') all.allqipan[nCountx][nCounty]=0;
				if(temp1[0]=='1') all.allqipan[nCountx][nCounty]=1;
				if(temp1[0]=='!') all.allqipan[nCountx][nCounty]=-1;
				if(temp1[0]!='0'&&temp1[0]!='1'&&temp1[0]!='!')
				{
					AfxMessageBox("File error!");
					OnRuleRestart();
					return;
				}
			}
			file.Read(temp1,1);
		}
		file.Read(temp2,3);
		who=temp2[0]-1;black.nCountkey=temp2[1];white.nCountkey=temp2[2];
		if(who!=1&&who!=-1||(temp2[1]-temp2[2])*(temp2[1]-temp2[2])>1)
		{
	
					AfxMessageBox("File error!");
					OnRuleRestart();
					return;
		}
		Invalidate();
	}

}
 

void CWuziqiView::OnFileSave() 
{//保存棋谱文件
	/*if(connect==1)
	{
		AfxMessageBox("You must disconnect first!");
		return;
	}*/
	CFileDialog dlg(FALSE,NULL,"*.wzq",NULL,"wuziqi Files (*.wzq)");
	if(dlg.DoModal()==IDOK)
	{
		char temp1[1],temp2[3];
		int nCountx,nCounty;
		CStdioFile file;
		if(!file.Open(dlg.GetPathName(),CFile::modeWrite|CFile::typeText))
		{			
			AfxMessageBox("Can't open file!");
			return;
		}	
		for(nCountx=0;nCountx<=17;nCountx++)
		{
			for(nCounty=0;nCounty<=17;nCounty++)
			{
				temp1[0]=all.allqipan[nCountx][nCounty]+'0';
				if(temp1[0]==-1+'0')temp1[0]='!';
				file.Write(temp1,1);
			}
			temp1[0]='\n';
			file.Write(temp1,1);
		}
		temp2[0]=who+1;temp2[1]=black.nCountkey;temp2[2]=white.nCountkey;
		file.Write(temp2,3);
	}

}

void CWuziqiView::OnFinalRelease() 
{

	if(connect==1)
	{
			SEND[0]='!';
	AfxBeginThread(ClientSocketThreadProc, GetSafeHwnd());

	}
	CView::OnFinalRelease();
}

void CWuziqiView::OnDestroy() 
{
	if(connect==1)
	{
			SEND[0]='!';
	AfxBeginThread(ClientSocketThreadProc, GetSafeHwnd());
	}
Sleep(2000);
	CView::OnDestroy();
	
	// TODO: Add your message handler code here
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -