C++ Easyx EGE 中国象棋

欸嘿,我C++小盆友又来啦!今天给大家带来中国象棋的图形化界面代码,老样子,音效我会上传,代码自取,求点赞评论关注转发 ^_^

代码共780行,耗时2天,效果相交大佬可能并不怎么好,不过看还是看得过去的……

效果长这样:

#include<stdio.h>
#include<math.h>
#include<string>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<stdlib.h>
#include<graphics.h>
#include<conio.h>
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")
#define BOARD_SIZE		50
#define BOARD_COL		10
#define BOARD_ROW		9
#define CHESSPEICE_SIZE	18
typedef int ll;
using namespace std;
enum Player {
	RED_PLAYER,
	BLACK_PLAYER
};
enum ChessPiece {
	PAWN,
	CANNON,
	ROOK,
	HORSE,
	ELEPHANT,
	GUARD,
	GENERAL
};
enum chessInfo {
	ERROR_LOCATION,
	EMPTY_LOCATION,
	RED_CHESS,
	BLACK_CHESS
};
enum selectTime {
	FIRST_TIME,
	SECOND_TIME
};
struct Chess {
	ll row, col;
	Player player;
	ChessPiece chessPiece;
};
vector<Chess> vecChess;
Chess selectedChess = { -1,-1 };
Chess lastSelectedChess = { -1,-1 };
ll newRow, newCol;
ll redGeneralRow = 4, redGeneralCol = 9;
ll blackGeneralRow = 4, blackGeneralCol = 0;
bool slt;
Player player = RED_PLAYER;
void drawChessPiece(Chess chess) {
	ll sz = BOARD_SIZE / 3 + 10;
	if (selectedChess.row == chess.row && selectedChess.col == chess.col) settextstyle(sz + 6, 0, "华文行楷");
	else settextstyle(sz, 0, "华文行楷");
	if (chess.player == RED_PLAYER) {
		setlinecolor(RED);
		settextcolor(RED);
	}
	else {
		setlinecolor(BLACK);
		settextcolor(BLACK);
	}
	if (selectedChess.row == chess.row && selectedChess.col == chess.col) settextcolor(LIGHTCYAN), setlinecolor(LIGHTCYAN);
	setfillcolor(RGB(240, 134, 80));
	fillcircle((chess.row + 1) * BOARD_SIZE, (chess.col + 1) * BOARD_SIZE, CHESSPEICE_SIZE);
	TCHAR endStr[16];
	if (chess.chessPiece == PAWN && chess.player == RED_PLAYER) _stprintf_s(endStr, "兵");
	if (chess.chessPiece == PAWN && chess.player == BLACK_PLAYER) _stprintf_s(endStr, "卒");
	if (chess.chessPiece == CANNON && chess.player == RED_PLAYER) _stprintf_s(endStr, "炮");
	if (chess.chessPiece == CANNON && chess.player == BLACK_PLAYER) _stprintf_s(endStr, "炮");
	if (chess.chessPiece == ROOK && chess.player == RED_PLAYER) _stprintf_s(endStr, "车");
	if (chess.chessPiece == ROOK && chess.player == BLACK_PLAYER) _stprintf_s(endStr, "车");
	if (chess.chessPiece == HORSE && chess.player == RED_PLAYER) _stprintf_s(endStr, "马");
	if (chess.chessPiece == HORSE && chess.player == BLACK_PLAYER) _stprintf_s(endStr, "马");
	if (chess.chessPiece == ELEPHANT && chess.player == RED_PLAYER) _stprintf_s(endStr, "相");
	if (chess.chessPiece == ELEPHANT && chess.player == BLACK_PLAYER) _stprintf_s(endStr, "象");
	if (chess.chessPiece == GUARD && chess.player == RED_PLAYER) _stprintf_s(endStr, "士");
	if (chess.chessPiece == GUARD && chess.player == BLACK_PLAYER) _stprintf_s(endStr, "士");
	if (chess.chessPiece == GENERAL && chess.player == RED_PLAYER) _stprintf_s(endStr, "帅");
	if (chess.chessPiece == GENERAL && chess.player == BLACK_PLAYER) _stprintf_s(endStr, "将");
	setbkmode(TRANSPARENT);
	if (selectedChess.row == chess.row && selectedChess.col == chess.col) outtextxy((chess.row + 0.72) * BOARD_SIZE, (chess.col + 0.72) * BOARD_SIZE, endStr);
	else outtextxy((chess.row + 0.74) * BOARD_SIZE, (chess.col + 0.74) * BOARD_SIZE, endStr);
}
void printChessBoard() {
	clearrectangle(0, 0, (BOARD_ROW + 1) * BOARD_SIZE, (BOARD_COL + 1) * BOARD_SIZE);
	setlinecolor(RED);
	setlinestyle(PS_SOLID, 2);
	setfillcolor(RGB(211, 177, 125));
	fillrectangle(BOARD_SIZE - 6, BOARD_SIZE - 6, BOARD_ROW * BOARD_SIZE + 6, BOARD_COL * BOARD_SIZE + 6);
	for (ll i = 0; i < BOARD_ROW - 1; i++) {
		for (ll j = 0; j < BOARD_COL - 1; j++) {
			if (j == 4) continue;
			fillrectangle((i + 1) * BOARD_SIZE, (j + 1) * BOARD_SIZE, (i + 2) * BOARD_SIZE, (j + 2) * BOARD_SIZE);
		}
	}
	line(BOARD_SIZE, BOARD_SIZE, BOARD_SIZE, BOARD_COL* BOARD_SIZE);
	line(BOARD_ROW*BOARD_SIZE, BOARD_SIZE, BOARD_ROW*BOARD_SIZE, BOARD_COL* BOARD_SIZE);
	line(4 * BOARD_SIZE, BOARD_SIZE, 6 * BOARD_SIZE, 3 * BOARD_SIZE);
	line(6 * BOARD_SIZE, BOARD_SIZE, 4 * BOARD_SIZE, 3 * BOARD_SIZE);
	line(4 * BOARD_SIZE, 8 * BOARD_SIZE, 6 * BOARD_SIZE, 10 * BOARD_SIZE);
	line(6 * BOARD_SIZE, 8 * BOARD_SIZE, 4 * BOARD_SIZE, 10 * BOARD_SIZE);
	//兵点十字
	line(BOARD_SIZE + 5, 7 * BOARD_SIZE - 13, BOARD_SIZE + 5, 7 * BOARD_SIZE - 5);
	line(BOARD_SIZE + 5, 7 * BOARD_SIZE - 5, BOARD_SIZE + 13, 7 * BOARD_SIZE - 5);
	line(3 * BOARD_SIZE + 5, 7 * BOARD_SIZE - 13, 3 * BOARD_SIZE + 5, 7 * BOARD_SIZE - 5);
	line(3 * BOARD_SIZE + 5, 7 * BOARD_SIZE - 5, 3 * BOARD_SIZE + 13, 7 * BOARD_SIZE - 5);
	line(5 * BOARD_SIZE + 5, 7 * BOARD_SIZE - 13, 5 * BOARD_SIZE + 5, 7 * BOARD_SIZE - 5);
	line(5 * BOARD_SIZE + 5, 7 * BOARD_SIZE - 5, 5 * BOARD_SIZE + 13, 7 * BOARD_SIZE - 5);
	line(7 * BOARD_SIZE + 5, 7 * BOARD_SIZE - 13, 7 * BOARD_SIZE + 5, 7 * BOARD_SIZE - 5);
	line(7 * BOARD_SIZE + 5, 7 * BOARD_SIZE - 5, 7 * BOARD_SIZE + 13, 7 * BOARD_SIZE - 5);
	line(BOARD_SIZE + 5, 4 * BOARD_SIZE - 13, BOARD_SIZE + 5, 4 * BOARD_SIZE - 5);
	line(BOARD_SIZE + 5, 4 * BOARD_SIZE - 5, BOARD_SIZE + 13, 4 * BOARD_SIZE - 5);
	line(3 * BOARD_SIZE + 5, 4 * BOARD_SIZE - 13, 3 * BOARD_SIZE + 5, 4 * BOARD_SIZE - 5);
	line(3 * BOARD_SIZE + 5, 4 * BOARD_SIZE - 5, 3 * BOARD_SIZE + 13, 4 * BOARD_SIZE - 5);
	line(5 * BOARD_SIZE + 5, 4 * BOARD_SIZE - 13, 5 * BOARD_SIZE + 5, 4 * BOARD_SIZE - 5);
	line(5 * BOARD_SIZE + 5, 4 * BOARD_SIZE - 5, 5 * BOARD_SIZE + 13, 4 * BOARD_SIZE - 5);
	line(7 * BOARD_SIZE + 5, 4 * BOARD_SIZE - 13, 7 * BOARD_SIZE + 5, 4 * BOARD_SIZE - 5);
	line(7 * BOARD_SIZE + 5, 4 * BOARD_SIZE - 5, 7 * BOARD_SIZE + 13, 4 * BOARD_SIZE - 5);
	line(3 * BOARD_SIZE - 5, 7 * BOARD_SIZE - 13, 3 * BOARD_SIZE - 5, 7 * BOARD_SIZE - 5);
	line(3 * BOARD_SIZE - 5, 7 * BOARD_SIZE - 5, 3 * BOARD_SIZE - 13, 7 * BOARD_SIZE - 5);
	line(5 * BOARD_SIZE - 5, 7 * BOARD_SIZE - 13, 5 * BOARD_SIZE - 5, 7 * BOARD_SIZE - 5);
	line(5 * BOARD_SIZE - 5, 7 * BOARD_SIZE - 5, 5 * BOARD_SIZE - 13, 7 * BOARD_SIZE - 5);
	line(7 * BOARD_SIZE - 5, 7 * BOARD_SIZE - 13, 7 * BOARD_SIZE - 5, 7 * BOARD_SIZE - 5);
	line(7 * BOARD_SIZE - 5, 7 * BOARD_SIZE - 5, 7 * BOARD_SIZE - 13, 7 * BOARD_SIZE - 5);
	line(9 * BOARD_SIZE - 5, 7 * BOARD_SIZE - 13, 9 * BOARD_SIZE - 5, 7 * BOARD_SIZE - 5);
	line(9 * BOARD_SIZE - 5, 7 * BOARD_SIZE - 5, 9 * BOARD_SIZE - 13, 7 * BOARD_SIZE - 5);
	line(3 * BOARD_SIZE - 5, 4 * BOARD_SIZE - 13, 3 * BOARD_SIZE - 5, 4 * BOARD_SIZE - 5);
	line(3 * BOARD_SIZE - 5, 4 * BOARD_SIZE - 5, 3 * BOARD_SIZE - 13, 4 * BOARD_SIZE - 5);
	line(5 * BOARD_SIZE - 5, 4 * BOARD_SIZE - 13, 5 * BOARD_SIZE - 5, 4 * BOARD_SIZE - 5);
	line(5 * BOARD_SIZE - 5, 4 * BOARD_SIZE - 5, 5 * BOARD_SIZE - 13, 4 * BOARD_SIZE - 5);
	line(7 * BOARD_SIZE - 5, 4 * BOARD_SIZE - 13, 7 * BOARD_SIZE - 5, 4 * BOARD_SIZE - 5);
	line(7 * BOARD_SIZE - 5, 4 * BOARD_SIZE - 5, 7 * BOARD_SIZE - 13, 4 * BOARD_SIZE - 5);
	line(9 * BOARD_SIZE - 5, 4 * BOARD_SIZE - 13, 9 * BOARD_SIZE - 5, 4 * BOARD_SIZE - 5);
	line(9 * BOARD_SIZE - 5, 4 * BOARD_SIZE - 5, 9 * BOARD_SIZE - 13, 4 * BOARD_SIZE - 5);
	line(BOARD_SIZE + 5, 7 * BOARD_SIZE + 13, BOARD_SIZE + 5, 7 * BOARD_SIZE + 5);
	line(BOARD_SIZE + 5, 7 * BOARD_SIZE + 5, BOARD_SIZE + 13, 7 * BOARD_SIZE + 5);
	line(3 * BOARD_SIZE + 5, 7 * BOARD_SIZE + 13, 3 * BOARD_SIZE + 5, 7 * BOARD_SIZE + 5);
	line(3 * BOARD_SIZE + 5, 7 * BOARD_SIZE + 5, 3 * BOARD_SIZE + 13, 7 * BOARD_SIZE + 5);
	line(5 * BOARD_SIZE + 5, 7 * BOARD_SIZE + 13, 5 * BOARD_SIZE + 5, 7 * BOARD_SIZE + 5);
	line(5 * BOARD_SIZE + 5, 7 * BOARD_SIZE + 5, 5 * BOARD_SIZE + 13, 7 * BOARD_SIZE + 5);
	line(7 * BOARD_SIZE + 5, 7 * BOARD_SIZE + 13, 7 * BOARD_SIZE + 5, 7 * BOARD_SIZE + 5);
	line(7 * BOARD_SIZE + 5, 7 * BOARD_SIZE + 5, 7 * BOARD_SIZE + 13, 7 * BOARD_SIZE + 5);
	line(BOARD_SIZE + 5, 4 * BOARD_SIZE + 13, BOARD_SIZE + 5, 4 * BOARD_SIZE + 5);
	line(BOARD_SIZE + 5, 4 * BOARD_SIZE + 5, BOARD_SIZE + 13, 4 * BOARD_SIZE + 5);
	line(3 * BOARD_SIZE + 5, 4 * BOARD_SIZE + 13, 3 * BOARD_SIZE + 5, 4 * BOARD_SIZE + 5);
	line(3 * BOARD_SIZE + 5, 4 * BOARD_SIZE + 5, 3 * BOARD_SIZE + 13, 4 * BOARD_SIZE + 5);
	line(5 * BOARD_SIZE + 5, 4 * BOARD_SIZE + 13, 5 * BOARD_SIZE + 5, 4 * BOARD_SIZE + 5);
	line(5 * BOARD_SIZE + 5, 4 * BOARD_SIZE + 5, 5 * BOARD_SIZE + 13, 4 * BOARD_SIZE + 5);
	line(7 * BOARD_SIZE + 5, 4 * BOARD_SIZE + 13, 7 * BOARD_SIZE + 5, 4 * BOARD_SIZE + 5);
	line(7 * BOARD_SIZE + 5, 4 * BOARD_SIZE + 5, 7 * BOARD_SIZE + 13, 4 * BOARD_SIZE + 5);
	line(3 * BOARD_SIZE - 5, 7 * BOARD_SIZE + 13, 3 * BOARD_SIZE - 5, 7 * BOARD_SIZE + 5);
	line(3 * BOARD_SIZE - 5, 7 * BOARD_SIZE + 5, 3 * BOARD_SIZE - 13, 7 * BOARD_SIZE + 5);
	line(5 * BOARD_SIZE - 5, 7 * BOARD_SIZE + 13, 5 * BOARD_SIZE - 5, 7 * BOARD_SIZE + 5);
	line(5 * BOARD_SIZE - 5, 7 * BOARD_SIZE + 5, 5 * BOARD_SIZE - 13, 7 * BOARD_SIZE + 5);
	line(7 * BOARD_SIZE - 5, 7 * BOARD_SIZE + 13, 7 * BOARD_SIZE - 5, 7 * BOARD_SIZE + 5);
	line(7 * BOARD_SIZE - 5, 7 * BOARD_SIZE + 5, 7 * BOARD_SIZE - 13, 7 * BOARD_SIZE + 5);
	line(9 * BOARD_SIZE - 5, 7 * BOARD_SIZE + 13, 9 * BOARD_SIZE - 5, 7 * BOARD_SIZE + 5);
	line(9 * BOARD_SIZE - 5, 7 * BOARD_SIZE + 5, 9 * BOARD_SIZE - 13, 7 * BOARD_SIZE + 5);
	line(3 * BOARD_SIZE - 5, 4 * BOARD_SIZE + 13, 3 * BOARD_SIZE - 5, 4 * BOARD_SIZE + 5);
	line(3 * BOARD_SIZE - 5, 4 * BOARD_SIZE + 5, 3 * BOARD_SIZE - 13, 4 * BOARD_SIZE + 5);
	line(5 * BOARD_SIZE - 5, 4 * BOARD_SIZE + 13, 5 * BOARD_SIZE - 5, 4 * BOARD_SIZE + 5);
	line(5 * BOARD_SIZE - 5, 4 * BOARD_SIZE + 5, 5 * BOARD_SIZE - 13, 4 * BOARD_SIZE + 5);
	line(7 * BOARD_SIZE - 5, 4 * BOARD_SIZE + 13, 7 * BOARD_SIZE - 5, 4 * BOARD_SIZE + 5);
	line(7 * BOARD_SIZE - 5, 4 * BOARD_SIZE + 5, 7 * BOARD_SIZE - 13, 4 * BOARD_SIZE + 5);
	line(9 * BOARD_SIZE - 5, 4 * BOARD_SIZE + 13, 9 * BOARD_SIZE - 5, 4 * BOARD_SIZE + 5);
	line(9 * BOARD_SIZE - 5, 4 * BOARD_SIZE + 5, 9 * BOARD_SIZE - 13, 4 * BOARD_SIZE + 5);
	//炮点十字
	line(2 * BOARD_SIZE + 5, 3 * BOARD_SIZE - 13, 2 * BOARD_SIZE + 5, 3 * BOARD_SIZE - 5);
	line(2 * BOARD_SIZE + 5, 3 * BOARD_SIZE - 5, 2 * BOARD_SIZE + 13, 3 * BOARD_SIZE - 5);
	line(8 * BOARD_SIZE + 5, 3 * BOARD_SIZE - 13, 8 * BOARD_SIZE + 5, 3 * BOARD_SIZE - 5);
	line(8 * BOARD_SIZE + 5, 3 * BOARD_SIZE - 5, 8 * BOARD_SIZE + 13, 3 * BOARD_SIZE - 5);
	line(2 * BOARD_SIZE + 5, 8 * BOARD_SIZE - 13, 2 * BOARD_SIZE + 5, 8 * BOARD_SIZE - 5);
	line(2 * BOARD_SIZE + 5, 8 * BOARD_SIZE - 5, 2 * BOARD_SIZE + 13, 8 * BOARD_SIZE - 5);
	line(8 * BOARD_SIZE + 5, 8 * BOARD_SIZE - 13, 8 * BOARD_SIZE + 5, 8 * BOARD_SIZE - 5);
	line(8 * BOARD_SIZE + 5, 8 * BOARD_SIZE - 5, 8 * BOARD_SIZE + 13, 8 * BOARD_SIZE - 5);
	line(2 * BOARD_SIZE - 5, 3 * BOARD_SIZE + 13, 2 * BOARD_SIZE - 5, 3 * BOARD_SIZE + 5);
	line(2 * BOARD_SIZE - 5, 3 * BOARD_SIZE + 5, 2 * BOARD_SIZE - 13, 3 * BOARD_SIZE + 5);
	line(8 * BOARD_SIZE - 5, 3 * BOARD_SIZE + 13, 8 * BOARD_SIZE - 5, 3 * BOARD_SIZE + 5);
	line(8 * BOARD_SIZE - 5, 3 * BOARD_SIZE + 5, 8 * BOARD_SIZE - 13, 3 * BOARD_SIZE + 5);
	line(2 * BOARD_SIZE - 5, 8 * BOARD_SIZE + 13, 2 * BOARD_SIZE - 5, 8 * BOARD_SIZE + 5);
	line(2 * BOARD_SIZE - 5, 8 * BOARD_SIZE + 5, 2 * BOARD_SIZE - 13, 8 * BOARD_SIZE + 5);
	line(8 * BOARD_SIZE - 5, 8 * BOARD_SIZE + 13, 8 * BOARD_SIZE - 5, 8 * BOARD_SIZE + 5);
	line(8 * BOARD_SIZE - 5, 8 * BOARD_SIZE + 5, 8 * BOARD_SIZE - 13, 8 * BOARD_SIZE + 5);
	line(2 * BOARD_SIZE + 5, 3 * BOARD_SIZE + 13, 2 * BOARD_SIZE + 5, 3 * BOARD_SIZE + 5);
	line(2 * BOARD_SIZE + 5, 3 * BOARD_SIZE + 5, 2 * BOARD_SIZE + 13, 3 * BOARD_SIZE + 5);
	line(8 * BOARD_SIZE + 5, 3 * BOARD_SIZE + 13, 8 * BOARD_SIZE + 5, 3 * BOARD_SIZE + 5);
	line(8 * BOARD_SIZE + 5, 3 * BOARD_SIZE + 5, 8 * BOARD_SIZE + 13, 3 * BOARD_SIZE + 5);
	line(2 * BOARD_SIZE + 5, 8 * BOARD_SIZE + 13, 2 * BOARD_SIZE + 5, 8 * BOARD_SIZE + 5);
	line(2 * BOARD_SIZE + 5, 8 * BOARD_SIZE + 5, 2 * BOARD_SIZE + 13, 8 * BOARD_SIZE + 5);
	line(8 * BOARD_SIZE + 5, 8 * BOARD_SIZE + 13, 8 * BOARD_SIZE + 5, 8 * BOARD_SIZE + 5);
	line(8 * BOARD_SIZE + 5, 8 * BOARD_SIZE + 5, 8 * BOARD_SIZE + 13, 8 * BOARD_SIZE + 5);
	line(2 * BOARD_SIZE - 5, 3 * BOARD_SIZE - 13, 2 * BOARD_SIZE - 5, 3 * BOARD_SIZE - 5);
	line(2 * BOARD_SIZE - 5, 3 * BOARD_SIZE - 5, 2 * BOARD_SIZE - 13, 3 * BOARD_SIZE - 5);
	line(8 * BOARD_SIZE - 5, 3 * BOARD_SIZE - 13, 8 * BOARD_SIZE - 5, 3 * BOARD_SIZE - 5);
	line(8 * BOARD_SIZE - 5, 3 * BOARD_SIZE - 5, 8 * BOARD_SIZE - 13, 3 * BOARD_SIZE - 5);
	line(2 * BOARD_SIZE - 5, 8 * BOARD_SIZE - 13, 2 * BOARD_SIZE - 5, 8 * BOARD_SIZE - 5);
	line(2 * BOARD_SIZE - 5, 8 * BOARD_SIZE - 5, 2 * BOARD_SIZE - 13, 8 * BOARD_SIZE - 5);
	line(8 * BOARD_SIZE - 5, 8 * BOARD_SIZE - 13, 8 * BOARD_SIZE - 5, 8 * BOARD_SIZE - 5);
	line(8 * BOARD_SIZE - 5, 8 * BOARD_SIZE - 5, 8 * BOARD_SIZE - 13, 8 * BOARD_SIZE - 5);
	//文字
	settextstyle(35, 0, "华文行楷");
	settextcolor(RED);
	TCHAR chhj[100] = " 楚  河                    汉  界 ";
	outtextxy(((BOARD_ROW + 1) * BOARD_SIZE - textwidth(chhj)) / 2, ((BOARD_COL + 1) * BOARD_SIZE - textheight(chhj)) / 2, chhj);
	for (ll i = 0; i < vecChess.size(); i++) drawChessPiece(vecChess[i]);
	FlushBatchDraw();
}
void ChessFall() {
	mciSendString("open res/音效.wav", 0, 0, 0);
	mciSendString("play res/音效.wav wait", 0, 0, 0);
	mciSendString("close res/音效.wav", 0, 0, 0);
}
void initChessPiece() {
	//兵
	Sleep(500);
	printChessBoard();
	for (ll i = 0; i < 5; i++) {
		Chess pawn_chess;
		pawn_chess.row = 2 * i;
		pawn_chess.col = 3;
		pawn_chess.player = BLACK_PLAYER;
		pawn_chess.chessPiece = PAWN;
		vecChess.push_back(pawn_chess);
	}
	for (ll i = 0; i < 5; i++) {
		Chess pawn_chess;
		pawn_chess.row = 2 * i;
		pawn_chess.col = 6;
		pawn_chess.player = RED_PLAYER;
		pawn_chess.chessPiece = PAWN;
		vecChess.push_back(pawn_chess);
	}
	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ChessFall, NULL, NULL, NULL);
	Sleep(600);
	printChessBoard();
	//炮
	for (ll i = 0; i < 2; i++) {
		Chess cannon_chess;
		cannon_chess.col = 2;
		cannon_chess.row = (i == 0 ? 1 : 7);
		cannon_chess.player = BLACK_PLAYER;
		cannon_chess.chessPiece = CANNON;
		vecChess.push_back(cannon_chess);
	}
	for (ll i = 0; i < 2; i++) {
		Chess cannon_chess;
		cannon_chess.col = 7;
		cannon_chess.row = (i == 0 ? 1 : 7);
		cannon_chess.player = RED_PLAYER;
		cannon_chess.chessPiece = CANNON;
		vecChess.push_back(cannon_chess);
	}
	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ChessFall, NULL, NULL, NULL);
	Sleep(500);
	printChessBoard();
	//车
	for (ll i = 0; i < 2; i++) {
		Chess rook_chess;
		rook_chess.col = 0;
		rook_chess.row = (i == 0 ? 0 : 8);
		rook_chess.player = BLACK_PLAYER;
		rook_chess.chessPiece = ROOK;
		vecChess.push_back(rook_chess);
	}
	for (ll i = 0; i < 2; i++) {
		Chess rook_chess;
		rook_chess.col = 9;
		rook_chess.row = (i == 0 ? 0 : 8);
		rook_chess.player = RED_PLAYER;
		rook_chess.chessPiece = ROOK;
		vecChess.push_back(rook_chess);
	}
	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ChessFall, NULL, NULL, NULL);
	Sleep(500);
	printChessBoard();
	//马
	for (ll i = 0; i < 2; i++) {
		Chess horse_chess;
		horse_chess.col = 0;
		horse_chess.row = (i == 0 ? 1 : 7);
		horse_chess.player = BLACK_PLAYER;
		horse_chess.chessPiece = HORSE;
		vecChess.push_back(horse_chess);
	}
	for (ll i = 0; i < 2; i++) {
		Chess horse_chess;
		horse_chess.col = 9;
		horse_chess.row = (i == 0 ? 1 : 7);
		horse_chess.player = RED_PLAYER;
		horse_chess.chessPiece = HORSE;
		vecChess.push_back(horse_chess);
	}
	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ChessFall, NULL, NULL, NULL);
	Sleep(500);
	printChessBoard();
	//象
	for (ll i = 0; i < 2; i++) {
		Chess elephant_chess;
		elephant_chess.col = 0;
		elephant_chess.row = (i == 0 ? 2 : 6);
		elephant_chess.player = BLACK_PLAYER;
		elephant_chess.chessPiece = ELEPHANT;
		vecChess.push_back(elephant_chess);
	}
	for (ll i = 0; i < 2; i++) {
		Chess elephant_chess;
		elephant_chess.col = 9;
		elephant_chess.row = (i == 0 ? 2 : 6);
		elephant_chess.player = RED_PLAYER;
		elephant_chess.chessPiece = ELEPHANT;
		vecChess.push_back(elephant_chess);
	}
	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ChessFall, NULL, NULL, NULL);
	Sleep(500);
	printChessBoard();
	//士
	for (ll i = 0; i < 2; i++) {
		Chess guard_chess;
		guard_chess.col = 0;
		guard_chess.row = (i == 0 ? 3 : 5);
		guard_chess.player = BLACK_PLAYER;
		guard_chess.chessPiece = GUARD;
		vecChess.push_back(guard_chess);
	}
	for (ll i = 0; i < 2; i++) {
		Chess guard_chess;
		guard_chess.col = 9;
		guard_chess.row = (i == 0 ? 3 : 5);
		guard_chess.player = RED_PLAYER;
		guard_chess.chessPiece = GUARD;
		vecChess.push_back(guard_chess);
	}
	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ChessFall, NULL, NULL, NULL);
	Sleep(500);
	printChessBoard();
	Chess general_chess;
	general_chess.col = 0;
	general_chess.row = 4;
	general_chess.player = BLACK_PLAYER;
	general_chess.chessPiece = GENERAL;
	vecChess.push_back(general_chess);
	general_chess.col = 9;
	general_chess.row = 4;
	general_chess.player = RED_PLAYER;
	general_chess.chessPiece = GENERAL;
	vecChess.push_back(general_chess);
	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ChessFall, NULL, NULL, NULL);
	Sleep(500);
	printChessBoard();
}
chessInfo getChessInfoByLocation(ll x, ll y, selectTime time) {
	if (x % BOARD_SIZE > CHESSPEICE_SIZE && x % BOARD_SIZE < BOARD_SIZE - CHESSPEICE_SIZE || y % BOARD_SIZE > CHESSPEICE_SIZE && y % BOARD_SIZE < BOARD_SIZE - CHESSPEICE_SIZE || x < BOARD_SIZE - CHESSPEICE_SIZE || x > BOARD_ROW * BOARD_SIZE + CHESSPEICE_SIZE || y < BOARD_SIZE - CHESSPEICE_SIZE || y>BOARD_COL * BOARD_SIZE + CHESSPEICE_SIZE) return ERROR_LOCATION;
	newRow = (x + CHESSPEICE_SIZE - BOARD_SIZE) / BOARD_SIZE;
	newCol = (y + CHESSPEICE_SIZE - BOARD_SIZE) / BOARD_SIZE;
	for (ll i = 0; i < vecChess.size(); i++) {
		if (vecChess[i].row == newRow && vecChess[i].col == newCol) {
			if (time == FIRST_TIME) {
				selectedChess = vecChess[i];
				if (vecChess[i].player == RED_PLAYER) return RED_CHESS;
				else return BLACK_CHESS;
			}
			else if (time == SECOND_TIME) {
				lastSelectedChess = selectedChess;
				selectedChess = vecChess[i];
				if (vecChess[i].player == RED_PLAYER) return RED_CHESS;
				else return BLACK_CHESS;
			}
		}
	}
	printChessBoard();
	FlushBatchDraw();
	return EMPTY_LOCATION;
}
bool chessMove(Chess chess) {
	printChessBoard();
	FlushBatchDraw();
	Chess newChess;
	newChess.row = newRow;
	newChess.col = newCol;
	newChess.player = chess.player;
	newChess.chessPiece = chess.chessPiece;
	for (auto iter = vecChess.begin(); iter != vecChess.end(); iter++) {
		if ((*iter).row == chess.row && (*iter).col == chess.col) {
			vecChess.erase(iter);
			vecChess.push_back(newChess);
			mciSendString("close res/音效.wav", 0, 0, 0);
			mciSendString("play res/音效.wav", 0, 0, 0);
			return 1;
		}
	}
	return 0;
}
bool pawnMove(Chess chess) {
	if (chess.player == RED_PLAYER) {
		printChessBoard();
		FlushBatchDraw();
		if (newCol > chess.col) return 0;
		if (chess.col <= 4) {
			ll x = abs(chess.col - newCol) + abs(chess.row - newRow);
			if (x != 1) return 0;
		}
		else {
			ll x = abs(chess.col - newCol) + abs(chess.row - newRow);
			if (x != 1 || abs(chess.row - newRow) != 0) return 0;
		}
	}
	if (chess.player == BLACK_PLAYER) {
		printChessBoard();
		FlushBatchDraw();
		if (newCol < chess.col) return 0;
		if (chess.col >= 5) {
			ll x = abs(chess.col - newCol) + abs(chess.row - newRow);
			if (x != 1) return 0;
		}
		else {
			ll x = abs(chess.col - newCol) + abs(chess.row - newRow);
			if (x != 1 || abs(chess.row - newRow) != 0) return 0;
		}
	}
	return chessMove(chess);
}
bool cannonMove(Chess chess) {
	if ((chess.row != newRow && chess.col != newCol) || (chess.row == newRow && chess.col == newCol)) return 0;
	ll startLocation = 0, endLocation = 0, rowCount = 0, colCount = 0;
	bool diffRow = false, diffCol = false;
	if (chess.row != newRow) {
		diffRow = 1;
		if (chess.row < newRow) startLocation = chess.row, endLocation = newRow;
		else startLocation = newRow, endLocation = chess.row;
	}
	else {
		diffCol = 1;
		if (chess.col < newCol) startLocation = chess.col, endLocation = newCol;
		else startLocation = newCol, endLocation = chess.col;
	}
	bool isChess = false;
	for (auto iter = vecChess.begin(); iter != vecChess.end(); iter++) {
		if (iter->row == newRow && iter->col == newCol) {
			isChess = true;
			if (iter->player == chess.player) return false;
		}
		if ((iter->col == newCol) && (iter->row > startLocation && iter->row < endLocation)) rowCount++;
		if ((iter->row == newRow) && (iter->col > startLocation && iter->col < endLocation)) colCount++;
	}
	if (isChess) {
		if (diffRow) if (rowCount != 1) return false;
		if (diffCol) if (colCount != 1) return false;
	}
	else {
		if (diffRow) if (rowCount != 0) return false;
		if (diffCol) if (colCount != 0) return false;
	}
	return chessMove(chess);
}
bool rookMove(Chess chess) {
	if ((chess.row != newRow && chess.col != newCol) || (chess.row == newRow && chess.col == newCol)) return 0;
	ll startLocation = 0, endLocation = 0;
	bool diffRow = false, diffCol = false;
	if (chess.row != newRow) {
		diffRow = 1;
		if (chess.row < newRow) startLocation = chess.row, endLocation = newRow;
		else startLocation = newRow, endLocation = chess.row;
	}
	else {
		diffCol = 1;
		if (chess.col < newCol) startLocation = chess.col, endLocation = newCol;
		else startLocation = newCol, endLocation = chess.col;
	}
	bool isChess = false;
	for (auto iter = vecChess.begin(); iter != vecChess.end(); iter++) {
		if (diffRow && (iter->col == newCol && (iter->row > startLocation) && iter->row < endLocation)) return false;
		if (diffCol && (iter->row == newRow && (iter->col > startLocation) && iter->col < endLocation)) return false;
	}
	return chessMove(chess);
}
bool horseMove(Chess chess) {
	if ((chess.row - newRow) * (chess.col - newCol) != -2 && (chess.row - newRow) * (chess.col - newCol) != 2) {
		return false;
	}
	ll legRow = 0, legCol = 0;
	if (chess.row - newRow == 2 && chess.col - newCol == 1) {
		legRow = chess.row - 1;
		legCol = chess.col;
	}
	if (chess.row - newRow == 2 && chess.col - newCol == -1) {
		legRow = chess.row - 1;
		legCol = chess.col;
	}
	if (chess.row - newRow == -2 && chess.col - newCol == 1) {
		legRow = chess.row + 1;
		legCol = chess.col;
	}
	if (chess.row - newRow == -2 && chess.col - newCol == -1) {
		legRow = chess.row + 1;
		legCol = chess.col;
	}
	if (chess.row - newRow == 1 && chess.col - newCol == 2) {
		legRow = chess.row;
		legCol = chess.col - 1;
	}
	if (chess.row - newRow == -1 && chess.col - newCol == 2) {
		legRow = chess.row;
		legCol = chess.col - 1;
	}
	if (chess.row - newRow == 1 && chess.col - newCol == -2) {
		legRow = chess.row;
		legCol = chess.col + 1;
	}
	if (chess.row - newRow == -1 && chess.col - newCol == -2) {
		legRow = chess.row;
		legCol = chess.col + 1;
	}
	for (auto iter = vecChess.begin(); iter != vecChess.end(); iter++) {
		if (iter->col == legCol && iter->row == legRow) {
			return false;
		}
	}
	return chessMove(chess);
}
bool elephantMove(Chess chess) {
	if ((chess.row - newRow) * (chess.col - newCol) != -4 && (chess.row - newRow) * (chess.col - newCol) != 4) return false;
	if (chess.player == RED_PLAYER && newCol <= 4) return false;
	if (chess.player == BLACK_PLAYER && newCol >= 5) return false;
	ll eyeRow = 0, eyeCol = 0;
	if (chess.row - newRow == 2 && chess.col - newCol == 2) {
		eyeRow = chess.row - 1, eyeCol = chess.col - 1;
	}
	if (chess.row - newRow == 2 && chess.col - newCol == -2) {
		eyeRow = chess.row - 1, eyeCol = chess.col + 1;
	}
	if (chess.row - newRow == -2 && chess.col - newCol == 2) {
		eyeRow = chess.row + 1, eyeCol = chess.col - 1;
	}
	if (chess.row - newRow == -2 && chess.col - newCol == -2) {
		eyeRow = chess.row + 1, eyeCol = chess.col + 1;
	}
	for (auto iter = vecChess.begin(); iter != vecChess.end(); iter++) {
		if (iter->row == eyeRow && iter->col == eyeCol) return false;
	}
	return chessMove(chess);
}
bool guardMove(Chess chess) {
	if ((chess.row - newRow) * (chess.col - newCol) != -1 && (chess.row - newRow) * (chess.col - newCol) != 1 || (chess.row == newRow && chess.col == newCol)) return false;
	if (chess.player == RED_PLAYER && (newRow < 3 || newRow > 5 || newCol < 7)) return false;
	if (chess.player == BLACK_PLAYER && (newRow < 3 || newRow > 5 || newCol > 2)) return false;
	return chessMove(chess);
}
bool generalMove(Chess chess) {
	if (((chess.row - newRow) + (chess.col - newCol) != -1 && (chess.row - newRow) + (chess.col - newCol) != 1) || (chess.row != newRow && chess.col != newCol) || (chess.row == newRow && chess.col == newCol)) return false;
	if (chess.player == RED_PLAYER && (newRow < 3 || newRow > 5 || newCol < 7)) return false;
	if (chess.player == BLACK_PLAYER && (newRow < 3 || newRow > 5 || newCol > 2)) return false;
	if (chess.player == RED_PLAYER) {
		redGeneralRow = newRow;
		redGeneralCol = newCol;
	}
	else {
		blackGeneralRow = newRow;
		blackGeneralCol = newCol;
	}
	return chessMove(chess);
}
bool moveToEmptyLocation() {
	bool isSuccessMove = 0;
	printChessBoard();
	FlushBatchDraw();
	if (selectedChess.chessPiece == PAWN) {
		isSuccessMove = pawnMove(selectedChess);
	}
	else if (selectedChess.chessPiece == CANNON) {
		isSuccessMove = cannonMove(selectedChess);
	}
	else if (selectedChess.chessPiece == ROOK) {
		isSuccessMove = rookMove(selectedChess);
	}
	else if (selectedChess.chessPiece == HORSE) {
		isSuccessMove = horseMove(selectedChess);
	}
	else if (selectedChess.chessPiece == ELEPHANT) {
		isSuccessMove = elephantMove(selectedChess);
	}
	else if (selectedChess.chessPiece == GUARD) {
		isSuccessMove = guardMove(selectedChess);
	}
	else if (selectedChess.chessPiece == GENERAL) {
		isSuccessMove = generalMove(selectedChess);
	}
	if (isSuccessMove) {
		printChessBoard();
		FlushBatchDraw();
		lastSelectedChess = { -1,-1 };
	}
	return isSuccessMove;
}
void reselectChess() {
	printChessBoard();
	FlushBatchDraw();
	lastSelectedChess = { -1,-1 };
}
void gameEndShow(Player player) {
	printChessBoard();
	clearrectangle(0, 0, (BOARD_ROW + 1) * BOARD_SIZE, (BOARD_COL + 1) * BOARD_SIZE);
	cleardevice();
	FlushBatchDraw();
	TCHAR endStr[30];
	if (player == RED_PLAYER) {
		settextcolor(RED);
		_stprintf_s(endStr, "红方胜利");
	}
	else {
		settextcolor(BLACK);
		_stprintf_s(endStr, "黑方胜利");
	}
	outtextxy(((BOARD_ROW + 1) * BOARD_SIZE-textwidth(endStr))/2, ((BOARD_COL + 1) * BOARD_SIZE-textheight(endStr))/2, endStr);
	FlushBatchDraw();
	Sleep(3000);
	EndBatchDraw();
	closegraph();
	exit(0);
}
bool capture() {
	bool isCapture = false;
	if (lastSelectedChess.chessPiece == PAWN) {
		isCapture = pawnMove(lastSelectedChess);
	}
	else if (lastSelectedChess.chessPiece == CANNON) {
		isCapture = cannonMove(lastSelectedChess);
	}
	else if (lastSelectedChess.chessPiece == ROOK) {
		isCapture = rookMove(lastSelectedChess);
	}
	else if (lastSelectedChess.chessPiece == HORSE) {
		isCapture = horseMove(lastSelectedChess);
	}
	else if (lastSelectedChess.chessPiece == ELEPHANT) {
		isCapture = elephantMove(lastSelectedChess);
	}
	else if (lastSelectedChess.chessPiece == GUARD) {
		isCapture = guardMove(lastSelectedChess);
	}
	else if (lastSelectedChess.chessPiece == GENERAL) {
		isCapture = generalMove(lastSelectedChess);
	}
	if (isCapture) {
		for (auto iter = vecChess.begin(); iter != vecChess.end(); iter++) {
			if (iter->row == selectedChess.row && iter->col == selectedChess.col && iter->player == selectedChess.player && iter->chessPiece == selectedChess.chessPiece) {
				if (iter->chessPiece == GENERAL) {
					gameEndShow((iter->player == RED_PLAYER ? BLACK_PLAYER : RED_PLAYER));
				}
				vecChess.erase(iter);
				lastSelectedChess = { -1,-1 };
				selectedChess = { -1,-1 };
				printChessBoard();
				FlushBatchDraw();
				break;
			}
		}
	}
	else {
		selectedChess = lastSelectedChess;
		lastSelectedChess = { -1,-1 };
	}
	return isCapture;
}
bool isGameOver() {
	if (redGeneralRow == blackGeneralRow) {
		int cnt = 0, krow = redGeneralRow;
		for (auto iter = vecChess.begin(); iter != vecChess.end(); iter++) {
			if (iter->row == krow && iter->col > blackGeneralCol && iter->col < redGeneralCol) {
				cnt++;
			}
		}
		if (cnt) return 0;
		else return 1;
	}
	return 0;
}
void ZhongGuoXiangQi() {
	//Sleep(1500);
	settextstyle(150, 0, "华文行楷");
	settextcolor(RED);
	setbkmode(TRANSPARENT);
	outtextxy(20, 50, "中");
	FlushBatchDraw();
	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ChessFall, NULL, NULL, NULL);
	Sleep(1500);
	outtextxy(90, 230, "国");
	FlushBatchDraw();
	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ChessFall, NULL, NULL, NULL);
	Sleep(1500);
	outtextxy(240, 145, "象");
	FlushBatchDraw();
	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ChessFall, NULL, NULL, NULL);
	Sleep(1500);
	outtextxy(270, 345, "棋");
	FlushBatchDraw();
	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ChessFall, NULL, NULL, NULL);
	Sleep(1500);
	settextstyle(70, 0, "华文行楷");
	outtextxy(50, 400, "帅");
	setlinecolor(RED);
	setlinestyle(PS_SOLID, 7);
	circle(50 + textwidth("帅") / 2, 400 + textheight("帅") / 2-5, (textwidth("帅") + textheight("帅")) / 4);
	FlushBatchDraw();
	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ChessFall, NULL, NULL, NULL);
	Sleep(1500);
	ExMessage msg = { 0 };
	while (msg.message != WM_LBUTTONDOWN) {
		getmessage(&msg, EX_MOUSE);
	}
}
int main() {
	HWND hwnd = initgraph((BOARD_ROW + 1) * BOARD_SIZE, (BOARD_COL + 1) * BOARD_SIZE);
	setbkcolor(RGB(211, 177, 125));
	cleardevice();
	mciSendString("open res/落子.mp3", 0, 0, 0);
	BeginBatchDraw();
	ZhongGuoXiangQi();
	initChessPiece();
	while (1) {
		cleardevice();
		ExMessage msg_select = { 0 };
		getmessage(&msg_select, EX_MOUSE);
		printChessBoard();
		FlushBatchDraw();
		if (msg_select.message == WM_LBUTTONUP) {
			chessInfo selected = getChessInfoByLocation(msg_select.x, msg_select.y, FIRST_TIME);
			printChessBoard();
			if (player == selectedChess.player) {
				drawChessPiece(selectedChess);
				printChessBoard();
				FlushBatchDraw();
				bool isSuccessMove = false;
				while (!isSuccessMove) {
					getmessage(&msg_select, EX_MOUSE);
					if (msg_select.message != WM_LBUTTONUP) continue;
					chessInfo move = getChessInfoByLocation(msg_select.x, msg_select.y, SECOND_TIME);
					if (move == EMPTY_LOCATION) {
						if (moveToEmptyLocation()) {
							printChessBoard();
							FlushBatchDraw();
							isSuccessMove = true;
							player = (player == RED_PLAYER ? BLACK_PLAYER : RED_PLAYER);
							if (isGameOver()) {
								Sleep(1500);
								gameEndShow(player);
							}
						}
					}
					else if (move == RED_CHESS || move == BLACK_CHESS) {
						printChessBoard();
						FlushBatchDraw();
						if ((move == RED_CHESS && player == RED_PLAYER) || (move == BLACK_CHESS && player == BLACK_PLAYER)) {
							printChessBoard();
							FlushBatchDraw();
							reselectChess();
						}
						else {
							if (capture()) {
								player = (player == RED_PLAYER ? BLACK_PLAYER : RED_PLAYER);
								if (isGameOver()) {
									Sleep(1500);
									gameEndShow(player);
								}
							}
							printChessBoard();
							FlushBatchDraw();
						}
					}
				}
			}
		}
		printChessBoard();
		FlushBatchDraw();
	}
	return 0;
}

