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

📄 mantischess.cpp

📁 学习VC++游戏编程的 好程序 象棋代码 算法比较复杂
💻 CPP
📖 第 1 页 / 共 2 页
字号:
  /***************************************************************
  MantisChess.cpp : MantisChess 程序入口

  版权所有(C) 共创软件联盟 CChessUG 项目开发小组成员 陈成涛 

  这一程序是自由软件,你可以遵照自由软件基金会出版的GNU通用公共
  许可证条款来修改和重新发布这一程序。或者用许可证的第二版,或者
  (根据你的选择)用任何更新的版本。

  发布这一程序的目的是希望它有用,但没有任何担保。甚至没有适合特
  定目的的隐含的担保。更详细的情况请参阅GNU通用公共许可证。
  
  你应该已经和程序一起收到一份GNU通用公共许可证的副本。
  如果还没有,写信给:

  The Free Software Foundation,Inc,,675 Mass Ave, Cambridge,
  MAO2139,USA

  如果你在使用本软件时有什么问题或建议,用以下地址可以与我取得联
  系:

		http://thecct.51.net
		http://cosoft.org.com

  或发Email到:

		stove@eyou.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);

//--------------------------------------
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 	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;			//游戏结束标志
//--------------------------------------

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;
				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));

⌨️ 快捷键说明

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