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

📄 cchessuidlg.cpp

📁 中国象棋人机对弈
💻 CPP
📖 第 1 页 / 共 5 页
字号:
inline void GameOver()
{
	fGameover = true;

	KillTimer((HWND)hhWnd,1);
	KillTimer((HWND)hhWnd,2);
}

//将鼠标所点位置转换为特定格子像素
inline POINT GetThePoint( CPoint point )
{
	POINT ptTemp;

	ptTemp.x = ((int)((point.x-5)/60))*60+5;
	ptTemp.y = ((int)((point.y-5)/60))*60+5;

	return ptTemp;
}

//将特定格子像素转换为CChessBoard的数组下标
inline POINT PointToCChessBoard( POINT point )
{
	POINT xyTemp;

	xyTemp.x = (point.x-5)/60;
	xyTemp.y = 9 - (point.y-5)/60;

	return xyTemp;
}

//将CChessBoard的数组下标转换为特定格子像素
inline POINT CChessBoardToPoint( POINT xyPoint )
{
	POINT ptTemp;

	ptTemp.x = xyPoint.x*60 + 5;
	ptTemp.y = (9 - xyPoint.y)*60 +5;

	return ptTemp;
}

//判断特定格子像素的棋子状况,返回如RED_B1形式或NOCHESS
inline int JudgeThePoint( POINT point )
{
	int i;
	for( i=0; i<32; i++ )
	{
		if( point.x == ptChess[i].x && point.y == ptChess[i].y )
		{
			return i;
		}
	}

	return NOCHESS;
}

//判断nA与nB是否是同一方的子,若是返回true,否则返回false
inline bool IsTheSameSide( int nA, int nB )
{
	if( nB == NOCHESS )
	{
		return false;
	}
	if( nA>=16 && nB>=16 )
	{
		return true;
	}
	if( nA<=15 && nB<=15 )
	{
		return true;
	}

	return false;
}

//判断是否游戏已经结束,红胜返回REDWIN,黑胜返回BLACKWIN,未结束返回NOTEND
inline int IsGameOverUI()
{
	if( ptChess[RED_K0].x == -100 )
	{
		return BLACKWIN;
	}
	if( ptChess[BLACK_K0].x == -100 )
	{
		return REDWIN;
	}
	return NOTEND;
}

