#include\u003Ciostream\u003E\n#include\u003Cvector\u003E\n\nusing namespace std;\n\nclass TicTacToe {\nprivate:\n int size;\n vector\u003Cvector\u003Cchar\u003E\u003E board;\n char player;\n char computer;\n\npublic:\n TicTacToe(int n) {\n size = n;\n board.resize(size, vector\u003Cchar\u003E(size, ''));\n }\n\n void displayBoard() {\n cout \u003C\u003C "Current board state:\n";\n for (int i = 0; i \u003C size; i++) {\n for (int j = 0; j \u003C size; j++) {\n cout \u003C\u003C board[i][j] \u003C\u003C " ";\n }\n cout \u003C\u003C endl;\n }\n }\n\n bool isBoardFull() {\n for (int i = 0; i \u003C size; i++) {\n for (int j = 0; j \u003C size; j++) {\n if (board[i][j] == '') {\n return false;\n }\n }\n }\n return true;\n }\n\n bool checkRow(char symbol) {\n for (int i = 0; i \u003C size; i++) {\n bool rowWin = true;\n for (int j = 0; j \u003C size; j++) {\n if (board[i][j] != symbol) {\n rowWin = false;\n break;\n }\n }\n if (rowWin) {\n return true;\n }\n }\n return false;\n }\n\n bool checkColumn(char symbol) {\n for (int i = 0; i \u003C size; i++) {\n bool columnWin = true;\n for (int j = 0; j \u003C size; j++) {\n if (board[j][i] != symbol) {\n columnWin = false;\n break;\n }\n }\n if (columnWin) {\n return true;\n }\n }\n return false;\n }\n\n bool checkDiagonal(char symbol) {\n bool diagonalWin1 = true;\n bool diagonalWin2 = true;\n for (int i = 0; i \u003C size; i++) {\n if (board[i][i] != symbol) {\n diagonalWin1 = false;\n }\n if (board[i][size - 1 - i] != symbol) {\n diagonalWin2 = false;\n }\n }\n return diagonalWin1 || diagonalWin2;\n }\n\n bool checkWin(char symbol) {\n return checkRow(symbol) || checkColumn(symbol) || checkDiagonal(symbol);\n }\n\n void makeMove(int row, int col, char symbol) {\n board[row][col] = symbol;\n }\n\n void playGame() {\n cout \u003C\u003C "Welcome to Tic Tac Toe!\n";\n cout \u003C\u003C "Choose the size of the board: ";\n cin \u003E\u003E size;\n\n cout \u003C\u003C "Choose your symbol ('X' or 'O'): ";\n cin \u003E\u003E player;\n computer = (player == 'X') ? 'O' : 'X';\n\n bool playerTurn = true;\n bool gameOver = false;\n\n while (!gameOver) {\n displayBoard();\n\n if (playerTurn) {\n cout \u003C\u003C "Player's turn.\n";\n int row, col;\n cout \u003C\u003C "Enter the row and column to make a move: ";\n cin \u003E\u003E row \u003E\u003E col;\n\n if (row \u003E= 0 && row \u003C size && col \u003E= 0 && col \u003C size && board[row][col] == '') {\n makeMove(row, col, player);\n playerTurn = false;\n } else {\n cout \u003C\u003C "Invalid move. Try again.\n";\n }\n } else {\n cout \u003C\u003C "Computer's turn.\n";\n\n int bestScore = -1;\n int bestRow = -1;\n int bestCol = -1;\n\n for (int i = 0; i \u003C size; i++) {\n for (int j = 0; j \u003C size; j++) {\n if (board[i][j] == '') {\n makeMove(i, j, computer);\n int score = evaluateMove(i, j, computer);\n makeMove(i, j, '');\n\n if (score \u003E bestScore) {\n bestScore = score;\n bestRow = i;\n bestCol = j;\n }\n }\n }\n }\n\n makeMove(bestRow, bestCol, computer);\n playerTurn = true;\n }\n\n if (checkWin(player)) {\n displayBoard();\n cout \u003C\u003C "Player wins!\n";\n gameOver = true;\n } else if (checkWin(computer)) {\n displayBoard();\n cout \u003C\u003C "Computer wins!\n";\n gameOver = true;\n } else if (isBoardFull()) {\n displayBoard();\n cout \u003C\u003C "It's a tie!\n";\n gameOver = true;\n }\n }\n }\n\n int evaluateMove(int row, int col, char symbol) {\n int score = 0;\n\n // Check row and column\n for (int i = 0; i \u003C size; i++) {\n if (board[row][i] == symbol) {\n score += (symbol == 'X') ? 10 : 8;\n } else if (board[row][i] == '') {\n score += 4;\n }\n if (board[i][col] == symbol) {\n score += (symbol == 'X') ? 10 : 8;\n } else if (board[i][col] == '') {\n score += 4;\n }\n }\n\n // Check diagonal\n if (row == col || row + col == size - 1) {\n for (int i = 0; i \u003C size; i++) {\n if (board[i][i] == symbol) {\n score += (symbol == 'X') ? 10 : 8;\n } else if (board[i][i] == '') {\n score += 4;\n }\n if (board[i][size - 1 - i] == symbol) {\n score += (symbol == 'X') ? 10 : 8;\n } else if (board[i][size - 1 - i] == '*') {\n score += 4;\n }\n }\n }\n\n return score;\n }\n};\n\nint main() {\n TicTacToe game(3);\n game.playGame();\n\n return 0;\n}

C++ 3x3 棋盘 人机对战  Tic Tac Toe 游戏代码

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

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