C++实现包含基本规则的国际象棋控制台游戏
以下是一个简单的 C++ 实现的国际象棋游戏,它包含了基本的国际象棋规则。这个程序是一个控制台应用程序,允许两名玩家轮流下棋。 ```cpp #include <iostream> #include <vector> #include <string> // 定义棋子类型 enum PieceType { EMPTY, PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING }; // 定义棋子颜色 enum PieceColor { WHITE, BLACK }; // 定义棋盘上的一个格子 struct Square { PieceType type; PieceColor color; Square() : type(EMPTY), color(WHITE) {} }; // 定义棋盘 class ChessBoard { private: std::vector<std::vector<Square>> board; // 初始化棋盘 void initializeBoard() { board.resize(8, std::vector<Square>(8)); // 放置白方棋子 for (int i = 0; i < 8; ++i) { board[1][i].type = PAWN; board[1][i].color = WHITE; } board[0][0].type = ROOK; board[0][0].color = WHITE; board[0][1].type = KNIGHT; board[0][1].color = WHITE; board[0][2].type = BISHOP; board[0][2].color = WHITE; board[0][3].type = QUEEN; board[0][3].color = WHITE; board[0][4].type = KING; board[0][4].color = WHITE; board[0][5].type = BISHOP; board[0][5].color = WHITE; board[0][6].type = KNIGHT; board[0][6].color = WHITE; board[0][7].type = ROOK; board[0][7].color = WHITE; // 放置黑方棋子 for (int i = 0; i < 8; ++i) { board[6][i].type = PAWN; board[6][i].color = BLACK; } board[7][0].type = ROOK; board[7][0].color = BLACK; board[7][1].type = KNIGHT; board[7][1].color = BLACK; board[7][2].type = BISHOP; board[7][2].color = BLACK; board[7][3].type = QUEEN; board[7][3].color = BLACK; board[7][4].type = KING; board[7][4].color = BLACK; board[7][5].type = BISHOP; board[7][5].color = BLACK; board[7][6].type = KNIGHT; board[7][6].color = BLACK; board[7][7].type = ROOK; board[7][7].color = BLACK; } // 检查坐标是否在棋盘内 bool isOnBoard(int x, int y) { return x >= 0 && x < 8 && y >= 0 && y < 8; } // 检查移动是否符合棋子规则(简化版) bool isValidMove(int fromX, int fromY, int toX, int toY) { PieceType piece = board[fromX][fromY].type; PieceColor color = board[fromX][fromY].color; // 目标位置不能是己方棋子 if (board[toX][toY].color == color && board[toX][toY].type != EMPTY) { return false; } switch (piece) { case PAWN: if (color == WHITE) { if (fromY == toY) { if (fromX == 1 && (toX == 2 || toX == 3)) { return board[2][fromY].type == EMPTY && board[3][fromY].type == EMPTY; } return toX == fromX + 1 && board[toX][toY].type == EMPTY; } else if (std::abs(fromY - toY) == 1 && toX == fromX + 1) { return board[toX][toY].type != EMPTY && board[toX][toY].color == BLACK; } } else { if (fromY == toY) { if (fromX == 6 && (toX == 5 || toX == 4)) { return board[5][fromY].type == EMPTY && board[4][fromY].type == EMPTY; } return toX == fromX - 1 && board[toX][toY].type == EMPTY; } else if (std::abs(fromY - toY) == 1 && toX == fromX - 1) { return board[toX][toY].type != EMPTY && board[toX][toY].color == WHITE; } } break; case ROOK: if (fromX == toX) { int step = (fromY < toY) ? 1 : -1; for (int y = fromY + step; y != toY; y += step) { if (board[fromX][y].type != EMPTY) { return false; } } return true; } else if (fromY == toY) { int step = (fromX < toX) ? 1 : -1; for (int x = fromX + step; x != toX; x += step) { if (board[x][fromY].type != EMPTY) { return false; } } return true; } break; case KNIGHT: return (std::abs(fromX - toX) == 2 && std::abs(fromY - toY) == 1) || (std::abs(fromX - toX) == 1 && std::abs(fromY - toY) == 2); case BISHOP: if (std::abs(fromX - toX) == std::abs(fromY - toY)) { int xStep = (fromX < toX) ? 1 : -1; int yStep = (fromY < toY) ? 1 : -1; for (int x = fromX + xStep, y = fromY + yStep; x != toX; x += xStep, y += yStep) { if (board[x][y].type != EMPTY) { return false; } } return true; } break; case QUEEN: if (fromX == toX) { int step = (fromY < toY) ? 1 : -1; for (int y = fromY + step; y != toY; y += step) { if (board[fromX][y].type != EMPTY) { return false; } } return true; } else if (fromY == toY) { int step = (fromX < toX) ? 1 : -1; for (int x = fromX + step; x != toX; x += step) { if (board[x][fromY].type != EMPTY) { return false; } } return true; } else if (std::abs(fromX - toX) == std::abs(fromY - toY)) { int xStep = (fromX < toX) ? 1 : -1; int yStep = (fromY < toY) ? 1 : -1; for (int x = fromX + xStep, y = fromY + yStep; x != toX; x += xStep, y += yStep) { if (board[x][y].type != EMPTY) { return false; } } return true; } break; case KING: return std::abs(fromX - toX) <= 1 && std::abs(fromY - toY) <= 1; default: break; } return false; } public: ChessBoard() { initializeBoard(); } // 打印棋盘 void printBoard() { std::cout << " 0 1 2 3 4 5 6 7" << std::endl; for (int i = 0; i < 8; ++i) { std::cout << i << " "; for (int j = 0; j < 8; ++j) { switch (board[i][j].type) { case EMPTY: std::cout << ". "; break; case PAWN: std::cout << (board[i][j].color == WHITE ? "P " : "p "); break; case ROOK: std::cout << (board[i][j].color == WHITE ? "R " : "r "); break; case KNIGHT: std::cout << (board[i][j].color == WHITE ? "N " : "n "); break; case BISHOP: std::cout << (board[i][j].color == WHITE ? "B " : "b "); break; case QUEEN: std::cout << (board[i][j].color == WHITE ? "Q " : "q "); break; case KING: std::cout << (board[i][j].color == WHITE ? "K " : "k "); break; } } std::cout << std::endl; } } // 移动棋子 bool movePiece(int fromX, int fromY, int toX, int toY) { if (!isOnBoard(fromX, fromY) || !isOnBoard(toX, toY)) { return false; } if (!isValidMove(fromX, fromY, toX, toY)) { return false; } board[toX][toY] = board[fromX][fromY]; board[fromX][fromY].type = EMPTY; board[fromX][fromY].color = WHITE; return true; } }; int main() { ChessBoard board; PieceColor currentPlayer = WHITE; while (true) { board.printBoard(); std::cout << (currentPlayer == WHITE ? "White's turn" : "Black's turn") << std::endl; int fromX, fromY, toX, toY; std::cout << "Enter from coordinates (x y): "; std::cin >> fromX >> fromY; std::cout << "Enter to coordinates (x y): "; std::cin >> toX >> toY; if (board.movePiece(fromX, fromY, toX, toY)) { currentPlayer = (currentPlayer == WHITE) ? BLACK : WHITE; } else { std::cout << "Invalid move. Try again." << std::endl; } } return 0; } ``` 代码说明: 1. 棋子类型和颜色:使用 `enum` 定义了棋子的类型(如 `PAWN`, `ROOK` 等)和颜色(`WHITE` 和 `BLACK`)。 2. 棋盘类 `ChessBoard`: - `initializeBoard` 方法:初始化棋盘,将棋子放置在初始位置。 - `printBoard` 方法:打印当前棋盘状态。 - `isOnBoard` 方法:检查给定的坐标是否在棋盘内。 - `isValidMove` 方法:检查移动是否符合棋子的规则。 - `movePiece` 方法:尝试移动棋子,如果移动有效则更新棋盘状态。 3. 主函数 `main`: - 创建一个 `ChessBoard` 对象。 - 循环让两名玩家轮流下棋,直到游戏结束(本程序未实现结束条件)。 - 玩家输入起始坐标和目标坐标,程序检查移动是否有效并更新棋盘。 注意事项: - 此程序是一个简化版的国际象棋游戏,没有实现一些复杂的规则,如王车易位、吃过路兵、升变等。 - 没有实现游戏结束条件(如将死、和棋等)。 ######[AI问答 | 2993点数解答 | 2025-11-23 09:10:16]
- 超燃年会来袭!诗舞歌戏轮番上阵,共赴欢乐盛宴! (字节豆包 | 206点数解答 | 2025-01-08 10:59:43)347
- Python 实现:根据甜度值为甜甜圈评定等级(字节豆包 | 243点数解答 | 2025-12-06 18:35:50)89
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)349
- “才女”技能揭秘:用牌时可跨类型出牌!(字节豆包 | 43点数解答 | 2025-07-31 10:26:35)183
- “才女”技能揭秘:使用规则与开发注意要点大曝光! (DeepSeek | 98点数解答 | 2025-10-02 14:38:59)107
- 51 单片机:定时器 0 实现 8 个 LED 循环点亮,附代码及优化建议(字节豆包 | 1193点数解答 | 2024-12-27 15:10:29)319
- C++实现喵际数独锦标赛自动评判系统:快速验证数独解答正确性(字节豆包 | 666点数解答 | 2025-05-16 16:46:31)173
- 用 JS 实现网页猜数字游戏:1 - 100 随机数等你挑战!(百度文心 | 653点数解答 | 2025-03-26 14:39:33)239
- JS实现猜数字游戏:弹窗输入,1 - 100随机挑战,教你轻松上手!(百度文心 | 646点数解答 | 2025-03-26 14:49:43)200
- 融合道德感机制:女性NPC规则全面升级(字节豆包 | 1489点数解答 | 2025-06-20 20:06:34)158
- C++ 解决老师奖励同学铅笔数量问题:最少奖励方案揭秘(字节豆包 | 724点数解答 | 2025-12-06 20:58:32)46
- Java 实现:输入 3 个学生成绩信息并计算总分与平均分(字节豆包 | 847点数解答 | 2024-10-11 08:45:50)733