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

📄 bwchessdlg.cpp

📁 这是我用vc编写的一个黑白棋网络游戏的使用程序
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	{	
		//在棋盘内,显示 IDC_WHITE_HAND:IDC_BLACK_HAND 型 光标
	                          //  0:  WHITE  1 : BLACK,m_byColor代表前一个棋手的颜色,即还没有更新
		::SetCursor(AfxGetApp()->LoadCursor(m_byColor ? IDC_WHITE_HAND:IDC_BLACK_HAND));
		return TRUE;
	}
  // 其他作默认处理
	return CDialog::OnSetCursor(pWnd, nHitTest, message);
}

void CBWChessDlg::OnSysCommand(UINT nID, LPARAM lParam) 
{
	if(nID == SC_CLOSE)
		OnExit();
	
	CDialog::OnSysCommand(nID, lParam);
}

void CBWChessDlg::OnContextMenu(CWnd* pWnd, CPoint point) //弹出菜单
{
	CRect rect;
	GetClientRect(&rect);
	CPoint pt=point;
	ScreenToClient(&pt);
	if(!rect.PtInRect (pt))//不在棋盘内
	{
		CDialog::OnContextMenu (pWnd,point);
		return;
	}
	if(g_nIsDemo)//正在演示(不会用到,因为此时互斥!)
		return;
	CMenu menu;
	menu.LoadMenu(IDR_MENU_CONTEXT_CHINESE);//载入菜单

	if(m_bGameOver)
	{
		menu.EnableMenuItem(IDM_UNDO, MF_GRAYED);   
		menu.EnableMenuItem(IDM_HINT, MF_GRAYED);   
		menu.EnableMenuItem(IDM_CANPLACE, MF_GRAYED);
		menu.EnableMenuItem(IDM_SAVEINFO,MF_ENABLED);
		menu.EnableMenuItem(IDM_REPLAY,MF_ENABLED);
	}
	if(!m_bGameOver)
	{
		menu.EnableMenuItem(IDM_SAVEINFO,MF_GRAYED);
		menu.EnableMenuItem(IDM_REPLAY,MF_GRAYED);
	}
	if(m_IsGameStart)
		menu.EnableMenuItem(IDM_SAVE, MF_GRAYED);

    menu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_RIGHTBUTTON, 
										   point.x, point.y, this);//弹出菜单
}
////////////////////////// End of BWCHESS.CPP ////////////////////////////////

