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

📄 mantischess.cpp

📁 学习VC++游戏编程的 好程序 象棋代码 算法比较复杂
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	g_hIconSelect=LoadIcon(hInst,MAKEINTRESOURCE(IDI_SELECT));

	Reset();
	HDC hdc=GetDC(hWnd);
	g_hdcChessboard=CreateCompatibleDC(hdc);
	g_hbmpChessboard=CreateCompatibleBitmap(hdc,XBW,YBW);
	SelectObject(g_hdcChessboard,g_hbmpChessboard);
	int nCurTime=(int)::GetTickCount();
	MakeBoard(g_hdcChessboard,RGB(180,180+nCurTime%30,160));
	RECT rect={0,0,XBW,YBW};
	AdjustWindowRect(&rect,WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU,TRUE);
	POINT lefttop;
	lefttop.x=(GetSystemMetrics(SM_CXSCREEN)-(rect.right-rect.left))/2;
	lefttop.y=(GetSystemMetrics(SM_CYSCREEN)-(rect.bottom-rect.top))/2;
	rect.left+=lefttop.x;
	rect.right+=lefttop.x;
	rect.top+=lefttop.y;
	rect.bottom+=lefttop.y;
	MoveWindow(hWnd,rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top,TRUE);
}

/******************************************************************
Reset:			把数据恢复为初始值

参数:			无

返回值:			无
******************************************************************/
void Reset()
{
	g_pointChessman[0].x=5;g_pointChessman[0].y=10;	//帅
	g_pointChessman[1].x=4;g_pointChessman[1].y=10;	//士
	g_pointChessman[2].x=6;g_pointChessman[2].y=10;	
	g_pointChessman[3].x=3;g_pointChessman[3].y=10;	//相
	g_pointChessman[4].x=7;g_pointChessman[4].y=10;
	g_pointChessman[5].x=2;g_pointChessman[5].y=10;	//马
	g_pointChessman[6].x=8;g_pointChessman[6].y=10;
	g_pointChessman[7].x=1;g_pointChessman[7].y=10;	//车
	g_pointChessman[8].x=9;g_pointChessman[8].y=10;
	g_pointChessman[9].x=2;g_pointChessman[9].y=8;	//炮
	g_pointChessman[10].x=8;g_pointChessman[10].y=8;
	g_pointChessman[11].x=1;g_pointChessman[11].y=7;//兵
	g_pointChessman[12].x=3;g_pointChessman[12].y=7;
	g_pointChessman[13].x=5;g_pointChessman[13].y=7;
	g_pointChessman[14].x=7;g_pointChessman[14].y=7;
	g_pointChessman[15].x=9;g_pointChessman[15].y=7;
	g_pointChessman[16].x=5;g_pointChessman[16].y=1;//将
	g_pointChessman[17].x=4;g_pointChessman[17].y=1;//士
	g_pointChessman[18].x=6;g_pointChessman[18].y=1;
	g_pointChessman[19].x=3;g_pointChessman[19].y=1;//相
	g_pointChessman[20].x=7;g_pointChessman[20].y=1;
	g_pointChessman[21].x=2;g_pointChessman[21].y=1;//马
	g_pointChessman[22].x=8;g_pointChessman[22].y=1;
	g_pointChessman[23].x=1;g_pointChessman[23].y=1;//车
	g_pointChessman[24].x=9;g_pointChessman[24].y=1;
	g_pointChessman[25].x=2;g_pointChessman[25].y=3;//炮
	g_pointChessman[26].x=8;g_pointChessman[26].y=3;
	g_pointChessman[27].x=1;g_pointChessman[27].y=4;//卒
	g_pointChessman[28].x=3;g_pointChessman[28].y=4;
	g_pointChessman[29].x=5;g_pointChessman[29].y=4;
	g_pointChessman[30].x=7;g_pointChessman[30].y=4;
	g_pointChessman[31].x=9;g_pointChessman[31].y=4;

	g_iSide=RED;

	FixManMap(g_iChessmanMap,g_pointChessman,g_iSide);
	g_pointBoxFrom.x=0;
	g_pointBoxFrom.y=0;
	g_pointBoxTo.x=0;
	g_pointBoxTo.y=0;
	g_iChessmanSelect=32;
	g_iComputerSide=1;
	g_bEndGame=FALSE;
}

