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

📄 jingziqiview.cpp

📁 井字棋
💻 CPP
📖 第 1 页 / 共 2 页
字号:
				flagcount=1;break;
			}
	}
	for (i=0;i<3;i++)
	{
		for (j=0;j<3;j++)
			temp=temp+abs(QP[i][j]);
	}
	if(temp==8)
	{
		temp=0;
		for (i=0;i<8;i++)
		{
			if (abs(count[i])>temp)
				temp=abs(count[i]);
		}
		if (temp<=1)
		{
			
			over=3;
			ShowState();
		//	MessageBox("  和  棋","结果",MB_OK);
			// legal=true;
		//	delay();
		  if(mode==1)
		  {
			Sleep(2000);
			OnStart();
			SetTimer(1,300,NULL);
		//	over=0;
		  }
			return(true);
		}
	}
	if(flagcount==0)
	{
		
		for(i=0;i<8;i++)
		{
			if (count[i]>max)
				max=count[i];
			if (count[i]<min)
				min=count[i];
		}
		if (max==3)/*人赢*/
		{
			if(mode==1)
			{
				
				over=2;
				ShowState();
			//	MessageBox("恭喜你赢了!","结果",MB_OK);
			//	delay();
				Sleep(2000);
			}
			else if(mode==2)
			{
				if(IsServer)
				{
					
					over=1;
					ShowState();
				//	MessageBox("呵呵! 你输了!","结果",MB_OK);
				//	delay();
				//	Sleep(2000);
				}
				else
				{
					
					over=2;
					ShowState();
				//	MessageBox("你太棒了!","结果",MB_OK);
				//	delay();
				//	Sleep(2000);
				}
			}
		// legal=true;
		if(mode==1)	
		{
			OnStart();
			SetTimer(1,300,NULL);
		//	over=0;
		}
			return(true);
		}
		if (min==-3)/*计算机赢*/
		{
			if(mode==1)
			{
			
				over=1;
				ShowState();
			//	MessageBox("不好意思,你输了!","结果",MB_OK);
			//	delay();
				Sleep(2000);
			}
			else if(mode==2)
			{
				if(!IsServer)
				{
					
					over=1;
					ShowState();
				//	MessageBox("呵呵! 你输了!","结果",MB_OK);
				//	delay();
				//	Sleep(2000);
				}
				else
				{
					
					over=2;
					ShowState();
				//	MessageBox("你太棒了!","结果",MB_OK);
				//	delay();
				//	Sleep(2000);
				}
			}
			// legal=true;
		if(mode==1)	
		{
			OnStart();
			SetTimer(1,300,NULL);
		//	over=0;
		}
			return(true);
		}
		
		// legal=true;
		
		over=3;
		ShowState();
	//	MessageBox("  和  棋","结果",MB_OK);
	//	delay();
		if(mode==1)
		{
		  Sleep(2000);
		  OnStart();
		  SetTimer(1,300,NULL);
		//  over=0;
		}
		return(true);
	}
	else for(i=0;i<8;i++)
	{
		if (count[i]==3)/*人赢*/
		{
			
			if(mode==1)
			{
				
				over=2;
				ShowState();
			//	MessageBox("了不起,你赢了!","结果",MB_OK);
			//	delay();
				Sleep(2000);
			}
			else if(mode==2)
			{
				if(IsServer)
				{
					
					over=1;
					ShowState();
				//	MessageBox("呵呵! 你输了!","结果",MB_OK);
				//	delay();
				//	Sleep(2000);
				}
				else
				{
					
					over=2;
					ShowState();
				//	MessageBox("你太棒了!","结果",MB_OK);
				//	delay();
				//	Sleep(2000);
				}
			}
			// legal=true;
		if(mode==1)	
		{
			OnStart();
			SetTimer(1,300,NULL);
		//	over=0;
		}
			return(true);
		}
		if (count[i]==-3)/*计算机赢*/
		{
			
			if(mode==1)
			{
				
				over=1;
				ShowState();
			//	MessageBox("你输了,快加油!","结果",MB_OK);
			//	delay();
				Sleep(2000);
			}
			else if(mode==2)
			{
				if(!IsServer)
				{
					
					over=1;
					ShowState();
				//	MessageBox("呵呵! 你输了!","结果",MB_OK);
				//	delay();
				//	Sleep(2000);
				}
				else
				{
					
					over=2;
					ShowState();
				//	MessageBox("你太棒了!","结果",MB_OK);
				//	delay();
				//	Sleep(2000);
				}
			}
			// legal=true;
		if(mode==1)	
		{
			
			OnStart();
			SetTimer(1,300,NULL);
		//	over=0;
		}
			return(true);
		}
		
	}
	
	ShowState();
	return(false);	
}


void CJingZiQiView::OnStart() 
{
	// TODO: Add your command handler code here
	CMainFrame *pMain=(CMainFrame*)AfxGetApp()->m_pMainWnd;
	if(mode==2&&over)
	{
	/*	cmptfirst=!cmptfirst;
		if(cmptfirst)
				man_turn=0;
			else
				man_turn=1;*/

		char Send_pMsg[1000];//,Show_Msg[1000];
		strcpy(Send_pMsg,"");
	//	strcpy(Show_Msg,"");
		if(requ_reply)
		{
			sprintf(Send_pMsg,"Q:%s","对方要求再来一盘!");		
		//	sprintf(Show_Msg,"%s","已发出挑战,请等回复...");
		//	ShowReq_Rep(Show_Msg);
		}
		else
		{
			sprintf(Send_pMsg,"P:%s","对方接受挑战!");
		//	sprintf(Show_Msg,"%s","对方请你先下!");
		//	ShowReq_Rep(Show_Msg);
		}

		if(pMain->m_pConnect&&IsServer)
		{
			pMain->SendMsg(pMain->m_pConnect,Send_pMsg);
			if(requ_reply)
				man_turn=1;
			else
			{
				man_turn=0;
				requ_reply=true;
			}
		}
		else if(pMain->m_pClient&&!IsServer)
		{
			pMain->SendMsg(pMain->m_pClient,Send_pMsg);
			if(requ_reply)
				man_turn=0;
			else
			{
				man_turn=1;
				requ_reply=true;
			}
		}
		over=0;
		for(int i=0;i<3;i++)
			for(int j=0;j<3;j++)
				QP[i][j]=0;
		Invalidate();
		return;
	}
	else if(mode==1)
	{
	//	over=0;
		for(int i=0;i<3;i++)
			for(int j=0;j<3;j++)
				QP[i][j]=0;
			Invalidate();
		if(cmptfirst)
		{
			int i,j;
			srand((int)time(NULL));
			i=rand()%3;
			j=rand()%3;
		
			CDC DC,*pDC;
			pDC=GetDC();
			if(DC.CreateCompatibleDC(pDC)==0)
			{
				AfxMessageBox("Fail to create DC!");
				return;
			}
			DC.SelectObject(m_bmRed);
			pDC->BitBlt(64+60*i,64+60*j,60,60,&DC,0,0,SRCCOPY);
			QP[i][j]=-1;
			man_turn=1;
		
		}
		else
			man_turn=1;
	}
	
}

int CJingZiQiView::hard()
{
	CMainFrame *pMain=(CMainFrame*)AfxGetApp()->m_pMainWnd;
	CDC *pDC=GetDC();
	CDC DC;
	if(DC.CreateCompatibleDC(pDC)==FALSE)
		MessageBox("fail to create DC!");
	if ((QP[0][0]==0)&&(QP[0][1]==0)&&(QP[0][2]==0)&&(QP[1][0]==0)&&(QP[1][1]==0)&&(QP[1][2]==1)&&(QP[2][0]==0)&&(QP[2][1]==0)&&(QP[2][2]==0))
	{
		QP[1][1]=-1;
		DC.SelectObject(m_bmRed);
		pDC->BitBlt(64+60*1,64+60*1,60,60,&DC,0,0,SRCCOPY);
		man_turn=1;
		return 1;
	}
	if ((QP[0][0]==0)&&(QP[0][1]==0)&&(QP[0][2]==0)&&(QP[1][0]==0)&&(QP[1][1]==0)&&(QP[1][2]==0)&&(QP[2][0]==0)&&(QP[2][1]==1)&&(QP[2][2]==0))
	{
		QP[1][1]=-1;
		DC.SelectObject(m_bmRed);
		pDC->BitBlt(64+60*1,64+60*1,60,60,&DC,0,0,SRCCOPY);
		man_turn=1;
		return 1;
	}
	if ((QP[0][0]==0)&&(QP[0][1]==0)&&(QP[0][2]==0)&&(QP[1][0]==0)&&(QP[1][1]==0)&&(QP[1][2]==0)&&(QP[2][0]==0)&&(QP[2][1]==0)&&(QP[2][2]==1))
	{
		QP[1][1]=-1;
		DC.SelectObject(m_bmRed);
		pDC->BitBlt(64+60*1,64+60*1,60,60,&DC,0,0,SRCCOPY);
		man_turn=1;
		return 1;
	}
	if ((QP[0][0]==-1)&&(QP[0][1]==0)&&(QP[0][2]==0)&&(QP[1][0]==0)&&(QP[1][1]==0)&&(QP[1][2]==0)&&(QP[2][0]==0)&&(QP[2][1]==0)&&(QP[2][2]==1))
	{
		QP[1][1]=-1;
		DC.SelectObject(m_bmRed);
		pDC->BitBlt(64+60*1,64+60*1,60,60,&DC,0,0,SRCCOPY);
		man_turn=1;
		return 1;
	}
	if ((QP[0][0]==-1)&&(QP[0][1]==0)&&(QP[0][2]==0)&&(QP[1][0]==0)&&(QP[1][1]==0)&&(QP[1][2]==0)&&(QP[2][0]==0)&&(QP[2][1]==1)&&(QP[2][2]==0))
	{
		QP[1][1]=-1;
		DC.SelectObject(m_bmRed);
		pDC->BitBlt(64+60*1,64+60*1,60,60,&DC,0,0,SRCCOPY);
		man_turn=1;
		return 1;
	}
	if ((QP[0][0]==-1)&&(QP[0][1]==0)&&(QP[0][2]==0)&&(QP[1][0]==0)&&(QP[1][1]==0)&&(QP[1][2]==1)&&(QP[2][0]==0)&&(QP[2][1]==0)&&(QP[2][2]==0))
	{
		QP[1][1]=-1;
		DC.SelectObject(m_bmRed);
		pDC->BitBlt(64+60*1,64+60*1,60,60,&DC,0,0,SRCCOPY);
		man_turn=1;
		return 1;
	}
	if ((QP[0][0]==0)&&(QP[0][1]==0)&&(QP[0][2]==0)&&(QP[1][0]==0)&&(QP[1][1]==-1)&&(QP[1][2]==1)&&(QP[2][0]==0)&&(QP[2][1]==1)&&(QP[2][2]==0))
	{
		QP[2][2]=-1;
		DC.SelectObject(m_bmRed);
		pDC->BitBlt(64+60*2,64+60*2,60,60,&DC,0,0,SRCCOPY);
		man_turn=1;
		return 1;
	}
	if ((QP[0][0]==-1)&&(QP[0][1]==0)&&(QP[0][2]==0)&&(QP[1][0]==0)&&(QP[1][1]==1)&&(QP[1][2]==0)&&(QP[2][0]==0)&&(QP[2][1]==0)&&(QP[2][2]==1))
	{
		QP[2][0]=-1;
		DC.SelectObject(m_bmRed);
		pDC->BitBlt(64+60*2,64+60*0,60,60,&DC,0,0,SRCCOPY);
		man_turn=1;
		return 1;
	}
	return 0;
}

void CJingZiQiView::Empty()
{
	for(int i=0;i<3;i++)
		for(int j=0;j<3;j++)
			QP[i][j]=0;
	Invalidate();
}

void CJingZiQiView::SendPosInfo(int x,int y,CMySocket *pSocket)
{
	char px[5],py[5];
	char Msg[100];

	if(!pSocket)
	{
		MessageBox("没有连接到任何服务器!","错误",MB_OK);
		return;
	}

	itoa(x,px,10);
	itoa(y,py,10);
	strcpy(Msg,"");
	strcat(Msg,px);
	strcat(Msg," ");
	strcat(Msg,py);
	strcat(Msg," ");
	UpdateData();
	CMainFrame *pFrame=(CMainFrame *)AfxGetApp()->m_pMainWnd;
	UpdateData(FALSE);	
	if(pSocket)
	{
		char pMsg[100];
		sprintf(pMsg,"%s",Msg);
		pFrame->SendMsg(pSocket,pMsg);
	}
}

void CJingZiQiView::OpponentDone()
{
	CMainFrame *pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd;
	int x=pFrame->m_point.x;
	int y=pFrame->m_point.y;
	
	if(IsServer)
	{
		man_turn=0;
		QP[x][y]=1;
	}
	else
	{
		man_turn=1;
		QP[x][y]=-1;
	}
	Invalidate();
	result();
}

