📄 mantischess.cpp
字号:
/***************************************************************
MantisChess.cpp : 程序入口
版权所有(C) 陈成涛
这一程序是自由软件,你可以遵照自由软件基金会出版的GNU通用公共
许可证条款来修改和重新发布这一程序。或者用许可证的第二版,或者
(根据你的选择)用任何更新的版本。
发布这一程序的目的是希望它有用,但没有任何担保。甚至没有适合特
定目的的隐含的担保。更详细的情况请参阅GNU通用公共许可证。
你应该已经和程序一起收到一份GNU通用公共许可证的副本。
如果还没有,写信给:
The Free Software Foundation,Inc,,675 Mass Ave, Cambridge,
MAO2139,USA
如果你在使用本软件时有什么问题或建议,用以下地址可以与我取得联
系:
http://thecct.51.net
或发Email到:
stove@eyou.com
thecct@163.com
------------------------------------------------------------------
MantisChess.cpp : program entrance
Copyright (C) Chen Chengtao, China
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
If you have any question about this software please visit my hompage:
http://thecct.51.net
or E_mail to:
stove@eyou.com
thecct@163.com
******************************************************************/
#include "StdAfx.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);
void OnBack(HWND hWnd);
//--------------------------------------
static POINT g_pointChessman[32]; //棋子坐标
static int g_iChessmanMap[11][12]; //棋位状态
static int g_iSide; //轮到哪放走
static HCURSOR g_hCurCantGo; //不可以点击时显示的鼠标
static HCURSOR g_hCurHand; //可以点击时显示的鼠标
static HCURSOR g_hCurThinking; //计算时显示的鼠标
static HICON g_hIconChessman[14]; //棋子的图像
static HICON g_hIconBox; //指示最后一步走法的框
static HICON g_hIconSelect; //指示选择棋子的框
static HICON g_hIconCanMove; //指示可到达的位置
static HICON g_hIconAttack; //指示被攻击的棋子
static HDC g_hdcChessboard; //棋盘的设备描述表
static HBITMAP g_hbmpChessboard; //棋盘的位图
static POINT g_pointBoxFrom; //最后一步的原始位置
static POINT g_pointBoxTo; //最后一步的目标位置
static int g_iChessmanSelect; //选择的棋子
static int g_iComputerSide; //电脑的颜色
static BOOL g_bEndGame; //游戏结束标志
static BOOL g_bCanMove[11][12]; //
static struct MOVEHISTORY g_MoveHistory;
//--------------------------------------
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();
g_iComputerSide=RED;
rect.left=0;
rect.top=0;
rect.right=XBW;
rect.bottom=YBW;
hdc=GetDC(hWnd);
ShowRect(hdc,&rect);
SetCursor(g_hCurThinking);
Think(hWnd);
SetCursor(g_hCurCantGo);
break;
case IDM_NEW_RED:
Reset();
g_iComputerSide=BLACK;
rect.left=0;
rect.top=0;
rect.right=XBW;
rect.bottom=YBW;
hdc=GetDC(hWnd);
ShowRect(hdc,&rect);
break;
case IDM_BACK:
OnBack(hWnd);
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(g_hbmpChessboard);
DeleteDC(g_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;
}
/******************************************************************
OnCreate: WM_CREATE 消息响应函数,建立窗口时进行初始化
参数:
hWnd: 窗口句柄
返回值: 无
******************************************************************/
void OnCreate(HWND hWnd)
{
g_hCurHand=LoadCursor(hInst,MAKEINTRESOURCE(IDC_HAND));
g_hCurThinking=LoadCursor(hInst,MAKEINTRESOURCE(IDC_THINKING));
g_hCurCantGo=LoadCursor(hInst,MAKEINTRESOURCE(IDC_CANTGO));
g_hIconChessman[RED_K]=LoadIcon(hInst,MAKEINTRESOURCE(IDI_R_K));
g_hIconChessman[RED_S]=LoadIcon(hInst,MAKEINTRESOURCE(IDI_R_S));
g_hIconChessman[RED_X]=LoadIcon(hInst,MAKEINTRESOURCE(IDI_R_X));
g_hIconChessman[RED_M]=LoadIcon(hInst,MAKEINTRESOURCE(IDI_R_M));
g_hIconChessman[RED_J]=LoadIcon(hInst,MAKEINTRESOURCE(IDI_R_J));
g_hIconChessman[RED_P]=LoadIcon(hInst,MAKEINTRESOURCE(IDI_R_P));
g_hIconChessman[RED_B]=LoadIcon(hInst,MAKEINTRESOURCE(IDI_R_B));
g_hIconChessman[BLACK_K]=LoadIcon(hInst,MAKEINTRESOURCE(IDI_B_K));
g_hIconChessman[BLACK_S]=LoadIcon(hInst,MAKEINTRESOURCE(IDI_B_S));
g_hIconChessman[BLACK_X]=LoadIcon(hInst,MAKEINTRESOURCE(IDI_B_X));
g_hIconChessman[BLACK_M]=LoadIcon(hInst,MAKEINTRESOURCE(IDI_B_M));
g_hIconChessman[BLACK_J]=LoadIcon(hInst,MAKEINTRESOURCE(IDI_B_J));
g_hIconChessman[BLACK_P]=LoadIcon(hInst,MAKEINTRESOURCE(IDI_B_P));
g_hIconChessman[BLACK_B]=LoadIcon(hInst,MAKEINTRESOURCE(IDI_B_B));
g_hIconBox=LoadIcon(hInst,MAKEINTRESOURCE(IDI_BOX));
g_hIconSelect=LoadIcon(hInst,MAKEINTRESOURCE(IDI_SELECT));
g_hIconCanMove=LoadIcon(hInst,MAKEINTRESOURCE(IDI_CANMOVE));
g_hIconAttack=LoadIcon(hInst,MAKEINTRESOURCE(IDI_ATTACK));
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()
{
int i,j;
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; //车
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -