酷代码 AI
菜单
服务商

用c++编写一个文字版19路围棋游戏 包含提子、禁入点、打劫、计算胜负等规则。

以下是一个用 C++ 编写的 19 路文字版围棋游戏,实现了提子、禁入点、打劫、计算胜负等规则: ```cpp #include <iostream> #include <vector> #include <set> const int BOARD_SIZE = 19; // 表示棋子颜色 enum class Color { EMPTY, BLACK, WHITE }; // 表示棋盘 class GoBoard { private: std::vector<std::vector<Color>> board; std::pair<int, int> lastMove; // 记录上一步落子位置,用于打劫判断 bool isFirstMove; // 是否是第一步棋 // 检查一个棋子的气 int liberties(int x, int y) { std::set<std::pair<int, int>> visited; std::vector<std::pair<int, int>> stack; stack.push_back({x, y}); Color color = board[x][y]; int lib = 0; while (!stack.empty()) { auto [cx, cy] = stack.back(); stack.pop_back(); if (visited.find({cx, cy}) != visited.end()) continue; visited.insert({cx, cy}); // 检查四个方向 int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; for (int i = 0; i < 4; ++i) { int nx = cx + dx[i]; int ny = cy + dy[i]; if (nx >= 0 && nx < BOARD_SIZE && ny >= 0 && ny < BOARD_SIZE) { if (board[nx][ny] == Color::EMPTY) { lib++; } else if (board[nx][ny] == color) { stack.push_back({nx, ny}); } } } } return lib; } // 移除无气的棋子 int removeDeadStones(Color opponentColor) { int removed = 0; std::vector<std::pair<int, int>> toRemove; for (int i = 0; i < BOARD_SIZE; ++i) { for (int j = 0; j < BOARD_SIZE; ++j) { if (board[i][j] == opponentColor && liberties(i, j) == 0) { toRemove.push_back({i, j}); } } } for (auto [x, y] : toRemove) { board[x][y] = Color::EMPTY; removed++; } return removed; } // 检查是否是禁入点 bool isForbiddenMove(int x, int y, Color color) { if (board[x][y] != Color::EMPTY) return true; board[x][y] = color; Color opponentColor = (color == Color::BLACK) ? Color::WHITE : Color::BLACK; int removed = removeDeadStones(opponentColor); bool hasLiberties = liberties(x, y) > 0; board[x][y] = Color::EMPTY; removeDeadStones(opponentColor); // 恢复棋盘 if (!hasLiberties) { // 如果落子后自己无气且没有提掉对方棋子,则为禁入点 return removed == 0; } return false; } // 检查是否是打劫 bool isKo(int x, int y, Color color) { if (isFirstMove) return false; board[x][y] = color; Color opponentColor = (color == Color::BLACK) ? Color::WHITE : Color::BLACK; int removed = removeDeadStones(opponentColor); if (removed == 1) { auto [lastX, lastY] = lastMove; if (board[lastX][lastY] == Color::EMPTY && liberties(lastX, lastY) == 0) { board[x][y] = Color::EMPTY; removeDeadStones(opponentColor); // 恢复棋盘 return true; } } board[x][y] = Color::EMPTY; removeDeadStones(opponentColor); // 恢复棋盘 return false; } public: GoBoard() : board(BOARD_SIZE, std::vector<Color>(BOARD_SIZE, Color::EMPTY)), lastMove({-1, -1}), isFirstMove(true) {} // 落子 bool makeMove(int x, int y, Color color) { if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE) return false; if (isForbiddenMove(x, y, color)) return false; if (isKo(x, y, color)) return false; board[x][y] = color; Color opponentColor = (color == Color::BLACK) ? Color::WHITE : Color::BLACK; removeDeadStones(opponentColor); lastMove = {x, y}; isFirstMove = false; return true; } // 计算胜负 std::pair<int, int> calculateScore() { int blackScore = 0; int whiteScore = 0; for (int i = 0; i < BOARD_SIZE; ++i) { for (int j = 0; j < BOARD_SIZE; ++j) { if (board[i][j] == Color::BLACK) { blackScore++; } else if (board[i][j] == Color::WHITE) { whiteScore++; } } } return {blackScore, whiteScore}; } // 打印棋盘 void printBoard() { for (int i = 0; i < BOARD_SIZE; ++i) { for (int j = 0; j < BOARD_SIZE; ++j) { if (board[i][j] == Color::EMPTY) { std::cout << ". "; } else if (board[i][j] == Color::BLACK) { std::cout << "X "; } else { std::cout << "O "; } } std::cout << std::endl; } } }; int main() { GoBoard board; Color currentPlayer = Color::BLACK; while (true) { board.printBoard(); std::cout << (currentPlayer == Color::BLACK ? "Black's turn" : "White's turn") << std::endl; int x, y; std::cout << "Enter x and y coordinates (0-18): "; std::cin >> x >> y; if (board.makeMove(x, y, currentPlayer)) { currentPlayer = (currentPlayer == Color::BLACK) ? Color::WHITE : Color::BLACK; } else { std::cout << "Invalid move. Try again." << std::endl; } auto [blackScore, whiteScore] = board.calculateScore(); std::cout << "Black score: " << blackScore << ", White score: " << whiteScore << std::endl; } return 0; } ``` 代码说明: 1. `Color` 枚举:表示棋子的颜色,包括空、黑子和白子。 2. `GoBoard` 类: - `board`:二维向量表示棋盘。 - `lastMove`:记录上一步落子位置,用于打劫判断。 - `isFirstMove`:标记是否是第一步棋。 - `liberties`:计算一个棋子或一块棋的气。 - `removeDeadStones`:移除无气的棋子。 - `isForbiddenMove`:检查是否是禁入点。 - `isKo`:检查是否是打劫。 - `makeMove`:落子并处理提子、禁入点和打劫规则。 - `calculateScore`:计算黑白双方的得分。 - `printBoard`:打印当前棋盘状态。 3. `main` 函数: - 创建 `GoBoard` 对象。 - 循环让玩家交替落子,处理输入并调用 `makeMove` 方法。 - 打印当前棋盘和得分。 这个程序实现了基本的围棋规则,包括提子、禁入点、打劫和计算胜负。玩家可以通过输入坐标来落子,程序会检查落子的合法性并更新棋盘状态。 [2025-12-14 15:20:13 | AI问答 | 2026点数解答]

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