void CJingZiQiView::ShowState()
{
	CBrush mybrush;
	CDC *pDC=GetDC();
	
	CRect CltRect,rect;
	GetClientRect(CltRect);
	rect.CopyRect(CltRect);
	rect.left+=300;

	//画背景
	mybrush.CreateSolidBrush(BCOLOR);
	pDC->FillRect(rect,&mybrush);

/*	CFont font,*oldfont;
	font.CreateFont(25,8,0,20,80,0,0,0,ANSI_CHARSET,
		OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,0,NULL);
//	font.CreateFont(0, 0, 0, 0, 55, FALSE, FALSE,0,0,0,0,0,0, "Arial"); 
	oldfont=pDC->SelectObject(&font);
	COLORREF OldColor=pDC->SetTextColor(::GetSysColor(COLOR_3DHILIGHT));
//	pDC->SetTextColor(::GetSysColor(COLOR_3DSHADOW));*/

//定义显示man_turn、computerfirst、Level、mode状态信息和输赢的区域
	CRect ModeRect(rect.left+5,rect.top+50,rect.right,rect.top+80);
	CRect LevelRect(rect.left+5,rect.top+90,rect.right,rect.top+120);
	CRect FstRect(rect.left+5,rect.top+130,rect.right,rect.top+160);
	CRect TurnRect(rect.left+5,rect.top+170,rect.right,rect.top+200);
	CRect ReltRect(rect.left+5,rect.top+210,rect.right,rect.top+240);
	
	pDC->SetTextColor(TCOLOR);
//	pDC->SetBkColor(BCOLOR);
	pDC->SetBkMode(TRANSPARENT);
 if(mode==1)
	pDC->DrawText("模式: 人机对弈",ModeRect,0);
 else
	 pDC->DrawText("模式: 联网游戏",ModeRect,0);
 if(Level==1&&mode==1)
	pDC->DrawText("难度: 易",LevelRect,0);
 else if(mode==1)
	pDC->DrawText("难度: 难",LevelRect,0);
 if(mode==1)
 {
	if(cmptfirst)
		pDC->DrawText("先下: 计算机",FstRect,0);
	else 
		pDC->DrawText("先下: 人",FstRect,0);
 }
 else if(over)
 {
	pDC->DrawText("点击""NEW""发出挑战!",FstRect,0);
 }
 if(mode==1)
 {
	if(man_turn&&!over)
		pDC->DrawText("当前: 该你下了!",TurnRect,0);
	else if(!over)
		pDC->DrawText("当前: 计算机下,稍等...",TurnRect,0);
	else
		pDC->DrawText("当前: 游戏结束!",TurnRect,0);
 }
 else
 {
	 CMainFrame *pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd;
	 if(!over)
	 {
		 if((!pFrame->m_pConnect&&IsServer)||(!pFrame->m_pClient&&!IsServer))
			 pDC->DrawText("当前: 等待连接...",TurnRect,0);
		 else
		 {
	   		if((IsServer&&man_turn==0)||(!IsServer&&man_turn))
				 pDC->DrawText("当前: 该你下了!",TurnRect,0);
			else
				pDC->DrawText("当前: 对方下,请稍等...",TurnRect,0);
		 }
	 }
	 else
		pDC->DrawText("当前: 游戏结束!",TurnRect,0);
 }
 switch(over)
 {
	case 0:	if(!over) pDC->DrawText("结果: 正在进行...",ReltRect+CPoint(1,1),0);break;
	case 1: pDC->SetTextColor(TCOLOR2);pDC->DrawText("结果: 你输了-_- 加油!",ReltRect,0);break;
	case 2: pDC->SetTextColor(TCOLOR2);pDC->DrawText("结果: 你赢了,真棒!^_^",ReltRect,0);break;
	case 3: pDC->SetTextColor(TCOLOR2);pDC->DrawText("结果: 打成平手!",ReltRect,0);break;
	default:break;
 }
/*	pDC->SetTextColor(::GetSysColor(COLOR_3DSHADOW));
	pDC->DrawText("结果: 正在进行...",ReltRect,0);
	pDC->SetTextColor(OldColor);
	pDC->SelectObject(oldfont);*/	
}

void CJingZiQiView::OnStart2()
{
	OnStart();
}

void CJingZiQiView::delay()
{
	for(long i=0;i<13000;i++)
		for(long j=0;j<13000;j++);

}



void CJingZiQiView::ShowReq_Rep(char *pMsg)
{
	CDC *pDC=GetDC();
	
		CRect CltRect,rect;
		GetClientRect(CltRect);
		rect.CopyRect(CltRect);
		rect.left+=300;	
		CRect FstRect(rect.left+5,rect.top+130,rect.right,rect.top+160);
		pDC->SetTextColor(TCOLOR);
		pDC->SetBkColor(BCOLOR);
		pDC->DrawText(pMsg,FstRect,0);
}

void CJingZiQiView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(nIDEvent==1)
	{
		over=0;
		ShowState();
	}
	CView::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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