#include "iostream"\n#include "vector"\n#include "algorithm"\n\nusing namespace std;\n\nclass TicTacToe {\nprivate:\n int size; // 棋盘大小\n vector<vector> board; // 棋盘\n char playerSymbol; // 玩家棋子符号\n char computerSymbol; // 计算机棋子符号\n bool playerTurn; // 玩家回合标志\n\npublic:\n TicTacToe(int size) {\n this->size = size;\n board.resize(size, vector(size, ''));\n playerTurn = true;\n }\n\n void startGame() {\n cout << "Welcome to Tic Tac Toe Game!" << endl;\n\n // 用户选择先后手和棋子符号\n cout << "Do you want to play first? (y/n): " ;\n char choice;\n cin >> choice;\n playerTurn = (choice == 'y' || choice == 'Y');\n\n cout << "Choose your symbol (X/O): " ;\n cin >> playerSymbol;\n computerSymbol = (playerSymbol == 'X') ? 'O' : 'X';\n\n // 游戏进行中\n while (!isGameOver()) {\n displayBoard();\n if (playerTurn) {\n playerMove();\n } else {\n computerMove();\n }\n playerTurn = !playerTurn;\n }\n\n // 游戏结束,显示最终结果\n displayBoard();\n if (isPlayerWin()) {\n cout << "Congratulations! You win!" << endl;\n } else if (isComputerWin()) {\n cout << "Sorry, you lose. Better luck next time!" << endl;\n } else {\n cout << "It's a draw!" << endl;\n }\n }\n\nprivate:\n // 显示棋盘\n void displayBoard() {\n cout << "Current board:" << endl;\n for (int i = 0; i < size; i++) {\n for (int j = 0; j < size; j++) {\n cout << board[i][j] << " " ;\n }\n cout << endl;\n }\n cout << endl;\n }\n\n // 玩家下棋\n void playerMove() {\n cout << "Your turn. Enter the position (1-" << sizesize << "): " ;\n int position;\n cin >> position;\n\n int row = (position - 1) / size;\n int col = (position - 1) % size;\n\n if (board[row][col] != '') {\n cout << "Invalid move. Please try again." << endl;\n playerMove();\n } else {\n board[row][col] = playerSymbol;\n }\n }\n\n // 计算机下棋\n void computerMove() {\n cout << "Computer's turn:" << endl;\n\n int bestScore = -1;\n int bestRow, bestCol;\n\n // 遍历每个空位,计算分值\n for (int i = 0; i < size; i++) {\n for (int j = 0; j < size; j++) {\n if (board[i][j] == '') {\n board[i][j] = computerSymbol;\n int score = calculateScore(computerSymbol, playerSymbol);\n board[i][j] = '';\n\n if (score > bestScore) {\n bestScore = score;\n bestRow = i;\n bestCol = j;\n }\n }\n }\n }\n\n board[bestRow][bestCol] = computerSymbol;\n }\n\n // 计算分值\n int calculateScore(char currentPlayerSymbol, char opponentSymbol) {\n int score = 0;\n\n // 计算行分值\n for (int i = 0; i < size; i++) {\n int currentPlayerCount = 0;\n int opponentCount = 0;\n for (int j = 0; j < size; j++) {\n if (board[i][j] == currentPlayerSymbol) {\n currentPlayerCount++;\n } else if (board[i][j] == opponentSymbol) {\n opponentCount++;\n }\n }\n\n if (currentPlayerCount == size) {\n score += 50;\n } else if (opponentCount == size) {\n score += 25;\n } else if (currentPlayerCount == size - 1 && opponentCount == 0) {\n score += 10;\n } else if (opponentCount == size - 1 && currentPlayerCount == 0) {\n score += 8;\n } else if (currentPlayerCount == 0 && opponentCount == 0) {\n score += 4;\n }\n }\n\n // 计算列分值\n for (int j = 0; j < size; j++) {\n int currentPlayerCount = 0;\n int opponentCount = 0;\n for (int i = 0; i < size; i++) {\n if (board[i][j] == currentPlayerSymbol) {\n currentPlayerCount++;\n } else if (board[i][j] == opponentSymbol) {\n opponentCount++;\n }\n }\n\n if (currentPlayerCount == size) {\n score += 50;\n } else if (opponentCount == size) {\n score += 25;\n } else if (currentPlayerCount == size - 1 && opponentCount == 0) {\n score += 10;\n } else if (opponentCount == size - 1 && currentPlayerCount == 0) {\n score += 8;\n } else if (currentPlayerCount == 0 && opponentCount == 0) {\n score += 4;\n }\n }\n\n // 计算对角线分值\n int currentPlayerCount = 0;\n int opponentCount = 0;\n for (int i = 0; i < size; i++) {\n if (board[i][i] == currentPlayerSymbol) {\n currentPlayerCount++;\n } else if (board[i][i] == opponentSymbol) {\n opponentCount++;\n }\n }\n\n if (currentPlayerCount == size) {\n score += 50;\n } else if (opponentCount == size) {\n score += 25;\n } else if (currentPlayerCount == size - 1 && opponentCount == 0) {\n score += 10;\n } else if (opponentCount == size - 1 && currentPlayerCount == 0) {\n score += 8;\n } else if (currentPlayerCount == 0 && opponentCount == 0) {\n score += 4;\n }\n\n currentPlayerCount = 0;\n opponentCount = 0;\n for (int i = 0; i < size; i++) {\n if (board[i][size - 1 - i] == currentPlayerSymbol) {\n currentPlayerCount++;\n } else if (board[i][size - 1 - i] == opponentSymbol) {\n opponentCount++;\n }\n }\n\n if (currentPlayerCount == size) {\n score += 50;\n } else if (opponentCount == size) {\n score += 25;\n } else if (currentPlayerCount == size - 1 && opponentCount == 0) {\n score += 10;\n } else if (opponentCount == size - 1 && currentPlayerCount == 0) {\n score += 8;\n } else if (currentPlayerCount == 0 && opponentCount == 0) {\n score += 4;\n }\n\n return score;\n }\n\n // 判断游戏是否结束\n bool isGameOver() {\n return isPlayerWin() || isComputerWin() || isDraw();\n }\n\n // 判断玩家是否获胜\n bool isPlayerWin() {\n return isWin(playerSymbol);\n }\n\n // 判断计算机是否获胜\n bool isComputerWin() {\n return isWin(computerSymbol);\n }\n\n // 判断是否平局\n bool isDraw() {\n for (int i = 0; i < size; i++) {\n for (int j = 0; j < size; j++) {\n if (board[i][j] == '') {\n return false;\n }\n }\n }\n return true;\n }\n\n // 判断是否有一方获胜\n bool isWin(char symbol) {\n // 检查行\n for (int i = 0; i < size; i++) {\n bool win = true;\n for (int j = 0; j < size; j++) {\n if (board[i][j] != symbol) {\n win = false;\n break;\n }\n }\n if (win) {\n return true;\n }\n }\n\n // 检查列\n for (int j = 0; j < size; j++) {\n bool win = true;\n for (int i = 0; i < size; i++) {\n if (board[i][j] != symbol) {\n win = false;\n break;\n }\n }\n if (win) {\n return true;\n }\n }\n\n // 检查对角线\n bool win = true;\n for (int i = 0; i < size; i++) {\n if (board[i][i] != symbol) {\n win = false;\n break;\n }\n }\n if (win) {\n return true;\n }\n\n win = true;\n for (int i = 0; i < size; i++) {\n if (board[i][size - 1 - i] != symbol) {\n win = false;\n break;\n }\n }\n if (win) {\n return true;\n }\n\n return false;\n }\n};\n\nint main() {\n int size;\n cout << "Enter the size of the board: " ;\n cin >> size;\n\n TicTacToe game(size);\n game.startGame();\n\n return 0;\n}