int CBWChessDlg::BtoW(int x1, int y1, int flag)//翻转棋子!!!
{ //x1 纵坐标  y1横坐标
	int yes;//if yes ==0 then there is no position to change
			//if yes==1 then there is any position to change
	int temp=flag;
	int x=0,y=0,i=0,j=0;
	int result=0;//result==0 indicate that x1,y1 is the wrong position
				 //result==1 indicate that they are legal position
	if(x1<0 || x1>=NUM || y1<0 || y1>=NUM)//棋子的位置在棋盘外面
		return 0;
	//横向--当前左
	x=x1;
	//y==0;
	yes=0;
  	//    kernel[NUM][NUM]    :     0 for none,1 for white,2 for black
	for(i=y1-1;i>=0;i--)
	{
		if(kernel[x][i]==0)
		{
			yes=0;
			break;
		}
		else if(kernel[x][i]==temp)
			break;
		else
			yes=1;
	}
	if((i!=0-1) && (yes==1))
	{
		for(i++;i<y1;i++)
		{//    kernel[NUM][NUM]    :     0 for none,1 for white,2 for black
			kernel[x][i]=temp;//temp代表当前棋手的颜色,将棋子翻成自己的颜色!!
			result=1;
		}
	}
	//横向--当前向右
	//x==x1;
	//y==NUM;
	yes=0;
	for(i=y1+1;i<NUM;i++)
	{
		if(kernel[x][i]==0)
		{
			yes=0;
			break;
		}
		else if(kernel[x][i]==temp)
			break;
		else
			yes=1;
	}
	if((i!=NUM) && (yes==1))
	{
		for(i--;i>y1;i--)
		{//    kernel[NUM][NUM]    :     0 for none,1 for white,2 for black
			kernel[x][i]=temp;
			result=1;
		}
	}
	//竖向--向上
	//x==0;
	y=y1;
	yes=0;
	for(i=x1-1;i>=0;i--)
	{
		if(kernel[i][y]==0)
		{
			yes=0;
			break;
		}
		else if(kernel[i][y]==temp)
			break;
		else
			yes=1;
	}
	if((i!=0-1) && (yes==1))
	{
		for(i++;i<x1;i++)
		{//    kernel[NUM][NUM]    :     0 for none,1 for white,2 for black
			kernel[i][y]=temp;
			result=1;
		}
	}
	//竖向--向下
	//x==NUM;
	//y==y1;
	yes=0;
	for(i=x1+1;i<NUM;i++)
	{
		if(kernel[i][y]==0)
		{
			yes=0;
			break;
		}
		else if(kernel[i][y]==temp)
			break;
		else
			yes=1;
	}
	if((i!=NUM) && (yes==1))
	{
		for(i--;i>x1;i--)
		{
			kernel[i][y]=temp;
			result=1;
		}
	}
	//斜向左上
	//x==0;
	//y==y1;
	yes=0;
	for(i=x1-1,j=y1-1;(i>=0)&&(j>=0);i--,j--)
	{
		if(kernel[i][j]==0)
		{
			yes=0;
			break;
		}
		else if(kernel[i][j]==temp)
			break;
		else
			yes=1;
	}

	if((i!=0-1) && (j!=-1) && (yes==1))
	{
		for(i++,j++;i<x1;i++,j++)
		{
			kernel[i][j]=temp;
			result=1;
		}
	}
	//斜向上向右
	yes=0;

	for(i=x1-1,j=y1+1;(i>=0)&&(j<NUM);i--,j++)
	{
		if(kernel[i][j]==0)
		{
			yes=0;
			break;
		}
		else if(kernel[i][j]==temp)
			break;
		else
			yes=1;
	}

	if((i!=0-1) && (j!=NUM) && (yes==1))
	{
		for(i++,j--;i<x1;i++,j--)
		{
			kernel[i][j]=temp;
			result=1;
		}
	}
	//斜向左下
	yes=0;

	for(i=x1+1,j=y1-1;(i<NUM)&&(j>=0);i++,j--)
	{
		if(kernel[i][j]==0)
		{
			yes=0;
			break;
		}
		else if(kernel[i][j]==temp)
			break;
		else
			yes=1;
	}

	if((i!=NUM) && (j!=-1) && (yes==1))
	{
		for(i--,j++;i>x1;i--,j++)
		{
			kernel[i][j]=temp;
			result=1;
		}
	}
	//斜向右下
	yes=0;

	for(i=x1+1,j=y1+1;(i<NUM)&&(j<NUM);i++,j++)
	{
		if(kernel[i][j]==0)
		{
			yes=0;//无改动
			break;
		}
		else if(kernel[i][j]==temp)
			break;
		else
			yes=1;//有改动
	}

	if((i!=NUM) && (j!=NUM) && (yes==1))
	{
		for(i--,j--;i>x1;i--,j--)
		{
			kernel[i][j]=temp;
			result=1;
		}
	}
	if(result==1)
		kernel[x1][y1]=temp;
	return result;//返回状态 1:OK,0:ERROR
}

int CBWChessDlg::IsEnd(/*int *x1, int *y1, */int whogo)
{//whogo 1:代表黑色,2代表白色
	int i,j;//行列坐标
	int x,y;
	int wn=0,bn=0; //wn:代表白色可放棋子的个数,bn:代表黑色可放棋子的个数(本函数的功能1)
	int btp_x=-7,btp_y=-7,wtp_x=-7,wtp_y=-7;
	int temp,temp2;

	if(!wsp.isempty())
		wsp.destroy();//先清空,再存储新的可下子的位置(本函数的功能2)

	if(!bsp.isempty())
		bsp.destroy();

	for(i=0;i<NUM;i++)
		for(j=0;j<NUM;j++)
		{
			if(kernel[i][j]==0)
			{
				//水平向左
				if(Check(i,j-1) && Check(i,j-2))//没有越界且此位置有棋子
				{
					x=i,y=j;
					temp=kernel[x][y-1];
					for(y-=2;y>=0;y--)
					{
						temp2=kernel[x][y];
						if(temp2==0)
							break;
						if(temp2!=temp)
						{
							if(temp2==1)
							{
								if(wtp_x!=i || wtp_y!=j)
								{
									wtp_x=i,wtp_y=j;
									wn++;
									wsp.push (i,j);//白色可放子的位置
								}
							}
							else
							{
								if(btp_x!=i || btp_y!=j)
								{
									btp_x=i,btp_y=j;
									bn++;
									bsp.push (i,j);//黑色可放子的位置
								}
							}
							break;
						}
					}
				}
				//水平向右
				if(Check(i,j+1) && Check(i,j+2))
				{
					x=i,y=j;
					temp=kernel[x][y+1];
					for(y+=2;y<NUM;y++)
					{
						temp2=kernel[x][y];
						if(temp2==0)
							break;
						if(temp2!=temp)
						{
							if(temp2==1)
							{
								if(wtp_x!=i || wtp_y!=j)
								{
									wtp_x=i,wtp_y=j;
									wn++;
									wsp.push (i,j);
								}
							}
							else
							{
								if(btp_x!=i || btp_y!=j)
								{
									btp_x=i,btp_y=j;
									bn++;
									bsp.push (i,j);
								}
							}
							break;
						}
					}
				}
				//垂直向上
				if(Check(i-1,j) && Check(i-2,j))
				{
					x=i,y=j;
					temp=kernel[x-1][y];
					for(x-=2;x>=0;x--)
					{
						temp2=kernel[x][y];
						if(temp2==0)
							break;
						if(temp2!=temp)
						{
							if(temp2==1)
							{
								if(wtp_x!=i || wtp_y!=j)
								{
									wtp_x=i,wtp_y=j;
									wn++;
									wsp.push (i,j);
								}
							}
							else
							{
								if(btp_x!=i || btp_y!=j)
								{
									btp_x=i,btp_y=j;
									bn++;
									bsp.push (i,j);
								}
							}
							break;
						}
					}
				}

				//垂直向下
				if(Check(i+1,j) && Check(i+2,j))
				{
					x=i,y=j;
					temp=kernel[x+1][y];
					for(x+=2;x<NUM;x++)
					{
						temp2=kernel[x][y];
						if(temp2==0)
							break;
						if(temp2!=temp)
						{
							if(temp2==1)
							{
								if(wtp_x!=i || wtp_y!=j)
								{
									wtp_x=i,wtp_y=j;
									wn++;
									wsp.push (i,j);
								}
							}
							else
							{
								if(btp_x!=i || btp_y!=j)
								{
									btp_x=i,btp_y=j;
									bn++;
									bsp.push (i,j);
								}
							}
							break;
						}
					}
				}

				//斜向左上
				if(Check(i-1,j-1) && Check(i-2,j-2))
				{
					x=i,y=j;
					temp=kernel[x-1][y-1];
					for(x-=2,y-=2;(x>=0) && (y>=0);x--,y--)
					{
						temp2=kernel[x][y];
						if(temp2==0)
							break;
						if(temp2!=temp)
						{
							if(temp2==1)
							{
								if(wtp_x!=i || wtp_y!=j)
								{
									wtp_x=i,wtp_y=j;
									wn++;
									wsp.push (i,j);
								}
							}
							else
							{
								if(btp_x!=i || btp_y!=j)
								{
									btp_x=i,btp_y=j;
									bn++;
									bsp.push (i,j);
								}
							}
							break;
						}
					}
				}

				//斜向右上
				if(Check(i-1,j+1) && Check(i-2,j+2))
				{
					x=i,y=j;
					temp=kernel[x-1][y+1];
					for(x-=2,y+=2;(x>=0) && (y<NUM);x--,y++)
					{
						temp2=kernel[x][y];
						if(temp2==0)
							break;
						if(temp2!=temp)
						{
							if(temp2==1)
							{
								if(wtp_x!=i || wtp_y!=j)
								{
									wtp_x=i,wtp_y=j;
									wn++;

⌨️ 快捷键说明

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