下面是代码解析:

ZhongGuoXiangQi:

void ZhongGuoXiangQi() {
	//Sleep(1500);
	settextstyle(150, 0, "华文行楷");
	settextcolor(RED);
	setbkmode(TRANSPARENT);
	outtextxy(20, 50, "中");
	FlushBatchDraw();
	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ChessFall, NULL, NULL, NULL);
	Sleep(1500);
	outtextxy(90, 230, "国");
	FlushBatchDraw();
	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ChessFall, NULL, NULL, NULL);
	Sleep(1500);
	outtextxy(240, 145, "象");
	FlushBatchDraw();
	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ChessFall, NULL, NULL, NULL);
	Sleep(1500);
	outtextxy(270, 345, "棋");
	FlushBatchDraw();
	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ChessFall, NULL, NULL, NULL);
	Sleep(1500);
	settextstyle(70, 0, "华文行楷");
	outtextxy(50, 400, "帅");
	setlinecolor(RED);
	setlinestyle(PS_SOLID, 7);
	circle(50 + textwidth("帅") / 2, 400 + textheight("帅") / 2-5, (textwidth("帅") + textheight("帅")) / 4);
	FlushBatchDraw();
	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ChessFall, NULL, NULL, NULL);
	Sleep(1500);
	ExMessage msg = { 0 };
	while (msg.message != WM_LBUTTONDOWN) {
		getmessage(&msg, EX_MOUSE);
	}
}

