酷代码 AI
菜单
服务商

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

以下是一个简单的文字版围棋游戏的 C++ 实现,包含提子、禁入点、打劫、计算胜负等规则: ```cpp #include <iostream> #include <vector> #include <set> #include <algorithm> const int BOARD_SIZE = 9; // 棋盘大小 // 定义棋子颜色 enum Color { EMPTY, BLACK, WHITE }; // 定义棋盘 std::vector<std::vector<Color>> board(BOARD_SIZE, std::vector<Color>(BOARD_SIZE, EMPTY)); // 检查坐标是否在棋盘内 bool isInBoard(int x, int y) { return x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE; } // 深度优先搜索查找连通块 void dfs(int x, int y, Color color, std::set<std::pair<int, int>>& group, std::set<std::pair<int, int>>& liberties) { if (!isInBoard(x, y) || group.count({x, y})) return; if (board[x][y] == color) { group.insert({x, y}); dfs(x + 1, y, color, group, liberties); dfs(x - 1, y, color, group, liberties); dfs(x, y + 1, color, group, liberties); dfs(x, y - 1, color, group, liberties); } else if (board[x][y] == EMPTY) { liberties.insert({x, y}); } } // 检查连通块的气 int getLiberties(const std::set<std::pair<int, int>>& group) { std::set<std::pair<int, int>> liberties; for (const auto& [x, y] : group) { if (isInBoard(x + 1, y) && board[x + 1][y] == EMPTY) liberties.insert({x + 1, y}); if (isInBoard(x - 1, y) && board[x - 1][y] == EMPTY) liberties.insert({x - 1, y}); if (isInBoard(x, y + 1) && board[x][y + 1] == EMPTY) liberties.insert({x, y + 1}); if (isInBoard(x, y - 1) && board[x][y - 1] == EMPTY) liberties.insert({x, y - 1}); } return liberties.size(); } // 提子操作 int removeDeadStones(Color opponentColor) { int removed = 0; std::vector<std::pair<int, int>> deadStones; std::set<std::pair<int, int>> visited; for (int i = 0; i < BOARD_SIZE; ++i) { for (int j = 0; j < BOARD_SIZE; ++j) { if (board[i][j] == opponentColor && !visited.count({i, j})) { std::set<std::pair<int, int>> group; std::set<std::pair<int, int>> liberties; dfs(i, j, opponentColor, group, liberties); if (getLiberties(group) == 0) { for (const auto& [x, y] : group) { deadStones.push_back({x, y}); visited.insert({x, y}); } } } } } for (const auto& [x, y] : deadStones) { board[x][y] = EMPTY; ++removed; } return removed; } // 检查禁入点 bool isForbiddenPoint(int x, int y, Color color) { if (board[x][y] != EMPTY) return true; board[x][y] = color; std::set<std::pair<int, int>> group; std::set<std::pair<int, int>> liberties; dfs(x, y, color, group, liberties); int selfLiberties = getLiberties(group); Color opponentColor = (color == BLACK) ? WHITE : BLACK; int opponentDeadStones = removeDeadStones(opponentColor); board[x][y] = EMPTY; if (selfLiberties == 0 && opponentDeadStones == 0) { return true; } return false; } // 打劫检查 bool isKo(std::vector<std::vector<Color>> prevBoard, int x, int y, Color color) { std::vector<std::vector<Color>> tempBoard = board; tempBoard[x][y] = color; Color opponentColor = (color == BLACK) ? WHITE : BLACK; removeDeadStones(opponentColor); return tempBoard == prevBoard; } // 落子操作 bool placeStone(int x, int y, Color color, std::vector<std::vector<Color>> prevBoard) { if (isForbiddenPoint(x, y, color)) return false; if (isKo(prevBoard, x, y, color)) return false; board[x][y] = color; Color opponentColor = (color == BLACK) ? WHITE : BLACK; removeDeadStones(opponentColor); if (getLiberties({{x, y}}) == 0) { board[x][y] = EMPTY; return false; } return true; } // 计算胜负 std::pair<int, int> calculateScore() { int blackScore = 0; int whiteScore = 0; std::set<std::pair<int, int>> visited; for (int i = 0; i < BOARD_SIZE; ++i) { for (int j = 0; j < BOARD_SIZE; ++j) { if (board[i][j] == BLACK) { ++blackScore; } else if (board[i][j] == WHITE) { ++whiteScore; } else if (!visited.count({i, j})) { std::set<std::pair<int, int>> group; std::set<std::pair<int, int>> liberties; dfs(i, j, EMPTY, group, liberties); bool blackSurrounded = true; bool whiteSurrounded = true; for (const auto& [x, y] : liberties) { if (board[x][y] == BLACK) whiteSurrounded = false; if (board[x][y] == WHITE) blackSurrounded = false; } if (blackSurrounded) { blackScore += group.size(); } else if (whiteSurrounded) { whiteScore += group.size(); } for (const auto& [x, y] : group) { visited.insert({x, y}); } } } } return {blackScore, whiteScore}; } // 打印棋盘 void printBoard() { std::cout << " "; for (int i = 0; i < BOARD_SIZE; ++i) { std::cout << i << " "; } std::cout << std::endl; for (int i = 0; i < BOARD_SIZE; ++i) { std::cout << i << " "; for (int j = 0; j < BOARD_SIZE; ++j) { if (board[i][j] == EMPTY) { std::cout << ". "; } else if (board[i][j] == BLACK) { std::cout << "X "; } else { std::cout << "O "; } } std::cout << std::endl; } } int main() { Color currentPlayer = BLACK; std::vector<std::vector<Color>> prevBoard; while (true) { printBoard(); std::cout << (currentPlayer == BLACK ? "Black's turn" : "White's turn") << std::endl; int x, y; std::cout << "Enter x y coordinates (or -1 -1 to end the game): "; std::cin >> x >> y; if (x == -1 && y == -1) break; if (!isInBoard(x, y)) { std::cout << "Invalid coordinates. Try again." << std::endl; continue; } if (isForbiddenPoint(x, y, currentPlayer)) { std::cout << "This is a forbidden point. Try again." << std::endl; continue; } if (isKo(prevBoard, x, y, currentPlayer)) { std::cout << "Ko situation. Try again." << std::endl; continue; } prevBoard = board; if (placeStone(x, y, currentPlayer, prevBoard)) { currentPlayer = (currentPlayer == BLACK) ? WHITE : BLACK; } else { std::cout << "Invalid move. Try again." << std::endl; } } auto [blackScore, whiteScore] = calculateScore(); std::cout << "Final Score:" << std::endl; std::cout << "Black: " << blackScore << std::endl; std::cout << "White: " << whiteScore << std::endl; if (blackScore > whiteScore) { std::cout << "Black wins!" << std::endl; } else if (whiteScore > blackScore) { std::cout << "White wins!" << std::endl; } else { std::cout << "It's a draw!" << std::endl; } return 0; } ``` 代码说明: 1. 棋盘表示:使用二维向量 `board` 表示棋盘,`EMPTY` 表示空位,`BLACK` 表示黑子,`WHITE` 表示白子。 2. 深度优先搜索(DFS):用于查找连通块和气。 3. 提子操作:`removeDeadStones` 函数用于移除没有气的棋子。 4. 禁入点检查:`isForbiddenPoint` 函数检查落子点是否为禁入点。 5. 打劫检查:`isKo` 函数检查是否为打劫情况。 6. 落子操作:`placeStone` 函数进行落子,并处理提子和禁入点、打劫检查。 7. 计算胜负:`calculateScore` 函数计算双方的得分。 运行方式: 编译并运行该程序,按照提示输入落子的坐标,输入 `-1 -1` 结束游戏,程序将输出最终得分和胜负结果。 [2025-12-14 15:14:09 | AI问答 | 2523点数解答]

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