酷代码 AI
菜单

用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` 对象。 - 循环让两名玩家轮流下棋,直到游戏结束(本程序未实现结束条件)。 - 玩家输入起始坐标和目标坐标,程序检查移动是否有效并更新棋盘。 注意事项: - 此程序是一个简化版的国际象棋游戏,没有实现一些复杂的规则,如王车易位、吃过路兵、升变等。 - 没有实现游戏结束条件(如将死、和棋等)。 [2025-11-23 09:10:16 | AI问答 | 2993点数解答]

服务商
相关提问
发财导航,免费问AI
实用工具查看更多