/******************************************************************
ShowRect:		重画 prect 指向的区域

参数:
hdc:			设备描述表句柄
prect:			重画区域的指针

返回值:			无
******************************************************************/
void ShowRect(HDC hdc,LPRECT prect)
{
	RECT rc1={0,0,XBW,YBW};
	//取得真正要更新的区域:
	IntersectRect(&rc1,&rc1,prect);
	BitBlt(hdc,rc1.left,rc1.top,rc1.right-rc1.left,rc1.bottom-rc1.top,g_hdcChessboard,rc1.left,rc1.top,SRCCOPY);
	
	int left=(rc1.left)/BWA,top=(rc1.top)/BWA,right=(rc1.right)/BWA,bottom=(rc1.bottom)/BWA;

	for(int i=left;i<=right;i++)
	for(int j=top;j<=bottom;j++)
	{
		if(g_iChessmanMap[i+1][j+1]!=32)
			DrawIcon(hdc,i*BWA+SW,j*BWA+SW,g_hIconChessman[ManToIcon[g_iChessmanMap[i+1][j+1]]]);
		if(g_pointBoxFrom.x==i+1&&g_pointBoxFrom.y==j+1||g_pointBoxTo.x==i+1&&g_pointBoxTo.y==j+1)
			DrawIcon(hdc,i*BWA+SW,j*BWA+SW,g_hIconBox);
		if(g_iChessmanSelect!=32)
		{
			if(g_pointChessman[g_iChessmanSelect].x==i+1&&g_pointChessman[g_iChessmanSelect].y==j+1)
				DrawIcon(hdc,i*BWA+SW,j*BWA+SW,g_hIconSelect);			
		}
	}
}

/******************************************************************
ShowPoint:		重画point棋位

参数:
hdc:			设备描述表句柄
point:			棋位坐标

返回值:			无
******************************************************************/
void ShowPoint(HDC hdc,POINT point)
{
	RECT rect;
	rect.left=(point.x-1)*BWA;
	rect.top=(point.y-1)*BWA;
	rect.right=rect.left+BWA;
	rect.bottom=rect.top+BWA;
	ShowRect(hdc,&rect);
}

/******************************************************************
Think:			求最佳走法并根据结果走棋

参数:
hWnd:			窗口句柄

返回值:			无
******************************************************************/
void Think(HWND hWnd)
{
	int i,j;
	POINT tmanposition[32];
	int  tside;
	int tmap[11][12];
	for(i=0;i<32;i++)
	{
		tmanposition[i]=g_pointChessman[i];
	}
	tside=g_iSide;
	for(i=0;i<11;i++)
	for(j=0;j<12;j++)
	{
		tmap[i][j]=g_iChessmanMap[i][j];
	}
	int resultman=32;
	POINT resultpoint={0,0};
	BOOL flag=Think(tmap,tmanposition,tside,resultman,resultpoint);
	if(flag)
	{
		Go(hWnd,resultman,resultpoint);
	}
	else
	{
		g_bEndGame=TRUE;
		MessageBox(hWnd,"游戏结束","MantisChess",MB_OK);
	}

}

/******************************************************************
Go:				走棋

参数:
hWnd:			窗口句柄
man:			所移动的棋子
targetpoint:	目标位置

返回值:			成功返回TRUE,否则返回FALSE
******************************************************************/
BOOL Go(HWND hWnd,int man, POINT targetpoint)
{
	HDC hdc =GetDC(hWnd);
	if(g_bEndGame)return FALSE;
	if(!CanGo(g_iChessmanMap,man,g_pointChessman[man],targetpoint))return FALSE;
	if(g_iChessmanMap[targetpoint.x][targetpoint.y]!=32)
	{
		g_pointChessman[g_iChessmanMap[targetpoint.x][targetpoint.y]].x=0;
	}

	POINT from,to;

	from=g_pointBoxFrom;
	to=g_pointBoxTo;
	g_pointBoxFrom=g_pointChessman[man];
	g_pointBoxTo=targetpoint;

	POINT oldselect;
	oldselect=g_pointChessman[man];
	g_pointChessman[man]=targetpoint;
	FixManMap(g_iChessmanMap,g_pointChessman,g_iSide);

	g_iChessmanSelect=32;
	g_iSide=!g_iSide;

	ShowPoint(hdc,oldselect);
	ShowPoint(hdc,targetpoint);
	ShowPoint(hdc,to);
	ShowPoint(hdc,from);
	if(g_pointChessman[FistOfSide[g_iSide]].x==0)
	{
		::MessageBox(hWnd,"游戏结束","MantisChess",MB_OK);
		g_bEndGame=TRUE;
		return TRUE;
	}
	if(g_iComputerSide==g_iSide&&!g_bEndGame)
	{
		SetCursor(g_hCurThinking);
		Think(hWnd);
		SetCursor(g_hCurCantGo);
	}
	return TRUE;
}

/******************************************************************
FaceToPoint:	把鼠标位置转换成对应的棋盘坐标

参数:
point:			鼠标位置

返回值:			鼠标位置在棋盘内返回TRUE,否则返回FALSE
******************************************************************/
BOOL FaceToPoint(POINT &point)
{
	if((point.x)%BWA<SW||(point.x)%BWA>BWA-SW)return FALSE;
	if((point.y)%BWA<SW||(point.y)%BWA>BWA-SW)return FALSE;
	POINT p;
	p.x=(point.x)/BWA+1;
	p.y=(point.y)/BWA+1;
	if(p.x<1||p.x>9||p.y<1||p.y>10)return FALSE;
	point=p;
	return TRUE;
}

/******************************************************************
OnLButtonDown:	WM_LBUTTONDOWN 消息响应函数

参数:
hWnd:			窗口句柄
point:			鼠标坐标

返回值:			无
******************************************************************/
void OnLButtonDown(HWND hWnd,POINT point) 
{
	if(g_bEndGame)
	{
		MessageBox(hWnd,"游戏已经结束","MantisChess",MB_OK);
	}
	else if(FaceToPoint(point))
	{
		if(SideOfMan[g_iChessmanMap[point.x][point.y]]==!g_iComputerSide)
		{
			HDC hdc=GetDC(hWnd);
			POINT p;
			BOOL flag=FALSE;
			if(g_iChessmanSelect!=32)
			{
				p=g_pointChessman[g_iChessmanSelect];
				flag=TRUE;
			}
			g_iChessmanSelect=g_iChessmanMap[point.x][point.y];
			ShowPoint(hdc,g_pointChessman[g_iChessmanSelect]);
			if(flag)
				ShowPoint(hdc,p);
		}
		else if(g_iChessmanSelect!=32)
		{
			Go(hWnd,g_iChessmanSelect,point);
		}
	}
}

/******************************************************************
OnMouseMove:	WM_MOUSEMOVE 消息响应函数

参数:
point:			鼠标坐标

返回值:			无
******************************************************************/
void OnMouseMove(POINT point) 
{
	if(FaceToPoint(point))
	{
		if(g_iChessmanSelect!=32)
		{
			if(CanGo(g_iChessmanMap,g_iChessmanSelect,g_pointChessman[g_iChessmanSelect],point))
			{
				SetCursor(g_hCurHand);
			}
			else
			{
				SetCursor(g_hCurCantGo);
			}
		}
		else
		{
			if(SideOfMan[g_iChessmanMap[point.x][point.y]]==!g_iComputerSide)
			{
				SetCursor(g_hCurHand);
			}
			else
			{
				SetCursor(g_hCurCantGo);
			}
		}
	}
}

⌨️ 快捷键说明

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