是下棋前的界面的初始化,这里我是手动调的文字位置,聪明的小伙伴们可以自行修改为自动调节的版本(我懒,不想写)

printChessBoard:

void printChessBoard() {
	clearrectangle(0, 0, (BOARD_ROW + 1) * BOARD_SIZE, (BOARD_COL + 1) * BOARD_SIZE);
	setlinecolor(RED);
	setlinestyle(PS_SOLID, 2);
	setfillcolor(RGB(211, 177, 125));
	fillrectangle(BOARD_SIZE - 6, BOARD_SIZE - 6, BOARD_ROW * BOARD_SIZE + 6, BOARD_COL * BOARD_SIZE + 6);
	for (ll i = 0; i < BOARD_ROW - 1; i++) {
		for (ll j = 0; j < BOARD_COL - 1; j++) {
			if (j == 4) continue;
			fillrectangle((i + 1) * BOARD_SIZE, (j + 1) * BOARD_SIZE, (i + 2) * BOARD_SIZE, (j + 2) * BOARD_SIZE);
		}
	}
	line(BOARD_SIZE, BOARD_SIZE, BOARD_SIZE, BOARD_COL* BOARD_SIZE);
	line(BOARD_ROW*BOARD_SIZE, BOARD_SIZE, BOARD_ROW*BOARD_SIZE, BOARD_COL* BOARD_SIZE);
	line(4 * BOARD_SIZE, BOARD_SIZE, 6 * BOARD_SIZE, 3 * BOARD_SIZE);
	line(6 * BOARD_SIZE, BOARD_SIZE, 4 * BOARD_SIZE, 3 * BOARD_SIZE);
	line(4 * BOARD_SIZE, 8 * BOARD_SIZE, 6 * BOARD_SIZE, 10 * BOARD_SIZE);
	line(6 * BOARD_SIZE, 8 * BOARD_SIZE, 4 * BOARD_SIZE, 10 * BOARD_SIZE);
	//兵点十字
	line(BOARD_SIZE + 5, 7 * BOARD_SIZE - 13, BOARD_SIZE + 5, 7 * BOARD_SIZE - 5);
	line(BOARD_SIZE + 5, 7 * BOARD_SIZE - 5, BOARD_SIZE + 13, 7 * BOARD_SIZE - 5);
	line(3 * BOARD_SIZE + 5, 7 * BOARD_SIZE - 13, 3 * BOARD_SIZE + 5, 7 * BOARD_SIZE - 5);
	line(3 * BOARD_SIZE + 5, 7 * BOARD_SIZE - 5, 3 * BOARD_SIZE + 13, 7 * BOARD_SIZE - 5);
	line(5 * BOARD_SIZE + 5, 7 * BOARD_SIZE - 13, 5 * BOARD_SIZE + 5, 7 * BOARD_SIZE - 5);
	line(5 * BOARD_SIZE + 5, 7 * BOARD_SIZE - 5, 5 * BOARD_SIZE + 13, 7 * BOARD_SIZE - 5);
	line(7 * BOARD_SIZE + 5, 7 * BOARD_SIZE - 13, 7 * BOARD_SIZE + 5, 7 * BOARD_SIZE - 5);
	line(7 * BOARD_SIZE + 5, 7 * BOARD_SIZE - 5, 7 * BOARD_SIZE + 13, 7 * BOARD_SIZE - 5);
	line(BOARD_SIZE + 5, 4 * BOARD_SIZE - 13, BOARD_SIZE + 5, 4 * BOARD_SIZE - 5);
	line(BOARD_SIZE + 5, 4 * BOARD_SIZE - 5, BOARD_SIZE + 13, 4 * BOARD_SIZE - 5);
	line(3 * BOARD_SIZE + 5, 4 * BOARD_SIZE - 13, 3 * BOARD_SIZE + 5, 4 * BOARD_SIZE - 5);
	line(3 * BOARD_SIZE + 5, 4 * BOARD_SIZE - 5, 3 * BOARD_SIZE + 13, 4 * BOARD_SIZE - 5);
	line(5 * BOARD_SIZE + 5, 4 * BOARD_SIZE - 13, 5 * BOARD_SIZE + 5, 4 * BOARD_SIZE - 5);
	line(5 * BOARD_SIZE + 5, 4 * BOARD_SIZE - 5, 5 * BOARD_SIZE + 13, 4 * BOARD_SIZE - 5);
	line(7 * BOARD_SIZE + 5, 4 * BOARD_SIZE - 13, 7 * BOARD_SIZE + 5, 4 * BOARD_SIZE - 5);
	line(7 * BOARD_SIZE + 5, 4 * BOARD_SIZE - 5, 7 * BOARD_SIZE + 13, 4 * BOARD_SIZE - 5);
	line(3 * BOARD_SIZE - 5, 7 * BOARD_SIZE - 13, 3 * BOARD_SIZE - 5, 7 * BOARD_SIZE - 5);
	line(3 * BOARD_SIZE - 5, 7 * BOARD_SIZE - 5, 3 * BOARD_SIZE - 13, 7 * BOARD_SIZE - 5);
	line(5 * BOARD_SIZE - 5, 7 * BOARD_SIZE - 13, 5 * BOARD_SIZE - 5, 7 * BOARD_SIZE - 5);
	line(5 * BOARD_SIZE - 5, 7 * BOARD_SIZE - 5, 5 * BOARD_SIZE - 13, 7 * BOARD_SIZE - 5);
	line(7 * BOARD_SIZE - 5, 7 * BOARD_SIZE - 13, 7 * BOARD_SIZE - 5, 7 * BOARD_SIZE - 5);
	line(7 * BOARD_SIZE - 5, 7 * BOARD_SIZE - 5, 7 * BOARD_SIZE - 13, 7 * BOARD_SIZE - 5);
	line(9 * BOARD_SIZE - 5, 7 * BOARD_SIZE - 13, 9 * BOARD_SIZE - 5, 7 * BOARD_SIZE - 5);
	line(9 * BOARD_SIZE - 5, 7 * BOARD_SIZE - 5, 9 * BOARD_SIZE - 13, 7 * BOARD_SIZE - 5);
	line(3 * BOARD_SIZE - 5, 4 * BOARD_SIZE - 13, 3 * BOARD_SIZE - 5, 4 * BOARD_SIZE - 5);
	line(3 * BOARD_SIZE - 5, 4 * BOARD_SIZE - 5, 3 * BOARD_SIZE - 13, 4 * BOARD_SIZE - 5);
	line(5 * BOARD_SIZE - 5, 4 * BOARD_SIZE - 13, 5 * BOARD_SIZE - 5, 4 * BOARD_SIZE - 5);
	line(5 * BOARD_SIZE - 5, 4 * BOARD_SIZE - 5, 5 * BOARD_SIZE - 13, 4 * BOARD_SIZE - 5);
	line(7 * BOARD_SIZE - 5, 4 * BOARD_SIZE - 13, 7 * BOARD_SIZE - 5, 4 * BOARD_SIZE - 5);
	line(7 * BOARD_SIZE - 5, 4 * BOARD_SIZE - 5, 7 * BOARD_SIZE - 13, 4 * BOARD_SIZE - 5);
	line(9 * BOARD_SIZE - 5, 4 * BOARD_SIZE - 13, 9 * BOARD_SIZE - 5, 4 * BOARD_SIZE - 5);
	line(9 * BOARD_SIZE - 5, 4 * BOARD_SIZE - 5, 9 * BOARD_SIZE - 13, 4 * BOARD_SIZE - 5);
	line(BOARD_SIZE + 5, 7 * BOARD_SIZE + 13, BOARD_SIZE + 5, 7 * BOARD_SIZE + 5);
	line(BOARD_SIZE + 5, 7 * BOARD_SIZE + 5, BOARD_SIZE + 13, 7 * BOARD_SIZE + 5);
	line(3 * BOARD_SIZE + 5, 7 * BOARD_SIZE + 13, 3 * BOARD_SIZE + 5, 7 * BOARD_SIZE + 5);
	line(3 * BOARD_SIZE + 5, 7 * BOARD_SIZE + 5, 3 * BOARD_SIZE + 13, 7 * BOARD_SIZE + 5);
	line(5 * BOARD_SIZE + 5, 7 * BOARD_SIZE + 13, 5 * BOARD_SIZE + 5, 7 * BOARD_SIZE + 5);
	line(5 * BOARD_SIZE + 5, 7 * BOARD_SIZE + 5, 5 * BOARD_SIZE + 13, 7 * BOARD_SIZE + 5);
	line(7 * BOARD_SIZE + 5, 7 * BOARD_SIZE + 13, 7 * BOARD_SIZE + 5, 7 * BOARD_SIZE + 5);
	line(7 * BOARD_SIZE + 5, 7 * BOARD_SIZE + 5, 7 * BOARD_SIZE + 13, 7 * BOARD_SIZE + 5);
	line(BOARD_SIZE + 5, 4 * BOARD_SIZE + 13, BOARD_SIZE + 5, 4 * BOARD_SIZE + 5);
	line(BOARD_SIZE + 5, 4 * BOARD_SIZE + 5, BOARD_SIZE + 13, 4 * BOARD_SIZE + 5);
	line(3 * BOARD_SIZE + 5, 4 * BOARD_SIZE + 13, 3 * BOARD_SIZE + 5, 4 * BOARD_SIZE + 5);
	line(3 * BOARD_SIZE + 5, 4 * BOARD_SIZE + 5, 3 * BOARD_SIZE + 13, 4 * BOARD_SIZE + 5);
	line(5 * BOARD_SIZE + 5, 4 * BOARD_SIZE + 13, 5 * BOARD_SIZE + 5, 4 * BOARD_SIZE + 5);
	line(5 * BOARD_SIZE + 5, 4 * BOARD_SIZE + 5, 5 * BOARD_SIZE + 13, 4 * BOARD_SIZE + 5);
	line(7 * BOARD_SIZE + 5, 4 * BOARD_SIZE + 13, 7 * BOARD_SIZE + 5, 4 * BOARD_SIZE + 5);
	line(7 * BOARD_SIZE + 5, 4 * BOARD_SIZE + 5, 7 * BOARD_SIZE + 13, 4 * BOARD_SIZE + 5);
	line(3 * BOARD_SIZE - 5, 7 * BOARD_SIZE + 13, 3 * BOARD_SIZE - 5, 7 * BOARD_SIZE + 5);
	line(3 * BOARD_SIZE - 5, 7 * BOARD_SIZE + 5, 3 * BOARD_SIZE - 13, 7 * BOARD_SIZE + 5);
	line(5 * BOARD_SIZE - 5, 7 * BOARD_SIZE + 13, 5 * BOARD_SIZE - 5, 7 * BOARD_SIZE + 5);
	line(5 * BOARD_SIZE - 5, 7 * BOARD_SIZE + 5, 5 * BOARD_SIZE - 13, 7 * BOARD_SIZE + 5);
	line(7 * BOARD_SIZE - 5, 7 * BOARD_SIZE + 13, 7 * BOARD_SIZE - 5, 7 * BOARD_SIZE + 5);
	line(7 * BOARD_SIZE - 5, 7 * BOARD_SIZE + 5, 7 * BOARD_SIZE - 13, 7 * BOARD_SIZE + 5);
	line(9 * BOARD_SIZE - 5, 7 * BOARD_SIZE + 13, 9 * BOARD_SIZE - 5, 7 * BOARD_SIZE + 5);
	line(9 * BOARD_SIZE - 5, 7 * BOARD_SIZE + 5, 9 * BOARD_SIZE - 13, 7 * BOARD_SIZE + 5);
	line(3 * BOARD_SIZE - 5, 4 * BOARD_SIZE + 13, 3 * BOARD_SIZE - 5, 4 * BOARD_SIZE + 5);
	line(3 * BOARD_SIZE - 5, 4 * BOARD_SIZE + 5, 3 * BOARD_SIZE - 13, 4 * BOARD_SIZE + 5);
	line(5 * BOARD_SIZE - 5, 4 * BOARD_SIZE + 13, 5 * BOARD_SIZE - 5, 4 * BOARD_SIZE + 5);
	line(5 * BOARD_SIZE - 5, 4 * BOARD_SIZE + 5, 5 * BOARD_SIZE - 13, 4 * BOARD_SIZE + 5);
	line(7 * BOARD_SIZE - 5, 4 * BOARD_SIZE + 13, 7 * BOARD_SIZE - 5, 4 * BOARD_SIZE + 5);
	line(7 * BOARD_SIZE - 5, 4 * BOARD_SIZE + 5, 7 * BOARD_SIZE - 13, 4 * BOARD_SIZE + 5);
	line(9 * BOARD_SIZE - 5, 4 * BOARD_SIZE + 13, 9 * BOARD_SIZE - 5, 4 * BOARD_SIZE + 5);
	line(9 * BOARD_SIZE - 5, 4 * BOARD_SIZE + 5, 9 * BOARD_SIZE - 13, 4 * BOARD_SIZE + 5);
	//炮点十字
	line(2 * BOARD_SIZE + 5, 3 * BOARD_SIZE - 13, 2 * BOARD_SIZE + 5, 3 * BOARD_SIZE - 5);
	line(2 * BOARD_SIZE + 5, 3 * BOARD_SIZE - 5, 2 * BOARD_SIZE + 13, 3 * BOARD_SIZE - 5);
	line(8 * BOARD_SIZE + 5, 3 * BOARD_SIZE - 13, 8 * BOARD_SIZE + 5, 3 * BOARD_SIZE - 5);
	line(8 * BOARD_SIZE + 5, 3 * BOARD_SIZE - 5, 8 * BOARD_SIZE + 13, 3 * BOARD_SIZE - 5);
	line(2 * BOARD_SIZE + 5, 8 * BOARD_SIZE - 13, 2 * BOARD_SIZE + 5, 8 * BOARD_SIZE - 5);
	line(2 * BOARD_SIZE + 5, 8 * BOARD_SIZE - 5, 2 * BOARD_SIZE + 13, 8 * BOARD_SIZE - 5);
	line(8 * BOARD_SIZE + 5, 8 * BOARD_SIZE - 13, 8 * BOARD_SIZE + 5, 8 * BOARD_SIZE - 5);
	line(8 * BOARD_SIZE + 5, 8 * BOARD_SIZE - 5, 8 * BOARD_SIZE + 13, 8 * BOARD_SIZE - 5);
	line(2 * BOARD_SIZE - 5, 3 * BOARD_SIZE + 13, 2 * BOARD_SIZE - 5, 3 * BOARD_SIZE + 5);
	line(2 * BOARD_SIZE - 5, 3 * BOARD_SIZE + 5, 2 * BOARD_SIZE - 13, 3 * BOARD_SIZE + 5);
	line(8 * BOARD_SIZE - 5, 3 * BOARD_SIZE + 13, 8 * BOARD_SIZE - 5, 3 * BOARD_SIZE + 5);
	line(8 * BOARD_SIZE - 5, 3 * BOARD_SIZE + 5, 8 * BOARD_SIZE - 13, 3 * BOARD_SIZE + 5);
	line(2 * BOARD_SIZE - 5, 8 * BOARD_SIZE + 13, 2 * BOARD_SIZE - 5, 8 * BOARD_SIZE + 5);
	line(2 * BOARD_SIZE - 5, 8 * BOARD_SIZE + 5, 2 * BOARD_SIZE - 13, 8 * BOARD_SIZE + 5);
	line(8 * BOARD_SIZE - 5, 8 * BOARD_SIZE + 13, 8 * BOARD_SIZE - 5, 8 * BOARD_SIZE + 5);
	line(8 * BOARD_SIZE - 5, 8 * BOARD_SIZE + 5, 8 * BOARD_SIZE - 13, 8 * BOARD_SIZE + 5);
	line(2 * BOARD_SIZE + 5, 3 * BOARD_SIZE + 13, 2 * BOARD_SIZE + 5, 3 * BOARD_SIZE + 5);
	line(2 * BOARD_SIZE + 5, 3 * BOARD_SIZE + 5, 2 * BOARD_SIZE + 13, 3 * BOARD_SIZE + 5);
	line(8 * BOARD_SIZE + 5, 3 * BOARD_SIZE + 13, 8 * BOARD_SIZE + 5, 3 * BOARD_SIZE + 5);
	line(8 * BOARD_SIZE + 5, 3 * BOARD_SIZE + 5, 8 * BOARD_SIZE + 13, 3 * BOARD_SIZE + 5);
	line(2 * BOARD_SIZE + 5, 8 * BOARD_SIZE + 13, 2 * BOARD_SIZE + 5, 8 * BOARD_SIZE + 5);
	line(2 * BOARD_SIZE + 5, 8 * BOARD_SIZE + 5, 2 * BOARD_SIZE + 13, 8 * BOARD_SIZE + 5);
	line(8 * BOARD_SIZE + 5, 8 * BOARD_SIZE + 13, 8 * BOARD_SIZE + 5, 8 * BOARD_SIZE + 5);
	line(8 * BOARD_SIZE + 5, 8 * BOARD_SIZE + 5, 8 * BOARD_SIZE + 13, 8 * BOARD_SIZE + 5);
	line(2 * BOARD_SIZE - 5, 3 * BOARD_SIZE - 13, 2 * BOARD_SIZE - 5, 3 * BOARD_SIZE - 5);
	line(2 * BOARD_SIZE - 5, 3 * BOARD_SIZE - 5, 2 * BOARD_SIZE - 13, 3 * BOARD_SIZE - 5);
	line(8 * BOARD_SIZE - 5, 3 * BOARD_SIZE - 13, 8 * BOARD_SIZE - 5, 3 * BOARD_SIZE - 5);
	line(8 * BOARD_SIZE - 5, 3 * BOARD_SIZE - 5, 8 * BOARD_SIZE - 13, 3 * BOARD_SIZE - 5);
	line(2 * BOARD_SIZE - 5, 8 * BOARD_SIZE - 13, 2 * BOARD_SIZE - 5, 8 * BOARD_SIZE - 5);
	line(2 * BOARD_SIZE - 5, 8 * BOARD_SIZE - 5, 2 * BOARD_SIZE - 13, 8 * BOARD_SIZE - 5);
	line(8 * BOARD_SIZE - 5, 8 * BOARD_SIZE - 13, 8 * BOARD_SIZE - 5, 8 * BOARD_SIZE - 5);
	line(8 * BOARD_SIZE - 5, 8 * BOARD_SIZE - 5, 8 * BOARD_SIZE - 13, 8 * BOARD_SIZE - 5);
	//文字
	settextstyle(35, 0, "华文行楷");
	settextcolor(RED);
	TCHAR chhj[100] = " 楚  河                    汉  界 ";
	outtextxy(((BOARD_ROW + 1) * BOARD_SIZE - textwidth(chhj)) / 2, ((BOARD_COL + 1) * BOARD_SIZE - textheight(chhj)) / 2, chhj);
	for (ll i = 0; i < vecChess.size(); i++) drawChessPiece(vecChess[i]);
	FlushBatchDraw();
}

是打印棋盘用的

drawChessPiece:

void drawChessPiece(Chess chess) {
	ll sz = BOARD_SIZE / 3 + 10;
	if (selectedChess.row == chess.row && selectedChess.col == chess.col) settextstyle(sz + 6, 0, "华文行楷");
	else settextstyle(sz, 0, "华文行楷");
	if (chess.player == RED_PLAYER) {
		setlinecolor(RED);
		settextcolor(RED);
	}
	else {
		setlinecolor(BLACK);
		settextcolor(BLACK);
	}
	if (selectedChess.row == chess.row && selectedChess.col == chess.col) settextcolor(LIGHTCYAN), setlinecolor(LIGHTCYAN);
	setfillcolor(RGB(240, 134, 80));
	fillcircle((chess.row + 1) * BOARD_SIZE, (chess.col + 1) * BOARD_SIZE, CHESSPEICE_SIZE);
	TCHAR endStr[16];
	if (chess.chessPiece == PAWN && chess.player == RED_PLAYER) _stprintf_s(endStr, "兵");
	if (chess.chessPiece == PAWN && chess.player == BLACK_PLAYER) _stprintf_s(endStr, "卒");
	if (chess.chessPiece == CANNON && chess.player == RED_PLAYER) _stprintf_s(endStr, "炮");
	if (chess.chessPiece == CANNON && chess.player == BLACK_PLAYER) _stprintf_s(endStr, "炮");
	if (chess.chessPiece == ROOK && chess.player == RED_PLAYER) _stprintf_s(endStr, "车");
	if (chess.chessPiece == ROOK && chess.player == BLACK_PLAYER) _stprintf_s(endStr, "车");
	if (chess.chessPiece == HORSE && chess.player == RED_PLAYER) _stprintf_s(endStr, "马");
	if (chess.chessPiece == HORSE && chess.player == BLACK_PLAYER) _stprintf_s(endStr, "马");
	if (chess.chessPiece == ELEPHANT && chess.player == RED_PLAYER) _stprintf_s(endStr, "相");
	if (chess.chessPiece == ELEPHANT && chess.player == BLACK_PLAYER) _stprintf_s(endStr, "象");
	if (chess.chessPiece == GUARD && chess.player == RED_PLAYER) _stprintf_s(endStr, "士");
	if (chess.chessPiece == GUARD && chess.player == BLACK_PLAYER) _stprintf_s(endStr, "士");
	if (chess.chessPiece == GENERAL && chess.player == RED_PLAYER) _stprintf_s(endStr, "帅");
	if (chess.chessPiece == GENERAL && chess.player == BLACK_PLAYER) _stprintf_s(endStr, "将");
	setbkmode(TRANSPARENT);
	if (selectedChess.row == chess.row && selectedChess.col == chess.col) outtextxy((chess.row + 0.72) * BOARD_SIZE, (chess.col + 0.72) * BOARD_SIZE, endStr);
	else outtextxy((chess.row + 0.74) * BOARD_SIZE, (chess.col + 0.74) * BOARD_SIZE, endStr);
}

是指定绘画某一个棋子

ChessFall:

void ChessFall() {
	mciSendString("open res/音效.wav", 0, 0, 0);
	mciSendString("play res/音效.wav wait", 0, 0, 0);
	mciSendString("close res/音效.wav", 0, 0, 0);
}

是音效播放的线程函数,这些都不难理解

initChessPiece:

void initChessPiece() {
	//兵
	Sleep(500);
	printChessBoard();
	for (ll i = 0; i < 5; i++) {
		Chess pawn_chess;
		pawn_chess.row = 2 * i;
		pawn_chess.col = 3;
		pawn_chess.player = BLACK_PLAYER;
		pawn_chess.chessPiece = PAWN;
		vecChess.push_back(pawn_chess);
	}
	for (ll i = 0; i < 5; i++) {
		Chess pawn_chess;
		pawn_chess.row = 2 * i;
		pawn_chess.col = 6;
		pawn_chess.player = RED_PLAYER;
		pawn_chess.chessPiece = PAWN;
		vecChess.push_back(pawn_chess);
	}
	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ChessFall, NULL, NULL, NULL);
	Sleep(600);
	printChessBoard();
	//炮
	for (ll i = 0; i < 2; i++) {
		Chess cannon_chess;
		cannon_chess.col = 2;
		cannon_chess.row = (i == 0 ? 1 : 7);
		cannon_chess.player = BLACK_PLAYER;
		cannon_chess.chessPiece = CANNON;
		vecChess.push_back(cannon_chess);
	}
	for (ll i = 0; i < 2; i++) {
		Chess cannon_chess;
		cannon_chess.col = 7;
		cannon_chess.row = (i == 0 ? 1 : 7);
		cannon_chess.player = RED_PLAYER;
		cannon_chess.chessPiece = CANNON;
		vecChess.push_back(cannon_chess);
	}
	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ChessFall, NULL, NULL, NULL);
	Sleep(500);
	printChessBoard();
	//车
	for (ll i = 0; i < 2; i++) {
		Chess rook_chess;
		rook_chess.col = 0;
		rook_chess.row = (i == 0 ? 0 : 8);
		rook_chess.player = BLACK_PLAYER;
		rook_chess.chessPiece = ROOK;
		vecChess.push_back(rook_chess);
	}
	for (ll i = 0; i < 2; i++) {
		Chess rook_chess;
		rook_chess.col = 9;
		rook_chess.row = (i == 0 ? 0 : 8);
		rook_chess.player = RED_PLAYER;
		rook_chess.chessPiece = ROOK;
		vecChess.push_back(rook_chess);
	}
	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ChessFall, NULL, NULL, NULL);
	Sleep(500);
	printChessBoard();
	//马
	for (ll i = 0; i < 2; i++) {
		Chess horse_chess;
		horse_chess.col = 0;
		horse_chess.row = (i == 0 ? 1 : 7);
		horse_chess.player = BLACK_PLAYER;
		horse_chess.chessPiece = HORSE;
		vecChess.push_back(horse_chess);
	}
	for (ll i = 0; i < 2; i++) {
		Chess horse_chess;
		horse_chess.col = 9;
		horse_chess.row = (i == 0 ? 1 : 7);
		horse_chess.player = RED_PLAYER;
		horse_chess.chessPiece = HORSE;
		vecChess.push_back(horse_chess);
	}
	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ChessFall, NULL, NULL, NULL);
	Sleep(500);
	printChessBoard();
	//象
	for (ll i = 0; i < 2; i++) {
		Chess elephant_chess;
		elephant_chess.col = 0;
		elephant_chess.row = (i == 0 ? 2 : 6);
		elephant_chess.player = BLACK_PLAYER;
		elephant_chess.chessPiece = ELEPHANT;
		vecChess.push_back(elephant_chess);
	}
	for (ll i = 0; i < 2; i++) {
		Chess elephant_chess;
		elephant_chess.col = 9;
		elephant_chess.row = (i == 0 ? 2 : 6);
		elephant_chess.player = RED_PLAYER;
		elephant_chess.chessPiece = ELEPHANT;
		vecChess.push_back(elephant_chess);
	}
	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ChessFall, NULL, NULL, NULL);
	Sleep(500);
	printChessBoard();
	//士
	for (ll i = 0; i < 2; i++) {
		Chess guard_chess;
		guard_chess.col = 0;
		guard_chess.row = (i == 0 ? 3 : 5);
		guard_chess.player = BLACK_PLAYER;
		guard_chess.chessPiece = GUARD;
		vecChess.push_back(guard_chess);
	}
	for (ll i = 0; i < 2; i++) {
		Chess guard_chess;
		guard_chess.col = 9;
		guard_chess.row = (i == 0 ? 3 : 5);
		guard_chess.player = RED_PLAYER;
		guard_chess.chessPiece = GUARD;
		vecChess.push_back(guard_chess);
	}
	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ChessFall, NULL, NULL, NULL);
	Sleep(500);
	printChessBoard();
	Chess general_chess;
	general_chess.col = 0;
	general_chess.row = 4;
	general_chess.player = BLACK_PLAYER;
	general_chess.chessPiece = GENERAL;
	vecChess.push_back(general_chess);
	general_chess.col = 9;
	general_chess.row = 4;
	general_chess.player = RED_PLAYER;
	general_chess.chessPiece = GENERAL;
	vecChess.push_back(general_chess);
	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ChessFall, NULL, NULL, NULL);
	Sleep(500);
	printChessBoard();
}

是初始化棋子,里面我每初始化一组同种类型的棋子,就重新打印棋盘,显示出这些棋子,并创建线程以播放音效(自己找吧)

chessMove:

bool chessMove(Chess chess) {
	printChessBoard();
	FlushBatchDraw();
	Chess newChess;
	newChess.row = newRow;
	newChess.col = newCol;
	newChess.player = chess.player;
	newChess.chessPiece = chess.chessPiece;
	for (auto iter = vecChess.begin(); iter != vecChess.end(); iter++) {
		if ((*iter).row == chess.row && (*iter).col == chess.col) {
			vecChess.erase(iter);
			vecChess.push_back(newChess);
			mciSendString("close res/音效.wav", 0, 0, 0);
			mciSendString("play res/音效.wav", 0, 0, 0);
			return 1;
		}
	}
	return 0;
}

是所有棋子移动的中心函数,但不难理解

pawnMove:

bool pawnMove(Chess chess) {
	if (chess.player == RED_PLAYER) {
		printChessBoard();
		FlushBatchDraw();
		if (newCol > chess.col) return 0;
		if (chess.col <= 4) {
			ll x = abs(chess.col - newCol) + abs(chess.row - newRow);
			if (x != 1) return 0;
		}
		else {
			ll x = abs(chess.col - newCol) + abs(chess.row - newRow);
			if (x != 1 || abs(chess.row - newRow) != 0) return 0;
		}
	}
	if (chess.player == BLACK_PLAYER) {
		printChessBoard();
		FlushBatchDraw();
		if (newCol < chess.col) return 0;
		if (chess.col >= 5) {
			ll x = abs(chess.col - newCol) + abs(chess.row - newRow);
			if (x != 1) return 0;
		}
		else {
			ll x = abs(chess.col - newCol) + abs(chess.row - newRow);
			if (x != 1 || abs(chess.row - newRow) != 0) return 0;
		}
	}
	return chessMove(chess);
}

cannonMove:

bool cannonMove(Chess chess) {
	if ((chess.row != newRow && chess.col != newCol) || (chess.row == newRow && chess.col == newCol)) return 0;
	ll startLocation = 0, endLocation = 0, rowCount = 0, colCount = 0;
	bool diffRow = false, diffCol = false;
	if (chess.row != newRow) {
		diffRow = 1;
		if (chess.row < newRow) startLocation = chess.row, endLocation = newRow;
		else startLocation = newRow, endLocation = chess.row;
	}
	else {
		diffCol = 1;
		if (chess.col < newCol) startLocation = chess.col, endLocation = newCol;
		else startLocation = newCol, endLocation = chess.col;
	}
	bool isChess = false;
	for (auto iter = vecChess.begin(); iter != vecChess.end(); iter++) {
		if (iter->row == newRow && iter->col == newCol) {
			isChess = true;
			if (iter->player == chess.player) return false;
		}
		if ((iter->col == newCol) && (iter->row > startLocation && iter->row < endLocation)) rowCount++;
		if ((iter->row == newRow) && (iter->col > startLocation && iter->col < endLocation)) colCount++;
	}
	if (isChess) {
		if (diffRow) if (rowCount != 1) return false;
		if (diffCol) if (colCount != 1) return false;
	}
	else {
		if (diffRow) if (rowCount != 0) return false;
		if (diffCol) if (colCount != 0) return false;
	}
	return chessMove(chess);
}
bool rookMo

rookMove:

bool rookMove(Chess chess) {
	if ((chess.row != newRow && chess.col != newCol) || (chess.row == newRow && chess.col == newCol)) return 0;
	ll startLocation = 0, endLocation = 0;
	bool diffRow = false, diffCol = false;
	if (chess.row != newRow) {
		diffRow = 1;
		if (chess.row < newRow) startLocation = chess.row, endLocation = newRow;
		else startLocation = newRow, endLocation = chess.row;
	}
	else {
		diffCol = 1;
		if (chess.col < newCol) startLocation = chess.col, endLocation = newCol;
		else startLocation = newCol, endLocation = chess.col;
	}
	bool isChess = false;
	for (auto iter = vecChess.begin(); iter != vecChess.end(); iter++) {
		if (diffRow && (iter->col == newCol && (iter->row > startLocation) && iter->row < endLocation)) return false;
		if (diffCol && (iter->row == newRow && (iter->col > startLocation) && iter->col < endLocation)) return false;
	}
	return chessMove(chess);
}

horseMove:

bool horseMove(Chess chess) {
	if ((chess.row - newRow) * (chess.col - newCol) != -2 && (chess.row - newRow) * (chess.col - newCol) != 2) {
		return false;
	}
	ll legRow = 0, legCol = 0;
	if (chess.row - newRow == 2 && chess.col - newCol == 1) {
		legRow = chess.row - 1;
		legCol = chess.col;
	}
	if (chess.row - newRow == 2 && chess.col - newCol == -1) {
		legRow = chess.row - 1;
		legCol = chess.col;
	}
	if (chess.row - newRow == -2 && chess.col - newCol == 1) {
		legRow = chess.row + 1;
		legCol = chess.col;
	}
	if (chess.row - newRow == -2 && chess.col - newCol == -1) {
		legRow = chess.row + 1;
		legCol = chess.col;
	}
	if (chess.row - newRow == 1 && chess.col - newCol == 2) {
		legRow = chess.row;
		legCol = chess.col - 1;
	}
	if (chess.row - newRow == -1 && chess.col - newCol == 2) {
		legRow = chess.row;
		legCol = chess.col - 1;
	}
	if (chess.row - newRow == 1 && chess.col - newCol == -2) {
		legRow = chess.row;
		legCol = chess.col + 1;
	}
	if (chess.row - newRow == -1 && chess.col - newCol == -2) {
		legRow = chess.row;
		legCol = chess.col + 1;
	}
	for (auto iter = vecChess.begin(); iter != vecChess.end(); iter++) {
		if (iter->col == legCol && iter->row == legRow) {
			return false;
		}
	}
	return chessMove(chess);
}