//判断走法是否合法,合法返回真,不合法返回假
inline bool CanMOVE(POINT ptfrom, POINT ptto)
{
	int x1 = ptfrom.x;
	int y1 = ptfrom.y;
	int x2 = ptto.x;
	int y2 = ptto.y;
	int chnum = 0;

	int chessid = CChessBoard[x1][y1];
	switch (chessid)
	{
	case RED_K:
		if (x2 > 5 || x2 < 3 || y2 > 2)
			return false;
		if (abs(x2 - x1) > 1 || abs(y2 - y1) > 1)
			return false;
		if (abs(x2 - x1) == 1 && abs(y2 - y1) == 1)
			return false;
		//if ( HaveMan(ptto) && isred(ptto))
		//	return false;
		break;

	case BLACK_K:
		if (x2 > 5 || x2 < 3 || y2 < 7)
			return false;
		if (abs(x2 - x1) > 1 || abs(y2 - y1) > 1)
			return false;
		if (abs(x2 - x1) == 1 && abs(y2 - y1) == 1)
			return false;
		//if ( HaveMan(ptto) && isblack(ptto))
		//	return false;
		break;

	case RED_J:
	case BLACK_J:
		if (x2 - x1 != 0 && y2 - y1 != 0)
			return false;
		/*if (HaveMan(ptto))
		{
			if (CChessBoard[x1][y1] == RED_J && isred(ptto))
				return false;
			if (CChessBoard[x1][y1] == BLACK_J && isblack(ptto))
				return false;
		}*/
		if (x2 > x1)
		{
			for (int i = 1; i < x2 - x1; i++)
			{	
				if (CChessBoard[x1 + i][y1] != 0)
					return false;
			}	
			return true;
		}
		if (x2 < x1)
		{
			for (int i = 1; i < x1 - x2; i++)
			{	
				if (CChessBoard[x1 - i][y1] != 0)
					return false;
			}
			return true;	
		}
		if (y2 > y1)
		{
			for (int i = 1; i < y2 - y1; i++)
			{	
				if (CChessBoard[x1][y1 + i] != 0)
					return false;
			} 
			return true;
		}
		if (y2 < y1)
		{
			for (int i = 1; i < y1 - y2; i++)
			{	
				if (CChessBoard[x1][y1 - i] != 0)
					return false;
			}  
			return true;
		}
		break;

	case RED_S:
		if (x2 > 5 || x2 < 3 || y2 > 2)
			return false;
		if (abs(x2 - x1) == 1 && abs(y2 - y1) == 1)
		{
			//if (HaveMan(ptto) && isred(ptto))
			//	return false;
			//else
				return true;
		}
		else
		{
			return false;
		}
		break;

	case BLACK_S:
		if (x2 > 5 || x2 < 3 || y2 < 7)
			return false;
		if (abs(x2 - x1) == 1 && abs(y2 - y1) == 1)
		{
			//if (HaveMan(ptto) && isblack(ptto))
			//	return false;
			//else
				return true;
		}
		else
		{
			return false;
		}
		break;

	case RED_M:
	case BLACK_M:

		if	(abs(x2 - x1) >2 || abs(y2 - y1) >2)
			return false;
		if (abs(x2 - x1) ==0 && abs(y2 - y1) >=1) 
			return false;
		if (abs(x2 - x1) == 1 && abs(y2 - y1) != 2)
			return false;
		if (abs(x2 - x1) == 2 && abs(y2 - y1) != 1)
			return false;
		if ( abs(x2 - x1) == 1)
		{
	
			if (y2 > y1 && CChessBoard[x1][y1 + 1] != 0)
				return false;
			if (y2 < y1 && CChessBoard[x1][y1 - 1] != 0)
				return false;
		}
		else if ( abs(y2 - y1) == 1)
		{
			if (x2 > x1 && CChessBoard[x1 + 1][y1] != 0)
				return false;
			if (x2 < x1 && CChessBoard[x1 - 1][y1] != 0)
				return false;
		}

		break;

	case RED_P:
	case BLACK_P:
		if (x2 - x1 != 0 && y2 - y1 != 0)
			return false;
		/*if (HaveMan(ptto))
		{
			if (CChessBoard[x1][y1] == RED_P && isred(ptto))
				return false;
			if (CChessBoard[x1][y1] == BLACK_P && isblack(ptto))
				return false;
		}*/
		if (y2 > y1 && !HaveMan(ptto))
		{
			for (int i = 1; i < y2 - y1; i++)
			{	
				if (CChessBoard[x1][y1 + i] != 0)
					return false;
			}
			return true;
		}
		if (y2 > y1 && HaveMan(ptto))
		{
			chnum =0;
			for (int i = 1; i < y2 - y1; i++)
			{
				if (CChessBoard[x1][y1 + i] != 0)
					chnum++;
			}
			if (chnum != 1 )
				return false;
			else
				return true;
		}
		if (y2 < y1 && !HaveMan(ptto))
		{
			for (int i = 1; i < y1 - y2; i++)
			{
				if (CChessBoard[x1][y1 - i] != 0)
					return false;
			}
			return true;
		}
		if (y2 < y1 && HaveMan(ptto))
		{
			chnum =0;
			for (int i = 1; i < y1 - y2; i++)
			{
				if (CChessBoard[x1][y1 - i] != 0)
					chnum++;
			}
			if (chnum != 1)
				return false;
			else
				return true;
		}
		if (x2 > x1 && !HaveMan(ptto))
		{
			for (int i = 1; i < x2 - x1; i++)
			{
				if (CChessBoard[x1 + i][y1] != 0)
					return false;
			}
			return true;
		}
		if (x2 > x1 && HaveMan(ptto))
		{
			chnum =0;
			for (int i = 1; i < x2 - x1; i++)
			{
				if (CChessBoard[x1 + i][y1] != 0)
					chnum++;
			}
			if (chnum != 1)
				return false;
			else
				return true;
		}
		if (x2 < x1 && !HaveMan(ptto))
		{
			for (int i = 1; i < x1 - x2; i++)
			{
				if (CChessBoard[x1 - i][y1] != 0)
					return false;
			}
			return true;
		}
		if (x2 < x1 && HaveMan(ptto))
		{
			chnum =0;
			for (int i = 1; i < x1 - x2; i++)
			{
				if (CChessBoard[x1 - i][y1] != 0)
					chnum++;
			}
			if (chnum != 1)
				return false;	
			else
				return true;
		}
		break;

	case RED_X:
		if (y2 > 4)
			return false;
		if (abs(x2 - x1) != 2 || abs(y2 - y1) != 2)
			return false;
		//if (HaveMan(ptto) && isred(ptto))
		//	return false;
		if (CChessBoard[(x1 + x2) / 2][(y2 + y1) / 2] != 0)
			return false;
		break;

	case BLACK_X:
		if (y2 < 5)
			return false;
		if (abs(x2 - x1) != 2 || abs(y2 - y1) != 2)
			return false;
		//if (HaveMan(ptto) && isblack(ptto))
		//	return false;
		if (CChessBoard[(x1 + x2) / 2][(y2 + y1) / 2] != 0)
			return false;
		break;

	case RED_B:
		if (y2 < y1)
			return false;
		//if (HaveMan(ptto) && isred(ptto))
		//	return false;
		if (abs(x2 - x1) + abs(y2 - y1) != 1)
			return false;
		if (y1 <= 4 && abs(x2-x1) != 0)
			return false;
		break;

	case BLACK_B:
		if (y2 > y1)
			return false;
		//if (HaveMan(ptto) && isblack(ptto))
		//	return false;
		if (abs(x2 - x1) + abs(y2 - y1) != 1)
			return false;
		if (y1 >= 5 && abs(x2-x1) != 0)
			return false;
		break;

	default:
		return true;
	}

	return true;
}

