📄 mantischess.cpp
字号:
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;
g_MoveHistory.count=0;
for(i=0;i<11;i++)
for(j=0;j<12;j++)
{
g_bCanMove[i][j]=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_bCanMove[i+1][j+1])
{
if(g_iChessmanMap[i+1][j+1]==32)
DrawIcon(hdc,i*BWA+SW,j*BWA+SW,g_hIconCanMove);
else
DrawIcon(hdc,i*BWA+SW,j*BWA+SW,g_hIconAttack);
}
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,"Game over!","MantisChess",MB_OK);
}
}
/******************************************************************
Go: 走棋
参数:
hWnd: 窗口句柄
man: 所移动的棋子
targetpoint: 目标位置
返回值: 成功返回TRUE,否则返回FALSE
******************************************************************/
BOOL Go(HWND hWnd,int man, POINT targetpoint)
{
int i,j;
HDC hdc =GetDC(hWnd);
if(g_bEndGame)return FALSE;
if(g_MoveHistory.count>=MAXMOVE)g_MoveHistory.count=0;
if(!CanGo(g_iChessmanMap,man,g_pointChessman[man],targetpoint))return FALSE;
g_MoveHistory.betaken[g_MoveHistory.count]=g_iChessmanMap[targetpoint.x][targetpoint.y];
g_MoveHistory.man[g_MoveHistory.count]=man;
g_MoveHistory.from[g_MoveHistory.count]=g_pointChessman[man];
g_MoveHistory.to[g_MoveHistory.count]=targetpoint;
g_MoveHistory.count++;
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;
for(i=0;i<11;i++)
for(j=0;j<12;j++)
{
POINT pt;
pt.x=i;
pt.y=j;
if(CanGo(g_iChessmanMap,man,g_pointChessman[man],pt))
{
if(!g_bCanMove[i][j])
{
g_bCanMove[i][j]=TRUE;
ShowPoint(hdc,pt);
}
}
else
{
if(g_bCanMove[i][j])
{
g_bCanMove[i][j]=FALSE;
ShowPoint(hdc,pt);
}
}
}
ShowPoint(hdc,oldselect);
ShowPoint(hdc,targetpoint);
ShowPoint(hdc,to);
ShowPoint(hdc,from);
if(g_pointChessman[FistOfSide[g_iSide]].x==0)
{
::MessageBox(hWnd,"Game over!","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)
{
int i,j;
if(g_bEndGame)
{
MessageBox(hWnd,"Game over!","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];
for(i=0;i<11;i++)
for(j=0;j<12;j++)
{
POINT pt;
pt.x=i;
pt.y=j;
if(CanGo(g_iChessmanMap,g_iChessmanSelect,g_pointChessman[g_iChessmanSelect],pt))
{
if(!g_bCanMove[i][j])
{
g_bCanMove[i][j]=TRUE;
ShowPoint(hdc,pt);
}
}
else
{
if(g_bCanMove[i][j])
{
g_bCanMove[i][j]=FALSE;
ShowPoint(hdc,pt);
}
}
}
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);
}
}
}
}
/******************************************************************
OnBack: Back菜单响应函数,悔棋
参数:
hWnd: 窗口句柄
返回值: 无
******************************************************************/
void OnBack(HWND hWnd)
{
int i,j;
if(g_MoveHistory.count<2) return;
for( i=g_MoveHistory.count-1;i>=g_MoveHistory.count-2;i--)
{
g_pointChessman[g_MoveHistory.man[i]]=g_MoveHistory.from[i];
g_iChessmanSelect=g_MoveHistory.man[i];
g_iChessmanMap[g_MoveHistory.from[i].x][g_MoveHistory.from[i].y]=g_MoveHistory.man[i];
g_pointBoxFrom=g_MoveHistory.from[i];
if(g_MoveHistory.betaken[i]!=32)
{
g_pointChessman[g_MoveHistory.betaken[i]]=g_MoveHistory.to[i];
}
g_iChessmanMap[g_MoveHistory.to[i].x][g_MoveHistory.to[i].y]=g_MoveHistory.betaken[i];
g_pointBoxTo=g_MoveHistory.to[i];
}
g_MoveHistory.count-=2;
g_bEndGame=FALSE;
for(i=0;i<11;i++)
for(j=0;j<12;j++)
{
g_bCanMove[i][j]=FALSE;
}
InvalidateRect(hWnd,NULL,TRUE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -