跳到正文

休闲·娱乐

国际象棋WindowsAPI(转载 来自 @liangyiha)

oukaiming02约 45 分钟
原文
文章信息与版本

文章信息

发布
2026年8月3日
收录
2026年8月3日
更新
2026年11月14日
篇幅
约 45 分钟

版本

  1. v1当前
C++
//前置:在 工具 -> 编译选项 -> 编译器 加入 -lgdi32 
#define UNICODE
#define _UNICODE
#include <windows.h>
#include <windowsx.h>
#include <cstring>
#include <cmath>

#define BOARD_SIZE 8
#define CELL_SIZE 72

enum PieceType { EMPTY, PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING };
enum Color { NO_COLOR, WHITE, BLACK };

struct Piece {
	PieceType type;
	Color color;
	bool hasMoved;
	Piece() : type(EMPTY), color(NO_COLOR), hasMoved(false) {}
	Piece(PieceType t, Color c, bool m = false) : type(t), color(c), hasMoved(m) {}
};

struct Move {
	int fr, fc, tr, tc;
	Piece captured;
	bool enPassant;
	bool castling;
	bool promotion;
	PieceType promoType;
	bool kingMoved;
	bool rookMoved;

	// 无参默认构造
	Move() 
		: fr(-1), fc(-1), tr(-1), tc(-1), captured(Piece()), enPassant(false), castling(false),
		promotion(false), promoType(EMPTY), kingMoved(false), rookMoved(false)
	{}

	// 基础移动构造(起点、终点、被俘棋子)
	Move(int f_r, int f_c, int t_r, int t_c, Piece cap)
		: fr(f_r), fc(f_c), tr(t_r), tc(t_c), captured(cap), enPassant(false), castling(false),
		promotion(false), promoType(EMPTY), kingMoved(false), rookMoved(false)
	{}

	// 带吃过路兵/升变的构造
	Move(int f_r, int f_c, int t_r, int t_c, Piece cap, bool ep, bool castle, bool promo)
		: fr(f_r), fc(f_c), tr(t_r), tc(t_c), captured(cap), enPassant(ep), castling(castle),
		promotion(promo), promoType(EMPTY), kingMoved(false), rookMoved(false)
	{}
};

static Piece board[BOARD_SIZE][BOARD_SIZE];
static Color player = WHITE;
static POINT sel = { -1, -1 };
static Move history[1000];
static int hCount = 0;
static int epRow = -1, epCol = -1;
static bool gameOver = false;
static Color winner = NO_COLOR;

static int halfMoveClock = 0;
static char positionHash[1000][64];

enum Promo { NONE, WAIT };
static Promo promo = NONE;
static Move pendPromo;

// ================ 函数原型 ================
static bool In(int r, int c);
static bool Empty(int r, int c);
static bool Enemy(int r, int c, Color col);
static void Set(int r, int c, PieceType t, Color col);
static void Init();
static bool Attacked(int r, int c, Color by);
static bool InCheck(Color col);
static int Pseudo(int r, int c, Move m[]);
static bool LeavesCheck(Move m);
static int Legal(int r, int c, Move m[]);
static bool AnyLegal();
static void DoMove(Move m);
static void Undo();
static void SavePosition();
static bool CheckRepetition();
static void DrawPiece(HDC hdc, int x, int y, Piece p);
static void Draw(HDC hdc);
static void Click(HWND hwnd, int x, int y);
static void Key(WPARAM k);
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp);

// ================ 工具 ================
static bool In(int r, int c) {
	return r >= 0 && r < 8 && c >= 0 && c < 8;
}
static bool Empty(int r, int c) {
	return In(r, c) && board[r][c].type == EMPTY;
}
static bool Enemy(int r, int c, Color col) {
	return In(r, c) && board[r][c].type != EMPTY && board[r][c].color != col;
}
static void Set(int r, int c, PieceType t, Color col) {
	board[r][c].type = t;
	board[r][c].color = col;
	board[r][c].hasMoved = false;
}

// ================ 初始化 ================
static void Init() {
	memset(board, 0, sizeof(board));
	hCount = 0;
	epRow = -1;
	epCol = -1;
	player = WHITE;
	gameOver = false;
	winner = NO_COLOR;
	sel.x = -1; sel.y = -1;
	promo = NONE;
	halfMoveClock = 0;
	memset(positionHash, 0, sizeof(positionHash));

	Set(0, 0, ROOK, BLACK);
	Set(0, 1, KNIGHT, BLACK);
	Set(0, 2, BISHOP, BLACK);
	Set(0, 3, QUEEN, BLACK);
	Set(0, 4, KING, BLACK);
	Set(0, 5, BISHOP, BLACK);
	Set(0, 6, KNIGHT, BLACK);
	Set(0, 7, ROOK, BLACK);
	for (int i = 0; i < 8; i++) Set(1, i, PAWN, BLACK);
	for (int i = 0; i < 8; i++) Set(6, i, PAWN, WHITE);
	Set(7, 0, ROOK, WHITE);
	Set(7, 1, KNIGHT, WHITE);
	Set(7, 2, BISHOP, WHITE);
	Set(7, 3, QUEEN, WHITE);
	Set(7, 4, KING, WHITE);
	Set(7, 5, BISHOP, WHITE);
	Set(7, 6, KNIGHT, WHITE);
	Set(7, 7, ROOK, WHITE);

	SavePosition();
}

// ================ 攻击检测 ================
static bool Attacked(int r, int c, Color by) {
	int d = (by == WHITE) ? -1 : 1;
	if (In(r + d, c - 1) && board[r + d][c - 1].type == PAWN && board[r + d][c - 1].color == by) return true;
	if (In(r + d, c + 1) && board[r + d][c + 1].type == PAWN && board[r + d][c + 1].color == by) return true;

	int km[8][2] = { {-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1} };
	for (int i = 0; i < 8; i++) {
		int nr = r + km[i][0], nc = c + km[i][1];
		if (In(nr, nc) && board[nr][nc].type == KNIGHT && board[nr][nc].color == by) return true;
	}

	int dirs[8][2] = { {-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1} };
	for (int d = 0; d < 8; d++) {
		for (int i = 1; i < 8; i++) {
			int nr = r + i * dirs[d][0], nc = c + i * dirs[d][1];
			if (!In(nr, nc)) break;
			if (!Empty(nr, nc)) {
				if (board[nr][nc].color == by) {
					PieceType t = board[nr][nc].type;
					if (d < 4 && (t == ROOK || t == QUEEN)) return true;
					if (d >= 4 && (t == BISHOP || t == QUEEN)) return true;
				}
				break;
			}
		}
	}
	return false;
}

static bool InCheck(Color col) {
	for (int r = 0; r < 8; r++)
		for (int c = 0; c < 8; c++)
			if (board[r][c].type == KING && board[r][c].color == col)
				return Attacked(r, c, col == WHITE ? BLACK : WHITE);
	return false;
}