/*
inline bool HaveMan(POINT ptPosition )
{
	if (CChessBoard[ptPosition.x][ptPosition.y] == 0)
		return false;
	else
		return true;
}

inline bool isred(POINT ptto)
{
	if (SideOfMan[CChessBoard[ptto.x][ptto.y]] == RED)
		return true;
	return false;
}

inline bool isblack(POINT ptto)
{
	if (SideOfMan[CChessBoard[ptto.x][ptto.y]] == RED)
		return true;
	return false;
}
*/

void CCChessUIDlg::OnBnClickedButton3()//退出
{
	// TODO: 在此添加控件通知处理程序代码
	if(MessageBox("你确定要退出吗?","确认",MB_OKCANCEL)==IDOK)
	{
		EndDialog(IDCANCEL);
	}
	
}

void CCChessUIDlg::OnBnClickedButton4()//新局
{
	KillTimer(1);
	GetDlgItem(IDC_STATIC3)->SetWindowText("00:00:00");
	GetDlgItem(IDC_STATIC4)->SetWindowText("00:00:00");
	Second_a =Minute_a =Hour_a = 0;
	Second_b =Minute_b =Hour_b = 0;
	Second_a2 = 0;
	Second_b2 = 0;

	Restart();

	//重绘屏幕
	InvalidateRect(NULL,FALSE);
	UpdateWindow();
}

//开局
void CCChessUIDlg::Restart()
{
	//清空着法名称列表
	int nCount=pLB->GetCount();
	for(int i=0;i< nCount ;i++)
	{
		pLB->DeleteString(0);
	}

	//禁用悔棋、还原按钮并清空队列
	cb1.EnableWindow(FALSE);
	cb2.EnableWindow(FALSE);

	vBackQueue.clear();
	vForwardQueue.clear();

	vNameQueue.clear();

	//nMoveStackCount =0;

	nNumOfStep =0;

	nHaveSelected = NOCHESS;

	//框的位置
	ptCurKuang.x = -100;
	ptCurKuang.y = -100;
	ptPreKuang = ptCurKuang;

	//红方棋子位置
	ptChess[RED_K0].x	= 245 ;	
	ptChess[RED_K0].y	= 545 ;
	ptChess[RED_Sl].x	= 185 ;	
	ptChess[RED_Sl].y	= 545 ;
	ptChess[RED_Sr].x	= 305 ;	
	ptChess[RED_Sr].y	= 545 ;	
	ptChess[RED_Xl].x	= 125 ;	
	ptChess[RED_Xl].y	= 545 ;
	ptChess[RED_Xr].x	= 365 ;	
	ptChess[RED_Xr].y	= 545 ;	
	ptChess[RED_Ml].x	= 65 ;
	ptChess[RED_Ml].y	= 545 ;	
	ptChess[RED_Mr].x	= 425 ;	
	ptChess[RED_Mr].y	= 545 ;	
	ptChess[RED_Jl].x	= 5 ;
	ptChess[RED_Jl].y	= 545 ;	
	ptChess[RED_Jr].x	= 485 ;	
	ptChess[RED_Jr].y	= 545 ;
	ptChess[RED_Pl].x	= 65 ;
	ptChess[RED_Pl].y	= 425 ;
	ptChess[RED_Pr].x	= 425 ;	
	ptChess[RED_Pr].y	= 425 ;
	ptChess[RED_B1].x	= 5 ;	
	ptChess[RED_B1].y	= 365 ;
	ptChess[RED_B2].x	= 125 ;	
	ptChess[RED_B2].y	= 365 ;
	ptChess[RED_B3].x	= 245 ;	
	ptChess[RED_B3].y	= 365 ;
	ptChess[RED_B4].x	= 365 ;	
	ptChess[RED_B4].y	= 365 ;
	ptChess[RED_B5].x	= 485 ;	
	ptChess[RED_B5].y	= 365 ;

	//黑方棋子位置
	ptChess[BLACK_K0].x	= 245 ;		
	ptChess[BLACK_K0].y	= 5 ;
	ptChess[BLACK_Sl].x	= 185 ;
	ptChess[BLACK_Sl].y	= 5 ;
	ptChess[BLACK_Sr].x	= 305 ;
	ptChess[BLACK_Sr].y	= 5 ;
	ptChess[BLACK_Xl].x	= 125 ;
	ptChess[BLACK_Xl].y	= 5 ;
	ptChess[BLACK_Xr].x	= 365 ;
	ptChess[BLACK_Xr].y	= 5 ;
	ptChess[BLACK_Ml].x	= 65 ;
	ptChess[BLACK_Ml].y	= 5 ;
	ptChess[BLACK_Mr].x	= 425 ;
	ptChess[BLACK_Mr].y	= 5 ;
	ptChess[BLACK_Jl].x	= 5 ;	
	ptChess[BLACK_Jl].y	= 5 ;
	ptChess[BLACK_Jr].x	= 485 ;
	ptChess[BLACK_Jr].y	= 5 ;
	ptChess[BLACK_Pl].x	= 65 ;
	ptChess[BLACK_Pl].y	= 125 ;
	ptChess[BLACK_Pr].x	= 425 ;
	ptChess[BLACK_Pr].y	= 125 ;
	ptChess[BLACK_B1].x	= 5 ;	
	ptChess[BLACK_B1].y	= 185 ;
	ptChess[BLACK_B2].x	= 125 ;
	ptChess[BLACK_B2].y	= 185 ;
	ptChess[BLACK_B3].x	= 245 ;
	ptChess[BLACK_B3].y	= 185 ;
	ptChess[BLACK_B4].x	= 365 ;	
	ptChess[BLACK_B4].y	= 185 ;
	ptChess[BLACK_B5].x	= 485 ;	
	ptChess[BLACK_B5].y	= 185 ;	
/*
	//黑方棋子位置
	//FOR TEST.................................
	ptChess[BLACK_K0].x	= 245 ;		
	ptChess[BLACK_K0].y	= 5 ;
	ptChess[BLACK_Sl].x	= -100 ;
	ptChess[BLACK_Sl].y	= -100 ;
	ptChess[BLACK_Sr].x	= -100 ;
	ptChess[BLACK_Sr].y	= -100 ;
	ptChess[BLACK_Xl].x	= -100 ;
	ptChess[BLACK_Xl].y	= -100 ;
	ptChess[BLACK_Xr].x	= -100 ;
	ptChess[BLACK_Xr].y	= -100 ;
	ptChess[BLACK_Ml].x	= -100 ;
	ptChess[BLACK_Ml].y	= -100 ;
	ptChess[BLACK_Mr].x	= -100 ;
	ptChess[BLACK_Mr].y	= -100 ;
	ptChess[BLACK_Jl].x	= -100 ;	
	ptChess[BLACK_Jl].y	= -100 ;
	ptChess[BLACK_Jr].x	= -100 ;
	ptChess[BLACK_Jr].y	= -100 ;
	ptChess[BLACK_Pl].x	= -100 ;
	ptChess[BLACK_Pl].y	= -100 ;
	ptChess[BLACK_Pr].x	= -100 ;
	ptChess[BLACK_Pr].y	= -100 ;
	ptChess[BLACK_B1].x	= 5 ;	
	ptChess[BLACK_B1].y	= 185 ;
	ptChess[BLACK_B2].x	= 125 ;
	ptChess[BLACK_B2].y	= 185 ;
	ptChess[BLACK_B3].x	= 245 ;
	ptChess[BLACK_B3].y	= 185 ;
	ptChess[BLACK_B4].x	= 365 ;	
	ptChess[BLACK_B4].y	= 185 ;
	ptChess[BLACK_B5].x	= 485 ;	
	ptChess[BLACK_B5].y	= 185 ;	
*/	

	//初始化棋盘
	for( int i=0; i<9; i++ )
	{
		for( int j=0; j<9; j++ )
		{

⌨️ 快捷键说明

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