elephantMove:

bool elephantMove(Chess chess) {
	if ((chess.row - newRow) * (chess.col - newCol) != -4 && (chess.row - newRow) * (chess.col - newCol) != 4) return false;
	if (chess.player == RED_PLAYER && newCol <= 4) return false;
	if (chess.player == BLACK_PLAYER && newCol >= 5) return false;
	ll eyeRow = 0, eyeCol = 0;
	if (chess.row - newRow == 2 && chess.col - newCol == 2) {
		eyeRow = chess.row - 1, eyeCol = chess.col - 1;
	}
	if (chess.row - newRow == 2 && chess.col - newCol == -2) {
		eyeRow = chess.row - 1, eyeCol = chess.col + 1;
	}
	if (chess.row - newRow == -2 && chess.col - newCol == 2) {
		eyeRow = chess.row + 1, eyeCol = chess.col - 1;
	}
	if (chess.row - newRow == -2 && chess.col - newCol == -2) {
		eyeRow = chess.row + 1, eyeCol = chess.col + 1;
	}
	for (auto iter = vecChess.begin(); iter != vecChess.end(); iter++) {
		if (iter->row == eyeRow && iter->col == eyeCol) return false;
	}
	return chessMove(chess);
}

guardMove:

bool guardMove(Chess chess) {
	if ((chess.row - newRow) * (chess.col - newCol) != -1 && (chess.row - newRow) * (chess.col - newCol) != 1 || (chess.row == newRow && chess.col == newCol)) return false;
	if (chess.player == RED_PLAYER && (newRow < 3 || newRow > 5 || newCol < 7)) return false;
	if (chess.player == BLACK_PLAYER && (newRow < 3 || newRow > 5 || newCol > 2)) return false;
	return chessMove(chess);
}

generalMove:

bool generalMove(Chess chess) {
	if (((chess.row - newRow) + (chess.col - newCol) != -1 && (chess.row - newRow) + (chess.col - newCol) != 1) || (chess.row != newRow && chess.col != newCol) || (chess.row == newRow && chess.col == newCol)) return false;
	if (chess.player == RED_PLAYER && (newRow < 3 || newRow > 5 || newCol < 7)) return false;
	if (chess.player == BLACK_PLAYER && (newRow < 3 || newRow > 5 || newCol > 2)) return false;
	if (chess.player == RED_PLAYER) {
		redGeneralRow = newRow;
		redGeneralCol = newCol;
	}
	else {
		blackGeneralRow = newRow;
		blackGeneralCol = newCol;
	}
	return chessMove(chess);
}

这些都是棋子移动函数,简(nan)单(ru)易(deng)懂(tian),不(wo)必(tai)解(lan)释(le)

getChessInfoByLocation:

chessInfo getChessInfoByLocation(ll x, ll y, selectTime time) {
	if (x % BOARD_SIZE > CHESSPEICE_SIZE && x % BOARD_SIZE < BOARD_SIZE - CHESSPEICE_SIZE || y % BOARD_SIZE > CHESSPEICE_SIZE && y % BOARD_SIZE < BOARD_SIZE - CHESSPEICE_SIZE || x < BOARD_SIZE - CHESSPEICE_SIZE || x > BOARD_ROW * BOARD_SIZE + CHESSPEICE_SIZE || y < BOARD_SIZE - CHESSPEICE_SIZE || y>BOARD_COL * BOARD_SIZE + CHESSPEICE_SIZE) return ERROR_LOCATION;
	newRow = (x + CHESSPEICE_SIZE - BOARD_SIZE) / BOARD_SIZE;
	newCol = (y + CHESSPEICE_SIZE - BOARD_SIZE) / BOARD_SIZE;
	for (ll i = 0; i < vecChess.size(); i++) {
		if (vecChess[i].row == newRow && vecChess[i].col == newCol) {
			if (time == FIRST_TIME) {
				selectedChess = vecChess[i];
				if (vecChess[i].player == RED_PLAYER) return RED_CHESS;
				else return BLACK_CHESS;
			}
			else if (time == SECOND_TIME) {
				lastSelectedChess = selectedChess;
				selectedChess = vecChess[i];
				if (vecChess[i].player == RED_PLAYER) return RED_CHESS;
				else return BLACK_CHESS;
			}
		}
	}
	printChessBoard();
	FlushBatchDraw();
	return EMPTY_LOCATION;
}

比较复杂,主要为判断选择的次数和是吃子还是移动,还是重选,大家可以多看几遍

moveToEmptyLocation:

bool moveToEmptyLocation() {
	bool isSuccessMove = 0;
	printChessBoard();
	FlushBatchDraw();
	if (selectedChess.chessPiece == PAWN) {
		isSuccessMove = pawnMove(selectedChess);
	}
	else if (selectedChess.chessPiece == CANNON) {
		isSuccessMove = cannonMove(selectedChess);
	}
	else if (selectedChess.chessPiece == ROOK) {
		isSuccessMove = rookMove(selectedChess);
	}
	else if (selectedChess.chessPiece == HORSE) {
		isSuccessMove = horseMove(selectedChess);
	}
	else if (selectedChess.chessPiece == ELEPHANT) {
		isSuccessMove = elephantMove(selectedChess);
	}
	else if (selectedChess.chessPiece == GUARD) {
		isSuccessMove = guardMove(selectedChess);
	}
	else if (selectedChess.chessPiece == GENERAL) {
		isSuccessMove = generalMove(selectedChess);
	}
	if (isSuccessMove) {
		printChessBoard();
		FlushBatchDraw();
		lastSelectedChess = { -1,-1 };
	}
	return isSuccessMove;
}

移动到空位置的棋子的判断函数

reselectChess:

void reselectChess() {
	printChessBoard();
	FlushBatchDraw();
	lastSelectedChess = { -1,-1 };
}

重新选择棋子,这是最好理解的了

gameEndShow:

void gameEndShow(Player player) {
	printChessBoard();
	clearrectangle(0, 0, (BOARD_ROW + 1) * BOARD_SIZE, (BOARD_COL + 1) * BOARD_SIZE);
	cleardevice();
	FlushBatchDraw();
	TCHAR endStr[30];
	if (player == RED_PLAYER) {
		settextcolor(RED);
		_stprintf_s(endStr, "红方胜利");
	}
	else {
		settextcolor(BLACK);
		_stprintf_s(endStr, "黑方胜利");
	}
	outtextxy(((BOARD_ROW + 1) * BOARD_SIZE-textwidth(endStr))/2, ((BOARD_COL + 1) * BOARD_SIZE-textheight(endStr))/2, endStr);
	FlushBatchDraw();
	Sleep(3000);
	EndBatchDraw();
	closegraph();
	exit(0);
}

游戏结束界面

capture:

bool capture() {
	bool isCapture = false;
	if (lastSelectedChess.chessPiece == PAWN) {
		isCapture = pawnMove(lastSelectedChess);
	}
	else if (lastSelectedChess.chessPiece == CANNON) {
		isCapture = cannonMove(lastSelectedChess);
	}
	else if (lastSelectedChess.chessPiece == ROOK) {
		isCapture = rookMove(lastSelectedChess);
	}
	else if (lastSelectedChess.chessPiece == HORSE) {
		isCapture = horseMove(lastSelectedChess);
	}
	else if (lastSelectedChess.chessPiece == ELEPHANT) {
		isCapture = elephantMove(lastSelectedChess);
	}
	else if (lastSelectedChess.chessPiece == GUARD) {
		isCapture = guardMove(lastSelectedChess);
	}
	else if (lastSelectedChess.chessPiece == GENERAL) {
		isCapture = generalMove(lastSelectedChess);
	}
	if (isCapture) {
		for (auto iter = vecChess.begin(); iter != vecChess.end(); iter++) {
			if (iter->row == selectedChess.row && iter->col == selectedChess.col && iter->player == selectedChess.player && iter->chessPiece == selectedChess.chessPiece) {
				if (iter->chessPiece == GENERAL) {
					gameEndShow((iter->player == RED_PLAYER ? BLACK_PLAYER : RED_PLAYER));
				}
				vecChess.erase(iter);
				lastSelectedChess = { -1,-1 };
				selectedChess = { -1,-1 };
				printChessBoard();
				FlushBatchDraw();
				break;
			}
		}
	}
	else {
		selectedChess = lastSelectedChess;
		lastSelectedChess = { -1,-1 };
	}
	return isCapture;
}

吃子

isGameOver:

bool isGameOver() {
	if (redGeneralRow == blackGeneralRow) {
		int cnt = 0, krow = redGeneralRow;
		for (auto iter = vecChess.begin(); iter != vecChess.end(); iter++) {
			if (iter->row == krow && iter->col > blackGeneralCol && iter->col < redGeneralCol) {
				cnt++;
			}
		}
		if (cnt) return 0;
		else return 1;
	}
	return 0;
}

判断将对脸

main:

int main() {
	HWND hwnd = initgraph((BOARD_ROW + 1) * BOARD_SIZE, (BOARD_COL + 1) * BOARD_SIZE);
	setbkcolor(RGB(211, 177, 125));
	cleardevice();
	mciSendString("open res/落子.mp3", 0, 0, 0);
	BeginBatchDraw();
	ZhongGuoXiangQi();
	initChessPiece();
	while (1) {
		cleardevice();
		ExMessage msg_select = { 0 };
		getmessage(&msg_select, EX_MOUSE);
		printChessBoard();
		FlushBatchDraw();
		if (msg_select.message == WM_LBUTTONUP) {
			chessInfo selected = getChessInfoByLocation(msg_select.x, msg_select.y, FIRST_TIME);
			printChessBoard();
			if (player == selectedChess.player) {
				drawChessPiece(selectedChess);
				printChessBoard();
				FlushBatchDraw();
				bool isSuccessMove = false;
				while (!isSuccessMove) {
					getmessage(&msg_select, EX_MOUSE);
					if (msg_select.message != WM_LBUTTONUP) continue;
					chessInfo move = getChessInfoByLocation(msg_select.x, msg_select.y, SECOND_TIME);
					if (move == EMPTY_LOCATION) {
						if (moveToEmptyLocation()) {
							printChessBoard();
							FlushBatchDraw();
							isSuccessMove = true;
							player = (player == RED_PLAYER ? BLACK_PLAYER : RED_PLAYER);
							if (isGameOver()) {
								Sleep(1500);
								gameEndShow(player);
							}
						}
					}
					else if (move == RED_CHESS || move == BLACK_CHESS) {
						printChessBoard();
						FlushBatchDraw();
						if ((move == RED_CHESS && player == RED_PLAYER) || (move == BLACK_CHESS && player == BLACK_PLAYER)) {
							printChessBoard();
							FlushBatchDraw();
							reselectChess();
						}
						else {
							if (capture()) {
								player = (player == RED_PLAYER ? BLACK_PLAYER : RED_PLAYER);
								if (isGameOver()) {
									Sleep(1500);
									gameEndShow(player);
								}
							}
							printChessBoard();
							FlushBatchDraw();
						}
					}
				}
			}
		}
		printChessBoard();
		FlushBatchDraw();
	}
	return 0;
}

包含鼠标点击等函数运行处

编码不易,求三连

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

C++小盆友

你的鼓励将是我创作的最大动力呦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值