C++#include <iostream>
#include <iomanip>
#include <string>
#include <windows.h>
using namespace std;
#define ROWS 10
#define COLS 9
enum PieceType {
EMPTY,
RED_K, RED_A, RED_B, RED_R, RED_H, RED_CAN, RED_PAWN,
BLACK_K, BLACK_A, BLACK_B, BLACK_R, BLACK_H, BLACK_CAN, BLACK_PAWN
};
struct Piece {
PieceType type;
bool isRed;
bool alive;
};
void setColor(int color) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, color);
}
void initBoard(Piece board[ROWS][COLS]) {
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLS; j++)
board[i][j] = { EMPTY, false, false };
// 红方
board[9][0] = { RED_R, true, true };
board[9][8] = { RED_R, true, true };
board[9][1] = { RED_H, true, true };
board[9][7] = { RED_H, true, true };
board[9][2] = { RED_B, true, true };
board[9][6] = { RED_B, true, true };
board[9][3] = { RED_A, true, true };
board[9][5] = { RED_A, true, true };
board[9][4] = { RED_K, true, true };
board[7][1] = { RED_CAN, true, true };
board[7][7] = { RED_CAN, true, true };
board[6][0] = { RED_PAWN, true, true };
board[6][2] = { RED_PAWN, true, true };
board[6][4] = { RED_PAWN, true, true };
board[6][6] = { RED_PAWN, true, true };
board[6][8] = { RED_PAWN, true, true };
// 黑方
board[0][0] = { BLACK_R, false, true };
board[0][8] = { BLACK_R, false, true };
board[0][1] = { BLACK_H, false, true };
board[0][7] = { BLACK_H, false, true };
board[0][2] = { BLACK_B, false, true };
board[0][6] = { BLACK_B, false, true };
board[0][3] = { BLACK_A, false, true };
board[0][5] = { BLACK_A, false, true };
board[0][4] = { BLACK_K, false, true };
board[2][1] = { BLACK_CAN, false, true };
board[2][7] = { BLACK_CAN, false, true };
board[3][0] = { BLACK_PAWN, false, true };
board[3][2] = { BLACK_PAWN, false, true };
board[3][4] = { BLACK_PAWN, false, true };
board[3][6] = { BLACK_PAWN, false, true };
board[3][8] = { BLACK_PAWN, false, true };
}
string getPieceStr(Piece p) {
if (!p.alive) return "十 ";
switch (p.type) {
case RED_K: return "帅 ";
case RED_A: return "仕 ";
case RED_B: return "相 ";
case RED_R: return "车 ";
case RED_H: return "马 ";
case RED_CAN: return "炮 ";
case RED_PAWN: return "兵 ";
case BLACK_K: return "将 ";
case BLACK_A: return "士 ";
case BLACK_B: return "象 ";
case BLACK_R: return "车 ";
case BLACK_H: return "马 ";
case BLACK_CAN: return "炮 ";
case BLACK_PAWN: return "卒 ";
default: return "十 ";
}
}
void printBoard(Piece board[ROWS][COLS]) {
system("cls");
setColor(7);
cout << " ";
for (int j = 0; j < COLS; j++)
cout << j << " ";
cout << endl;
for (int i = 0; i < ROWS; i++) {
setColor(7);
cout << setw(2) << i << " ";
for (int j = 0; j < COLS; j++) {
Piece p = board[i][j];
if (!p.alive) {
setColor(7);
cout << "十 ";
} else if (p.isRed) {
setColor(12);
cout << getPieceStr(p);
} else {
setColor(9);
cout << getPieceStr(p);
}
}
setColor(7);
cout << endl;
}
}
bool inBoard(int x, int y) {
return x >= 0 && x < ROWS && y >= 0 && y < COLS;
}
// 统计两点之间有多少棋子(车/炮专用)
int countBetween(Piece board[ROWS][COLS], int x1, int y1, int x2, int y2) {
int cnt = 0;
if (x1 == x2) {
int minY = min(y1, y2) + 1;
int maxY = max(y1, y2);
for (int y = minY; y < maxY; y++)
if (board[x1][y].alive) cnt++;
} else if (y1 == y2) {
int minX = min(x1, x2) + 1;
int maxX = max(x1, x2);
for (int x = minX; x < maxX; x++)
if (board[x][y1].alive) cnt++;
}
return cnt;
}
// 帅/将 不能对面
bool kingFaceToFace(Piece board[ROWS][COLS]) {
int rx = -1, ry = -1, bx = -1, by = -1;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (board[i][j].type == RED_K && board[i][j].alive) {
rx = i; ry = j;
}
if (board[i][j].type == BLACK_K && board[i][j].alive) {
bx = i; by = j;
}
}
}
if (ry != by) return false;
return countBetween(board, rx, ry, bx, by) == 0;
}
// 完整象棋走法规则
bool canMove(Piece board[ROWS][COLS], int fx, int fy, int tx, int ty) {
if (!inBoard(fx, fy) || !inBoard(tx, ty)) return false;
if (fx == tx && fy == ty) return false;
Piece from = board[fx][fy];
Piece to = board[tx][ty];
if (!from.alive) return false;
if (to.alive && from.isRed == to.isRed) return false;
int dx = tx - fx;
int dy = ty - fy;
bool red = from.isRed;
// ===== 1. 帅/将 =====
if (from.type == RED_K || from.type == BLACK_K) {
bool inPalace = false;
if (red) inPalace = (tx >= 7 && tx <= 9 && ty >= 3 && ty <= 5);
else inPalace = (tx >= 0 && tx <= 2 && ty >= 3 && ty <= 5);
if (!inPalace) return false;
if ((abs(dx) == 1 && dy == 0) || (dx == 0 && abs(dy) == 1)) return true;
return false;
}
// ===== 2. 士/仕 =====
if (from.type == RED_A || from.type == BLACK_A) {
bool inPalace = false;
if (red) inPalace = (tx >= 7 && tx <= 9 && ty >= 3 && ty <= 5);
else inPalace = (tx >= 0 && tx <= 2 && ty >= 3 && ty <= 5);
if (!inPalace) return false;
if (abs(dx) == 1 && abs(dy) == 1) return true;
return false;
}
// ===== 3. 象/相 =====
if (from.type == RED_B || from.type == BLACK_B) {
if (red && tx < 5) return false;
if (!red && tx > 4) return false;
if (abs(dx) != 2 || abs(dy) != 2) return false;
int mx = fx + dx / 2;
int my = fy + dy / 2;
if (board[mx][my].alive) return false;
return true;
}
// ===== 4. 马 =====
if (from.type == RED_H || from.type == BLACK_H) {
if (!((abs(dx) == 1 && abs(dy) == 2) || (abs(dx) == 2 && abs(dy) == 1))) return false;
int mx = fx + (dx > 0 ? (dx >= 2 ? 1 : 0) : (dx <= -2 ? -1 : 0));
int my = fy + (dy > 0 ? (dy >= 2 ? 1 : 0) : (dy <= -2 ? -1 : 0));
if (board[mx][my].alive) return false;
return true;
}
// ===== 5. 车 =====
if (from.type == RED_R || from.type == BLACK_R) {
if (fx != tx && fy != ty) return false;
if (countBetween(board, fx, fy, tx, ty) == 0) return true;
return false;
}
// ===== 6. 炮 =====
if (from.type == RED_CAN || from.type == BLACK_CAN) {
if (fx != tx && fy != ty) return false;
int cnt = countBetween(board, fx, fy, tx, ty);
if (!to.alive && cnt == 0) return true;
if (to.alive && cnt == 1) return true;
return false;
}
// ===== 7. 兵/卒 =====
if (from.type == RED_PAWN || from.type == BLACK_PAWN) {
bool crossed = red ? (fx <= 4) : (fx >= 5);
if (red) {
if (dx > 0) return false;
if (!crossed && (dx != -1 || dy != 0)) return false;
if (crossed && !((dx == -1 && dy == 0) || (dx == 0 && abs(dy) == 1))) return false;
} else {
if (dx < 0) return false;
if (!crossed && (dx != 1 || dy != 0)) return false;
if (crossed && !((dx == 1 && dy == 0) || (dx == 0 && abs(dy) == 1))) return false;
}
return true;
}
return false;
}
bool movePiece(Piece board[ROWS][COLS], int fx, int fy, int tx, int ty, bool isRedTurn) {
Piece& from = board[fx][fy];
if (from.isRed != isRedTurn) return false;
if (!canMove(board, fx, fy, tx, ty)) return false;
// 临时移动,检测帅将对面
Piece tempTo = board[tx][ty];
board[tx][ty] = from;
board[fx][fy] = { EMPTY, false, false };
bool illegal = kingFaceToFace(board);
if (illegal) {
board[fx][fy] = from;
board[tx][ty] = tempTo;
return false;
}
return true;
}
bool isGameOver(Piece board[ROWS][COLS]) {
bool rk = false, bk = false;
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLS; j++) {
if (board[i][j].type == RED_K && board[i][j].alive) rk = true;
if (board[i][j].type == BLACK_K && board[i][j].alive) bk = true;
}
return !rk || !bk;
}
int main() {
Piece board[ROWS][COLS];
initBoard(board);
bool redTurn = true;
setColor(7);
cout << "===== 完整规则彩色中国象棋 =====" << endl;
cout << "输入:起始行 起始列 目标行 目标列" << endl;
system("pause");
while (!isGameOver(board)) {
printBoard(board);
int fx, fy, tx, ty;
setColor(7);
cout << (redTurn ? "红方走" : "黑方走") << ":";
cin >> fx >> fy >> tx >> ty;
if(!(0 <= fx <= 9 && 0 <= tx <= 9 && 0 <= fy <= 8 && 0 <= ty <= 8)) {
cout << " 走法非法!按回车重试" << endl;
system("pause");
} else if (movePiece(board, fx, fy, tx, ty, redTurn)) {
redTurn = !redTurn;
} else {
cout << " 走法非法!按回车重试" << endl;
system("pause");
}
}
printBoard(board);
setColor(10);
cout << (redTurn ? "黑方胜利!" : "红方胜利!") << endl;
setColor(7);
return 0;
}