// ================ 伪合法走法(全部改用构造函数,删除{}赋值) ================
static int Pseudo(int r, int c, Move m[]) {
	int cnt = 0;
	Piece p = board[r][c];
	if (p.type == EMPTY) return 0;

	if (p.type == PAWN) {
		int d = (p.color == WHITE) ? -1 : 1;
		int start = (p.color == WHITE) ? 6 : 1;
		if (In(r + d, c) && Empty(r + d, c)) {
			bool isPromo = (r + d == 0 || r + d == 7);
			m[cnt++] = Move(r, c, r + d, c, board[r + d][c], false, false, isPromo);
			if (r == start && Empty(r + 2 * d, c))
				m[cnt++] = Move(r, c, r + 2 * d, c, board[r + 2 * d][c]);
		}
		for (int dc = -1; dc <= 1; dc += 2) {
			int nr = r + d, nc = c + dc;
			if (In(nr, nc) && Enemy(nr, nc, p.color))
				m[cnt++] = Move(r, c, nr, nc, board[nr][nc], false, false, (nr == 0 || nr == 7));
		}
		return cnt;
	}

	if (p.type == KNIGHT) {
		int km[8][2] = { {-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1} };
		for (int i = 0; i < 8; i++) {
			int nr = r + km[i][0], nc = c + km[i][1];
			if (In(nr, nc) && (Empty(nr, nc) || Enemy(nr, nc, p.color)))
				m[cnt++] = Move(r, c, nr, nc, board[nr][nc]);
		}
		return cnt;
	}

	if (p.type == KING) {
		int dirs[8][2] = { {-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1} };
		for (int d = 0; d < 8; d++) {
			int nr = r + dirs[d][0], nc = c + dirs[d][1];
			if (In(nr, nc) && (Empty(nr, nc) || Enemy(nr, nc, p.color)))
				m[cnt++] = Move(r, c, nr, nc, board[nr][nc]);
		}
		return cnt;
	}

	int s = 0, e = 8;
	if (p.type == ROOK) e = 4;
	if (p.type == BISHOP) s = 4;
	int dirs[8][2] = { {-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1} };
	for (int d = s; d < e; d++) {
		for (int i = 1; i < 8; i++) {
			int nr = r + i * dirs[d][0], nc = c + i * dirs[d][1];
			if (!In(nr, nc)) break;
			if (Empty(nr, nc))
				m[cnt++] = Move(r, c, nr, nc, board[nr][nc]);
			else {
				if (Enemy(nr, nc, p.color))
					m[cnt++] = Move(r, c, nr, nc, board[nr][nc]);
				break;
			}
		}
	}
	return cnt;
}

// ================ 是否离开将军(修复过路兵坐标) ================
static bool LeavesCheck(Move m) {
	Piece src = board[m.fr][m.fc];
	Piece dst = board[m.tr][m.tc];
	Piece ep;

	board[m.tr][m.tc] = src;
	board[m.fr][m.fc] = Piece();

	if (m.enPassant) {
		int eatR = (src.color == WHITE) ? (m.tr + 1) : (m.tr - 1);
		ep = board[eatR][m.tc];
		board[eatR][m.tc] = Piece();
	}
	Piece rookSave;
	if (m.castling) {
		int rf = (m.tc > m.fc) ? 7 : 0, rt = (m.tc > m.fc) ? 5 : 3;
		rookSave = board[m.tr][rf];
		board[m.tr][rt] = rookSave;
		board[m.tr][rf] = Piece();
	}

	bool bad = InCheck(src.color);

	board[m.fr][m.fc] = src;
	board[m.tr][m.tc] = dst;
	if (m.enPassant) {
		int eatR = (src.color == WHITE) ? (m.tr + 1) : (m.tr - 1);
		board[eatR][m.tc] = ep;
	}
	if (m.castling) {
		int rf = (m.tc > m.fc) ? 7 : 0, rt = (m.tc > m.fc) ? 5 : 3;
		board[m.tr][rf] = rookSave;
		board[m.tr][rt] = Piece();
	}
	return bad;
}

// ================ 合法走法(王车易位改用构造函数) ================
static int Legal(int r, int c, Move m[]) {
	int cnt = 0;
	if (board[r][c].color != player) return 0;
	Move pm[100];
	int pc = Pseudo(r, c, pm);
	for (int i = 0; i < pc; i++)
		if (!LeavesCheck(pm[i]))
			m[cnt++] = pm[i];

	Piece p = board[r][c];
	Color enemyColor = (player == WHITE) ? BLACK : WHITE;
	if (p.type == KING && !p.hasMoved && !InCheck(player) && c == 4) {
		// 短易位
		Piece& rookShort = board[r][7];
		if (!rookShort.hasMoved && Empty(r, 5) && Empty(r, 6)) {
			if (!Attacked(r, 5, enemyColor) && !Attacked(r, 6, enemyColor)) {
				Move castle(r, c, r, 6, board[r][6], false, true, false);
				if (!LeavesCheck(castle)) m[cnt++] = castle;
			}
		}
		// 长易位
		Piece& rookLong = board[r][0];
		if (!rookLong.hasMoved && Empty(r, 1) && Empty(r, 2) && Empty(r, 3)) {
			if (!Attacked(r, 2, enemyColor) && !Attacked(r, 3, enemyColor)) {
				Move castle(r, c, r, 2, board[r][2], false, true, false);
				if (!LeavesCheck(castle)) m[cnt++] = castle;
			}
		}
	}

	if (p.type == PAWN) {
		int d = (player == WHITE) ? -1 : 1;
		if (epRow == r + d && (epCol == c - 1 || epCol == c + 1)) {
			bool promoFlag = (epRow == 0 || epRow == 7);
			Move epm(r, c, epRow, epCol, board[r][epCol], true, false, promoFlag);
			if (!LeavesCheck(epm)) m[cnt++] = epm;
		}
	}
	return cnt;
}

// ================ 是否有合法走法 ================
static bool AnyLegal() {
	Move m[100];
	for (int r = 0; r < 8; r++)
		for (int c = 0; c < 8; c++)
			if (board[r][c].color == player)
				if (Legal(r, c, m) > 0) return true;
	return false;
}

// ================ 保存局面 ================
static void SavePosition() {
	for (int r = 0; r < 8; r++)
		for (int c = 0; c < 8; c++)
			positionHash[hCount][r * 8 + c] = (char)((board[r][c].type << 4) | board[r][c].color);
}

// ================ 检测三次重复 ================
static bool CheckRepetition() {
	int count = 0;
	for (int i = 0; i <= hCount; i++)
		if (memcmp(positionHash[i], positionHash[hCount], 64) == 0)
			if (++count >= 3) return true;
	return false;
}

// ================ 执行走棋 ================
static void DoMove(Move m) {
	Move h = m;
	h.kingMoved = board[m.fr][m.fc].hasMoved;
	if (m.castling) {
		int rc = (m.tc > m.fc) ? 7 : 0;
		h.rookMoved = board[m.fr][rc].hasMoved;
	}
	history[hCount++] = h;
	epRow = -1;

	bool pawnMove = (board[m.fr][m.fc].type == PAWN);
	bool capture = (m.captured.type != EMPTY) || m.enPassant;
	if (pawnMove || capture) halfMoveClock = 0;
	else halfMoveClock++;

	board[m.tr][m.tc] = board[m.fr][m.fc];
	board[m.fr][m.fc] = Piece();
	board[m.tr][m.tc].hasMoved = true;

	if (board[m.tr][m.tc].type == PAWN) {
		int d = (player == WHITE) ? -1 : 1;
		if (abs(m.tr - m.fr) == 2) {
			epRow = m.tr;
			epCol = m.fc;
		}
		if (m.promotion) board[m.tr][m.tc].type = m.promoType;
	}

	if (m.enPassant) {
		int eatR = (player == WHITE) ? (m.tr + 1) : (m.tr - 1);
		board[eatR][m.tc] = Piece();
	}

	if (m.castling) {
		if (m.tc > m.fc) {
			board[m.tr][5] = board[m.tr][7];
			board[m.tr][7] = Piece();
			board[m.tr][5].hasMoved = true;
		}
		else {
			board[m.tr][3] = board[m.tr][0];
			board[m.tr][0] = Piece();
			board[m.tr][3].hasMoved = true;
		}
	}

	player = (player == WHITE) ? BLACK : WHITE;
	SavePosition();

	if (InCheck(player) && !AnyLegal()) {
		gameOver = true;
		winner = (player == WHITE) ? BLACK : WHITE;
	}
	else if (!InCheck(player) && !AnyLegal()) {
		gameOver = true;
		winner = NO_COLOR;
	}
	else if (halfMoveClock >= 100) {
		gameOver = true;
		winner = NO_COLOR;
	}
	else if (CheckRepetition()) {
		gameOver = true;
		winner = NO_COLOR;
	}
}

// ================ 悔棋 ================
static void Undo() {
	if (hCount == 0) return;
	Move lm = history[--hCount];

	board[lm.fr][lm.fc] = board[lm.tr][lm.tc];
	board[lm.fr][lm.fc].hasMoved = lm.kingMoved;
	board[lm.tr][lm.tc] = lm.captured;

	if (lm.enPassant) {
		int eatR = (board[lm.fr][lm.fc].color == WHITE) ? (lm.tr + 1) : (lm.tr - 1);
		board[eatR][lm.tc] = lm.captured;
	}

	if (lm.castling) {
		int rc = (lm.tc > lm.fc) ? 7 : 0, rt = (lm.tc > lm.fc) ? 5 : 3;
		board[lm.tr][rc].type = ROOK;
		board[lm.tr][rc].color = board[lm.fr][lm.fc].color;
		board[lm.tr][rc].hasMoved = lm.rookMoved;
		board[lm.tr][rt] = Piece();
	}

	player = (player == WHITE) ? BLACK : WHITE;
	gameOver = false;
	winner = NO_COLOR;
	epRow = -1;
	if (hCount > 0 && history[hCount - 1].enPassant) {
		epRow = history[hCount - 1].tr;
		epCol = history[hCount - 1].tc;
	}
	halfMoveClock = 0;
	memset(positionHash[hCount], 0, sizeof(positionHash[0]));
}

// ================ 绘制棋子(所有数值强制转LONG,消除窄化警告) ================
static void DrawPiece(HDC hdc, int x, int y, Piece p) {
	HPEN pen = CreatePen(PS_SOLID, 2, RGB(0, 0, 0));
	HBRUSH br = CreateSolidBrush(p.color == WHITE ? RGB(250, 248, 242) : RGB(38, 38, 42));
	HPEN oldPen = (HPEN)SelectObject(hdc, pen);
	HBRUSH oldBr = (HBRUSH)SelectObject(hdc, br);

	LONG cx = (LONG)x + CELL_SIZE / 2;
	LONG cy = (LONG)y + CELL_SIZE / 2;
	LONG r = (LONG)(CELL_SIZE * 10.0 / 26.0);

	switch (p.type) {
	case PAWN: {
		Ellipse(hdc, cx - (LONG)(r * 0.8), cy + (LONG)(r * 0.3), cx + (LONG)(r * 0.8), cy + (LONG)(r * 0.9));
		POINT body[3] = {
			{cx - (LONG)(r * 0.5), cy + (LONG)(r * 0.3)},
			{cx + (LONG)(r * 0.5), cy + (LONG)(r * 0.3)},
			{cx, cy - (LONG)(r * 0.5)}
		};
		Polygon(hdc, body, 3);
		Ellipse(hdc, cx - (LONG)(r * 0.18), cy - (LONG)(r * 0.55), cx + (LONG)(r * 0.18), cy - (LONG)(r * 0.19));
		break;
	}
	case ROOK: {
		Rectangle(hdc, cx - (LONG)(r * 0.75), cy - (LONG)(r * 0.5), cx + (LONG)(r * 0.75), cy + (LONG)(r * 0.9));
		for (int i = -1; i <= 1; i += 2)
			Rectangle(hdc, cx + (LONG)(i * r * 0.375) - (LONG)(r * 0.22), cy - (LONG)(r * 0.75),
				cx + (LONG)(i * r * 0.375) + (LONG)(r * 0.22), cy - (LONG)(r * 0.47));
		Rectangle(hdc, cx - (LONG)(r * 0.78), cy - (LONG)(r * 0.52), cx + (LONG)(r * 0.78), cy - (LONG)(r * 0.43));
		break;
	}
	case KNIGHT: {
		POINT head[7] = {
			{cx - (LONG)(r * 0.7), cy + (LONG)(r * 0.4)},
			{cx - (LONG)(r * 0.95), cy - (LONG)(r * 0.05)},
			{cx - (LONG)(r * 0.6), cy - (LONG)(r * 0.5)},
			{cx - (LONG)(r * 0.1), cy - (LONG)(r * 0.82)},
			{cx + (LONG)(r * 0.35), cy - (LONG)(r * 0.62)},
			{cx + (LONG)(r * 0.58), cy - (LONG)(r * 0.08)},
			{cx + (LONG)(r * 0.02), cy + (LONG)(r * 0.33)}
		};
		Polygon(hdc, head, 7);
		POINT ear[3] = {
			{cx - (LONG)(r * 0.06), cy - (LONG)(r * 0.84)},
			{cx + (LONG)(r * 0.14), cy - (LONG)(r * 1.05)},
			{cx + (LONG)(r * 0.34), cy - (LONG)(r * 0.82)}
		};
		Polygon(hdc, ear, 3);
		HBRUSH eye = CreateSolidBrush(RGB(0, 0, 0));
		SelectObject(hdc, eye);
		Ellipse(hdc, cx - (LONG)(r * 0.03), cy - (LONG)(r * 0.53), cx + (LONG)(r * 0.13), cy - (LONG)(r * 0.37));
		DeleteObject(eye);
		SelectObject(hdc, oldBr);
		break;
	}
	case BISHOP: {
		POINT hat[3] = {
			{cx - (LONG)(r * 0.92), cy + (LONG)(r * 0.44)},
			{cx + (LONG)(r * 0.88), cy + (LONG)(r * 0.46)},
			{cx, cy - (LONG)(r * 0.76)}
		};
		Polygon(hdc, hat, 3);
		Ellipse(hdc, cx - (LONG)(r * 0.98), cy + (LONG)(r * 0.31), cx + (LONG)(r * 0.96), cy + (LONG)(r * 0.58));
		Ellipse(hdc, cx - (LONG)(r * 0.16), cy - (LONG)(r * 0.82), cx + (LONG)(r * 0.16), cy - (LONG)(r * 0.54));
		POINT robe[4] = {
			{cx - (LONG)(r * 0.77), cy + (LONG)(r * 0.57)},
			{cx + (LONG)(r * 0.73), cy + (LONG)(r * 0.56)},
			{cx + (LONG)(r * 0.93), cy + (LONG)(r * 0.91)},
			{cx - (LONG)(r * 0.95), cy + (LONG)(r * 0.90)}
		};
		Polygon(hdc, robe, 4);
		break;
	}
	case QUEEN: {
		POINT crown[5] = {
			{cx - (LONG)(r * 0.95), cy + (LONG)(r * 0.22)},
			{cx - (LONG)(r * 0.65), cy - (LONG)(r * 0.42)},
			{cx, cy - (LONG)(r * 0.82)},
			{cx + (LONG)(r * 0.63), cy - (LONG)(r * 0.41)},
			{cx + (LONG)(r * 0.94), cy + (LONG)(r * 0.23)}
		};
		Polygon(hdc, crown, 5);
		POINT s1[3] = {
			{cx - (LONG)(r * 0.49), cy - (LONG)(r * 0.29)},
			{cx - (LONG)(r * 0.35), cy - (LONG)(r * 0.60)},
			{cx - (LONG)(r * 0.21), cy - (LONG)(r * 0.30)}
		};
		POINT s2[3] = {
			{cx + (LONG)(r * 0.04), cy - (LONG)(r * 0.40)},
			{cx + (LONG)(r * 0.01), cy - (LONG)(r * 0.81)},
			{cx - (LONG)(r * 0.09), cy - (LONG)(r * 0.39)}
		};
		POINT s3[3] = {
			{cx + (LONG)(r * 0.20), cy - (LONG)(r * 0.30)},
			{cx + (LONG)(r * 0.34), cy - (LONG)(r * 0.61)},
			{cx + (LONG)(r * 0.51), cy - (LONG)(r * 0.30)}
		};
		Polygon(hdc, s1, 3);
		Polygon(hdc, s2, 3);
		Polygon(hdc, s3, 3);
		POINT robe[4] = {
			{cx - (LONG)(r * 0.83), cy + (LONG)(r * 0.35)},
			{cx + (LONG)(r * 0.81), cy + (LONG)(r * 0.36)},
			{cx + (LONG)(r * 0.97), cy + (LONG)(r * 0.93)},
			{cx - (LONG)(r * 0.99), cy + (LONG)(r * 0.92)}
		};
		Polygon(hdc, robe, 4);
		break;
	}
	case KING: {
		Rectangle(hdc, cx - (LONG)(r * 0.12), cy - (LONG)(r * 0.95), cx + (LONG)(r * 0.12), cy + (LONG)(r * 0.70));
		Rectangle(hdc, cx - (LONG)(r * 0.65), cy - (LONG)(r * 0.15), cx + (LONG)(r * 0.65), cy + (LONG)(r * 0.05));
		POINT base[4] = {
			{cx - (LONG)(r * 0.80), cy - (LONG)(r * 0.05)},
			{cx + (LONG)(r * 0.80), cy - (LONG)(r * 0.05)},
			{cx + (LONG)(r * 0.90), cy + (LONG)(r * 0.25)},
			{cx - (LONG)(r * 0.90), cy + (LONG)(r * 0.25)}
		};
		Polygon(hdc, base, 4);
		POINT tip[3] = {
			{cx - (LONG)(r * 0.50), cy - (LONG)(r * 0.65)},
			{cx, cy - (LONG)(r * 0.65)},
			{cx + (LONG)(r * 0.50), cy - (LONG)(r * 0.15)}
		};
		Polygon(hdc, tip, 3);
		POINT cape[4] = {
			{cx - (LONG)(r * 0.75), cy + (LONG)(r * 0.30)},
			{cx + (LONG)(r * 0.75), cy + (LONG)(r * 0.30)},
			{cx + (LONG)(r * 0.85), cy + (LONG)(r * 0.85)},
			{cx - (LONG)(r * 0.85), cy + (LONG)(r * 0.85)}
		};
		Polygon(hdc, cape, 4);
		Ellipse(hdc, cx - (LONG)(r * 0.70), cy + (LONG)(r * 0.65), cx + (LONG)(r * 0.70), cy + (LONG)(r * 0.92));
		break;
	}
	default:
		break;
	}

	SelectObject(hdc, oldPen);
	SelectObject(hdc, oldBr);
	DeleteObject(pen);
	DeleteObject(br);
}

// ================ 绘制主界面 ================
static void Draw(HDC hdc) {
	for (int r = 0; r < 8; r++) {
		for (int c = 0; c < 8; c++) {
			RECT rc = { c * CELL_SIZE,r * CELL_SIZE,(c + 1) * CELL_SIZE,(r + 1) * CELL_SIZE };
			COLORREF clr = ((r + c) % 2 == 0) ? RGB(238, 215, 173) : RGB(184, 139, 74);
			if (sel.x == c && sel.y == r) clr = RGB(130, 210, 130);
			if (hCount > 0) {
				Move lm = history[hCount - 1];
				if ((r == lm.fr && c == lm.fc) || (r == lm.tr && c == lm.tc))
					clr = RGB(220, 220, 110);
			}
			HBRUSH b = CreateSolidBrush(clr);
			FillRect(hdc, &rc, b);
			DeleteObject(b);
		}
	}

	for (int r = 0; r < 8; r++)
		for (int c = 0; c < 8; c++)
			if (board[r][c].type != EMPTY)
				DrawPiece(hdc, c * CELL_SIZE, r * CELL_SIZE, board[r][c]);

	// 状态栏
	RECT st = { 0,CELL_SIZE * 8,CELL_SIZE * 8,CELL_SIZE * 8 + 28 };
	HBRUSH sb = CreateSolidBrush(RGB(45, 45, 48));
	FillRect(hdc, &st, sb);
	DeleteObject(sb);
	SetTextColor(hdc, RGB(255, 255, 255));
	SetBkMode(hdc, TRANSPARENT);
	wchar_t txt[256];
	if (gameOver) {
		if (winner == NO_COLOR)
			wsprintfW(txt, L"Draw! (Stalemate / 50-move / Repetition)");
		else
			wsprintfW(txt, L"Checkmate! %s wins!", winner == WHITE ? L"White" : L"Black");
	}
	else {
		wsprintfW(txt, L"%s's turn%s | Half-move: %d",
			player == WHITE ? L"White" : L"Black",
			InCheck(player) ? L" (CHECK!)" : L"",
			halfMoveClock);
	}
	DrawTextW(hdc, txt, -1, &st, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

	// 升变选项栏
	if (promo == WAIT) {
		RECT pr = { 0,CELL_SIZE * 8,CELL_SIZE * 8,CELL_SIZE * 8 + 36 };
		HBRUSH pb = CreateSolidBrush(RGB(25, 25, 25));
		FillRect(hdc, &pr, pb);
		DeleteObject(pb);
		SetTextColor(hdc, RGB(200, 200, 200));
		const wchar_t* s[] = { L"Queen",L"Rook",L"Bishop",L"Knight" };
		for (int i = 0; i < 4; i++) {
			RECT cr = { i * CELL_SIZE,CELL_SIZE * 8,(i + 1) * CELL_SIZE,CELL_SIZE * 8 + 36 };
			DrawTextW(hdc, s[i], -1, &cr, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
		}
	}
}

// ================ 鼠标点击逻辑 ================
static void Click(HWND hwnd, int x, int y) {
	if (gameOver) {
		Init();
		InvalidateRect(hwnd, NULL, TRUE);
		return;
	}

	if (promo == WAIT) {
		if (y >= CELL_SIZE * 8) {
			int col = x / CELL_SIZE;
			if (col >= 0 && col <= 3) {
				PieceType t[] = { QUEEN,ROOK,BISHOP,KNIGHT };
				pendPromo.promoType = t[col];
				DoMove(pendPromo);
				promo = NONE;
				sel.x = -1; sel.y = -1;
				InvalidateRect(hwnd, NULL, TRUE);
				return;
			}
		}
		Undo();
		promo = NONE;
		sel.x = -1; sel.y = -1;
		InvalidateRect(hwnd, NULL, TRUE);
		return;
	}

	int r = y / CELL_SIZE, c = x / CELL_SIZE;
	if (!In(r, c)) return;

	if (sel.x == -1) {
		if (board[r][c].color == player) {
			sel.x = c;
			sel.y = r;
			InvalidateRect(hwnd, NULL, TRUE);
		}
		return;
	}

	Move m[100];
	int cnt = Legal(sel.y, sel.x, m);
	for (int i = 0; i < cnt; i++) {
		if (m[i].tr == r && m[i].tc == c) {
			if (m[i].promotion) {
				promo = WAIT;
				pendPromo = m[i];
				InvalidateRect(hwnd, NULL, TRUE);
				return;
			}
			DoMove(m[i]);
			sel.x = -1; sel.y = -1;
			InvalidateRect(hwnd, NULL, TRUE);
			return;
		}
	}
	if (board[r][c].color == player) {
		sel.x = c;
		sel.y = r;
	}
	else sel.x = -1, sel.y = -1;
	InvalidateRect(hwnd, NULL, TRUE);
}

// ================ 键盘响应 ================
static void Key(WPARAM k) {
	if (k == 'Z' && (GetKeyState(VK_CONTROL) & 0x8000)) Undo();
	if (k == VK_ESCAPE) {
		if (promo == WAIT) {
			Undo();
			promo = NONE;
			sel.x = -1; sel.y = -1;
		}
		else {
			Init();
			sel.x = -1; sel.y = -1;
		}
	}
	InvalidateRect(GetActiveWindow(), NULL, TRUE);
}

// ================ 窗口过程 ================
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
	switch (msg) {
	case WM_CREATE:
		Init();
		break;
	case WM_PAINT: {
		PAINTSTRUCT ps;
		HDC hdc = BeginPaint(hwnd, &ps);
		Draw(hdc);
		EndPaint(hwnd, &ps);
		break;
	}
	case WM_LBUTTONDOWN:
		Click(hwnd, GET_X_LPARAM(lp), GET_Y_LPARAM(lp));
		break;
	case WM_KEYDOWN:
		Key(wp);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProcW(hwnd, msg, wp, lp);
	}
	return 0;
}

// ================ 程序入口 ================
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, int nCmd) {
	WNDCLASSW wc = { 0 };
	wc.lpfnWndProc = WndProc;
	wc.hInstance = hInst;
	wc.lpszClassName = L"Chess";
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	RegisterClassW(&wc);

	HWND hwnd = CreateWindowExW(0, L"Chess", L"International Chess (Full Rules)",
		WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX,
		CW_USEDEFAULT, CW_USEDEFAULT,
		CELL_SIZE * 8 + 16, CELL_SIZE * 8 + 67,
		NULL, NULL, hInst, NULL);
	ShowWindow(hwnd, nCmd);
	UpdateWindow(hwnd);

	MSG msg;
	while (GetMessageW(&msg, NULL, 0, 0)) {
		TranslateMessage(&msg);
		DispatchMessageW(&msg);
	}
	return 0;
}
原文