📄 mantis.cpp
字号:
// Mantis.cpp : Defines the entry point for the application.
//
#include "Windows.h"
#include "resource.h"
#include "MantisChessDef.h"
#include "MantisChessDraw.h"
#include "MantisChessStd.h"
#include "MantisChessThink.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
//----------------------------
void OnCreate(HWND hWnd);
void Reset();
void ShowRect(HDC hdc,LPRECT prect);
void ShowPoint(HDC hdc,POINT point);
void Think(HWND hWnd);
BOOL Go(HWND hWnd,int man,POINT targetpoint);
void OnMouseMove(POINT point);
void OnLButtonDown(HWND hWnd,POINT point);
BOOL FaceToPoint(POINT &point);
//--------------------------------------
static POINT m_pointChessman[32];
static int m_iChessmanMap[11][12];
static int m_iSide;
static HCURSOR m_hCurCantGo;
static HCURSOR m_hCurHand;
static HCURSOR m_hCurThinking;
static HICON m_hIconChessman[14];
static HICON m_hIconBox;
static HICON m_hIconSelect;
static HDC m_hdcChessboard;
static HBITMAP m_hbmpChessboard;
static POINT m_pointBoxFrom;
static POINT m_pointBoxTo;
static int m_iChessmanSelect;
static int m_iComputerSide;
static BOOL m_bEndGame;
//--------------------------------------
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_MANTIS, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_MANTIS);
wcex.hCursor = NULL;
wcex.hbrBackground = NULL;
wcex.lpszMenuName = (LPCSTR)IDC_MANTIS;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU,,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
RECT rect;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
POINT point;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case IDM_NEW_BLACK:
Reset();
m_iComputerSide=RED;
rect.left=0;
rect.top=0;
rect.right=XBW;
rect.bottom=YBW;
hdc=GetDC(hWnd);
ShowRect(hdc,&rect);
SetCursor(m_hCurThinking);
Think(hWnd);
SetCursor(m_hCurCantGo);
break;
case IDM_NEW_RED:
Reset();
m_iComputerSide=BLACK;
rect.left=0;
rect.top=0;
rect.right=XBW;
rect.bottom=YBW;
hdc=GetDC(hWnd);
ShowRect(hdc,&rect);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rect;
GetClientRect(hWnd, &rect);
ShowRect(hdc,&rect);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
DeleteObject(m_hbmpChessboard);
DeleteDC(m_hdcChessboard);
PostQuitMessage(0);
break;
case WM_CREATE:
OnCreate(hWnd);
break;
case WM_LBUTTONDOWN:
point.x=LOWORD(lParam);
point.y=HIWORD(lParam);
OnLButtonDown(hWnd,point);
break;
case WM_MOUSEMOVE:
point.x=LOWORD(lParam);
point.y=HIWORD(lParam);
OnMouseMove(point);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
void OnCreate(HWND hWnd)
{
m_hCurHand=LoadCursor(hInst,MAKEINTRESOURCE(IDC_HAND));
m_hCurThinking=LoadCursor(hInst,MAKEINTRESOURCE(IDC_THINKING));
m_hCurCantGo=LoadCursor(hInst,MAKEINTRESOURCE(IDC_CANTGO));
m_hIconChessman[RED_K]=LoadIcon(hInst,MAKEINTRESOURCE(IDI_R_K));
m_hIconChessman[RED_S]=LoadIcon(hInst,MAKEINTRESOURCE(IDI_R_S));
m_hIconChessman[RED_X]=LoadIcon(hInst,MAKEINTRESOURCE(IDI_R_X));
m_hIconChessman[RED_M]=LoadIcon(hInst,MAKEINTRESOURCE(IDI_R_M));
m_hIconChessman[RED_J]=LoadIcon(hInst,MAKEINTRESOURCE(IDI_R_J));
m_hIconChessman[RED_P]=LoadIcon(hInst,MAKEINTRESOURCE(IDI_R_P));
m_hIconChessman[RED_B]=LoadIcon(hInst,MAKEINTRESOURCE(IDI_R_B));
m_hIconChessman[BLACK_K]=LoadIcon(hInst,MAKEINTRESOURCE(IDI_B_K));
m_hIconChessman[BLACK_S]=LoadIcon(hInst,MAKEINTRESOURCE(IDI_B_S));
m_hIconChessman[BLACK_X]=LoadIcon(hInst,MAKEINTRESOURCE(IDI_B_X));
m_hIconChessman[BLACK_M]=LoadIcon(hInst,MAKEINTRESOURCE(IDI_B_M));
m_hIconChessman[BLACK_J]=LoadIcon(hInst,MAKEINTRESOURCE(IDI_B_J));
m_hIconChessman[BLACK_P]=LoadIcon(hInst,MAKEINTRESOURCE(IDI_B_P));
m_hIconChessman[BLACK_B]=LoadIcon(hInst,MAKEINTRESOURCE(IDI_B_B));
m_hIconBox=LoadIcon(hInst,MAKEINTRESOURCE(IDI_BOX));
m_hIconSelect=LoadIcon(hInst,MAKEINTRESOURCE(IDI_SELECT));
Reset();
HDC hdc=GetDC(hWnd);
m_hdcChessboard=CreateCompatibleDC(hdc);
m_hbmpChessboard=CreateCompatibleBitmap(hdc,XBW,YBW);
SelectObject(m_hdcChessboard,m_hbmpChessboard);
MakeBoard(m_hdcChessboard,RGB(180,210,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);
}
void Reset()
{
m_pointChessman[0].x=5;m_pointChessman[0].y=10; //帅
m_pointChessman[1].x=4;m_pointChessman[1].y=10; //士
m_pointChessman[2].x=6;m_pointChessman[2].y=10;
m_pointChessman[3].x=3;m_pointChessman[3].y=10; //相
m_pointChessman[4].x=7;m_pointChessman[4].y=10;
m_pointChessman[5].x=2;m_pointChessman[5].y=10; //马
m_pointChessman[6].x=8;m_pointChessman[6].y=10;
m_pointChessman[7].x=1;m_pointChessman[7].y=10; //车
m_pointChessman[8].x=9;m_pointChessman[8].y=10;
m_pointChessman[9].x=2;m_pointChessman[9].y=8; //炮
m_pointChessman[10].x=8;m_pointChessman[10].y=8;
m_pointChessman[11].x=1;m_pointChessman[11].y=7;//兵
m_pointChessman[12].x=3;m_pointChessman[12].y=7;
m_pointChessman[13].x=5;m_pointChessman[13].y=7;
m_pointChessman[14].x=7;m_pointChessman[14].y=7;
m_pointChessman[15].x=9;m_pointChessman[15].y=7;
m_pointChessman[16].x=5;m_pointChessman[16].y=1;//将
m_pointChessman[17].x=4;m_pointChessman[17].y=1;//士
m_pointChessman[18].x=6;m_pointChessman[18].y=1;
m_pointChessman[19].x=3;m_pointChessman[19].y=1;//相
m_pointChessman[20].x=7;m_pointChessman[20].y=1;
m_pointChessman[21].x=2;m_pointChessman[21].y=1;//马
m_pointChessman[22].x=8;m_pointChessman[22].y=1;
m_pointChessman[23].x=1;m_pointChessman[23].y=1;//车
m_pointChessman[24].x=9;m_pointChessman[24].y=1;
m_pointChessman[25].x=2;m_pointChessman[25].y=3;//炮
m_pointChessman[26].x=8;m_pointChessman[26].y=3;
m_pointChessman[27].x=1;m_pointChessman[27].y=4;//卒
m_pointChessman[28].x=3;m_pointChessman[28].y=4;
m_pointChessman[29].x=5;m_pointChessman[29].y=4;
m_pointChessman[30].x=7;m_pointChessman[30].y=4;
m_pointChessman[31].x=9;m_pointChessman[31].y=4;
m_iSide=RED;
FixManMap(m_iChessmanMap,m_pointChessman,m_iSide);
m_pointBoxFrom.x=0;
m_pointBoxFrom.y=0;
m_pointBoxTo.x=0;
m_pointBoxTo.y=0;
m_iChessmanSelect=32;
m_iComputerSide=1;
m_bEndGame=FALSE;
}
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,m_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(m_iChessmanMap[i+1][j+1]!=32)
DrawIcon(hdc,i*BWA+SW,j*BWA+SW,m_hIconChessman[ManToIcon[m_iChessmanMap[i+1][j+1]]]);
if(m_pointBoxFrom.x==i+1&&m_pointBoxFrom.y==j+1||m_pointBoxTo.x==i+1&&m_pointBoxTo.y==j+1)
DrawIcon(hdc,i*BWA+SW,j*BWA+SW,m_hIconBox);
if(m_iChessmanSelect!=32)
{
if(m_pointChessman[m_iChessmanSelect].x==i+1&&m_pointChessman[m_iChessmanSelect].y==j+1)
DrawIcon(hdc,i*BWA+SW,j*BWA+SW,m_hIconSelect);
}
}
}
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);
}
void Think(HWND hWnd)
{
int i,j;
POINT tmanposition[32];
int tside;
int tmap[11][12];
for(i=0;i<32;i++)
{
tmanposition[i]=m_pointChessman[i];
}
tside=m_iSide;
for(i=0;i<11;i++)
for(j=0;j<12;j++)
{
tmap[i][j]=m_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
{
m_bEndGame=TRUE;
MessageBox(hWnd,"游戏结束","MantisChess",MB_OK);
}
}
BOOL Go(HWND hWnd,int man, POINT targetpoint)
{
HDC hdc =GetDC(hWnd);
if(m_bEndGame)return FALSE;
if(!CanGo(m_iChessmanMap,man,m_pointChessman[man],targetpoint))return FALSE;
if(m_iChessmanMap[targetpoint.x][targetpoint.y]!=32)
{
m_pointChessman[m_iChessmanMap[targetpoint.x][targetpoint.y]].x=0;
}
POINT from,to;
from=m_pointBoxFrom;
to=m_pointBoxTo;
m_pointBoxFrom=m_pointChessman[man];
m_pointBoxTo=targetpoint;
POINT oldselect;
oldselect=m_pointChessman[man];
m_pointChessman[man]=targetpoint;
FixManMap(m_iChessmanMap,m_pointChessman,m_iSide);
m_iChessmanSelect=32;
m_iSide=!m_iSide;
ShowPoint(hdc,oldselect);
ShowPoint(hdc,targetpoint);
ShowPoint(hdc,to);
ShowPoint(hdc,from);
if(m_pointChessman[FistOfSide[m_iSide]].x==0)
{
::MessageBox(hWnd,"游戏结束","MantisChess",MB_OK);
m_bEndGame=TRUE;
return TRUE;
}
if(m_iComputerSide==m_iSide&&!m_bEndGame)
{
SetCursor(m_hCurThinking);
Think(hWnd);
SetCursor(m_hCurCantGo);
}
return TRUE;
}
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;
}
void OnLButtonDown(HWND hWnd,POINT point)
{
if(m_bEndGame)
{
MessageBox(hWnd,"游戏已经结束","MantisChess",MB_OK);
}
else if(FaceToPoint(point))
{
if(SideOfMan[m_iChessmanMap[point.x][point.y]]==!m_iComputerSide)
{
HDC hdc=GetDC(hWnd);
POINT p;
BOOL flag=FALSE;
if(m_iChessmanSelect!=32)
{
p=m_pointChessman[m_iChessmanSelect];
flag=TRUE;
}
m_iChessmanSelect=m_iChessmanMap[point.x][point.y];
ShowPoint(hdc,m_pointChessman[m_iChessmanSelect]);
if(flag)
ShowPoint(hdc,p);
}
else if(m_iChessmanSelect!=32)
{
Go(hWnd,m_iChessmanSelect,point);
}
}
}
void OnMouseMove(POINT point)
{
if(FaceToPoint(point))
{
if(m_iChessmanSelect!=32)
{
if(CanGo(m_iChessmanMap,m_iChessmanSelect,m_pointChessman[m_iChessmanSelect],point))
{
SetCursor(m_hCurHand);
}
else
{
SetCursor(m_hCurCantGo);
}
}
else
{
if(SideOfMan[m_iChessmanMap[point.x][point.y]]==!m_iComputerSide)
{
SetCursor(m_hCurHand);
}
else
{
SetCursor(m_hCurCantGo);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -