#include \n#include \n\nconst int SIZE = 15; // 棋盘大小\n\nenum Player {\n NONE,\n BLACK,\n WHITE\n};\n\nclass Gomoku {\nprivate:\n std::vector<std::vector> board; // 棋盘\n Player currentPlayer; // 当前玩家\n\npublic:\n Gomoku() {\n // 初始化棋盘\n board.resize(SIZE, std::vector(SIZE, NONE));\n currentPlayer = BLACK;\n }\n\n // 下棋\n void makeMove(int row, int col) {\n if (row < 0 || row >= SIZE || col < 0 || col >= SIZE || board[row][col] != NONE) {\n std::cout << "Invalid move!" << std::endl;\n return;\n }\n\n board[row][col] = currentPlayer;\n std::cout << "Player " << currentPlayer << " placed a stone at (" << row << ", " << col << ")." << std::endl;\n\n // 检查是否有五子连珠\n if (checkWin(row, col)) {\n std::cout << "Player " << currentPlayer << " wins!" << std::endl;\n return;\n }\n\n // 切换玩家\n currentPlayer = (currentPlayer == BLACK) ? WHITE : BLACK;\n }\n\n // 检查是否有五子连珠\n bool checkWin(int row, int col) {\n Player player = board[row][col];\n\n // 水平方向\n int count = 1;\n int r = row, c = col;\n while (--c >= 0 && board[r][c] == player) {\n count++;\n }\n c = col;\n while (++c < SIZE && board[r][c] == player) {\n count++;\n }\n if (count >= 5) {\n return true;\n }\n\n // 垂直方向\n count = 1;\n r = row, c = col;\n while (--r >= 0 && board[r][c] == player) {\n count++;\n }\n r = row;\n while (++r < SIZE && board[r][c] == player) {\n count++;\n }\n if (count >= 5) {\n return true;\n }\n\n // 左斜方向\n count = 1;\n r = row, c = col;\n while (--r >= 0 && --c >= 0 && board[r][c] == player) {\n count++;\n }\n r = row, c = col;\n while (++r < SIZE && ++c < SIZE && board[r][c] == player) {\n count++;\n }\n if (count >= 5) {\n return true;\n }\n \n // 右斜方向\n count = 1;\n r = row, c = col;\n while (--r >= 0 && ++c < SIZE && board[r][c] == player) {\n count++;\n }\n r = row, c = col;\n while (++r < SIZE && --c >= 0 && board[r][c] == player) {\n count++;\n }\n if (count >= 5) {\n return true;\n }\n\n return false;\n }\n\n // 打印棋盘\n void printBoard() {\n for (int i = 0; i < SIZE; i++) {\n for (int j = 0; j < SIZE; j++) {\n std::cout << board[i][j] << " ";\n }\n std::cout << std::endl;\n }\n }\n};\n\nint main() {\n Gomoku game;\n int row, col;\n\n while (true) {\n game.printBoard();\n\n std::cout << "Player " << game.getCurrentPlayer() << ", make your move (row col): ";\n std::cin >> row >> col;\n game.makeMove(row, col);\n }\n\n return 0;\n}\n


原文地址: https://www.cveoy.top/t/topic/pRMM 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录