#include \n#include \nusing namespace std;\n\nclass TicTacToe {\nprivate:\n int size;\n vector<vector> board;\n char computerSymbol;\n char playerSymbol;\n\npublic:\n TicTacToe(int s) {\n size = s;\n board.resize(size, vector(size, ''));\n }\n\n void startGame() {\n cout << "Welcome to Tic Tac Toe!" << endl;\n cout << "Choose your symbol (X/O): ";\n cin >> playerSymbol;\n while (playerSymbol != 'X' && playerSymbol != 'O') {\n cout << "Invalid symbol. Please choose again: ";\n cin >> playerSymbol;\n }\n\n cout << "Do you want to play first? (Y/N): ";\n char choice;\n cin >> choice;\n while (choice != 'Y' && choice != 'N') {\n cout << "Invalid choice. Please choose again: ";\n cin >> choice;\n }\n\n if (choice == 'Y') {\n cout << "You play first." << endl;\n computerSymbol = (playerSymbol == 'X') ? 'O' : 'X';\n printBoard();\n playGame();\n } else {\n cout << "Computer plays first." << endl;\n computerSymbol = (playerSymbol == 'X') ? 'X' : 'O';\n playGame();\n }\n }\n\n void playGame() {\n bool playerTurn = (playerSymbol == 'X');\n bool gameOver = false;\n while (!gameOver) {\n if (playerTurn) {\n playerMove();\n printBoard();\n if (isGameOver()) {\n cout << "You win!" << endl;\n gameOver = true;\n }\n playerTurn = false;\n } else {\n computerMove();\n printBoard();\n if (isGameOver()) {\n cout << "Computer wins!" << endl;\n gameOver = true;\n }\n playerTurn = true;\n }\n }\n }\n\n void playerMove() {\n int row, col;\n cout << "Enter the row and column to place your symbol (1-" << size << "): ";\n cin >> row >> col;\n while (row < 1 || row > size || col < 1 || col > size || board[row-1][col-1] != '') {\n cout << "Invalid move. Please enter again: ";\n cin >> row >> col;\n }\n board[row-1][col-1] = playerSymbol;\n }\n\n void computerMove() {\n int maxScore = 0;\n int maxRow, maxCol;\n for (int i = 0; i < size; i++) {\n for (int j = 0; j < size; j++) {\n if (board[i][j] == '') {\n int score = calculateScore(i, j);\n if (score > maxScore) {\n maxScore = score;\n maxRow = i;\n maxCol = j;\n }\n }\n }\n }\n board[maxRow][maxCol] = computerSymbol;\n cout << "Computer placed symbol at row " << maxRow+1 << ", column " << maxCol+1 << endl;\n }\n\n int calculateScore(int row, int col) {\n int score = 0;\n // Check row\n int xCount = 0, oCount = 0, emptyCount = 0;\n for (int j = 0; j < size; j++) {\n if (board[row][j] == 'X') {\n xCount++;\n } else if (board[row][j] == 'O') {\n oCount++;\n } else {\n emptyCount++;\n }\n }\n score += (xCount == size-1 && emptyCount == 1) ? 50 : 0;\n score += (oCount == size-1 && emptyCount == 1) ? 25 : 0;\n score += (xCount == size-2 && emptyCount == 2) ? 10 : 0;\n score += (oCount == size-2 && emptyCount == 2) ? 8 : 0;\n score += (emptyCount == size) ? 4 : 0;\n\n // Check column\n xCount = 0, oCount = 0, emptyCount = 0;\n for (int i = 0; i < size; i++) {\n if (board[i][col] == 'X') {\n xCount++;\n } else if (board[i][col] == 'O') {\n oCount++;\n } else {\n emptyCount++;\n }\n }\n score += (xCount == size-1 && emptyCount == 1) ? 50 : 0;\n score += (oCount == size-1 && emptyCount == 1) ? 25 : 0;\n score += (xCount == size-2 && emptyCount == 2) ? 10 : 0;\n score += (oCount == size-2 && emptyCount == 2) ? 8 : 0;\n score += (emptyCount == size) ? 4 : 0;\n\n // Check diagonal (top-left to bottom-right)\n if (row == col) {\n xCount = 0, oCount = 0, emptyCount = 0;\n for (int i = 0; i < size; i++) {\n if (board[i][i] == 'X') {\n xCount++;\n } else if (board[i][i] == 'O') {\n oCount++;\n } else {\n emptyCount++;\n }\n }\n score += (xCount == size-1 && emptyCount == 1) ? 50 : 0;\n score += (oCount == size-1 && emptyCount == 1) ? 25 : 0;\n score += (xCount == size-2 && emptyCount == 2) ? 10 : 0;\n score += (oCount == size-2 && emptyCount == 2) ? 8 : 0;\n score += (emptyCount == size) ? 4 : 0;\n }\n\n // Check diagonal (top-right to bottom-left)\n if (row + col == size - 1) {\n xCount = 0, oCount = 0, emptyCount = 0;\n for (int i = 0; i < size; i++) {\n if (board[i][size-i-1] == 'X') {\n xCount++;\n } else if (board[i][size-i-1] == 'O') {\n oCount++;\n } else {\n emptyCount++;\n }\n }\n score += (xCount == size-1 && emptyCount == 1) ? 50 : 0;\n score += (oCount == size-1 && emptyCount == 1) ? 25 : 0;\n score += (xCount == size-2 && emptyCount == 2) ? 10 : 0;\n score += (oCount == size-2 && emptyCount == 2) ? 8 : 0;\n score += (emptyCount == size) ? 4 : 0;\n }\n\n return score;\n }\n\n bool isGameOver() {\n // Check rows\n for (int i = 0; i < size; i++) {\n bool rowWin = true;\n for (int j = 0; j < size-1; j++) {\n if (board[i][j] != board[i][j+1]) {\n rowWin = false;\n break;\n }\n }\n if (rowWin && board[i][0] != '') {\n return true;\n }\n }\n\n // Check columns\n for (int j = 0; j < size; j++) {\n bool colWin = true;\n for (int i = 0; i < size-1; i++) {\n if (board[i][j] != board[i+1][j]) {\n colWin = false;\n break;\n }\n }\n if (colWin && board[0][j] != '') {\n return true;\n }\n }\n\n // Check diagonal (top-left to bottom-right)\n bool diag1Win = true;\n for (int i = 0; i < size-1; i++) {\n if (board[i][i] != board[i+1][i+1]) {\n diag1Win = false;\n break;\n }\n }\n if (diag1Win && board[0][0] != '') {\n return true;\n }\n\n // Check diagonal (top-right to bottom-left)\n bool diag2Win = true;\n for (int i = 0; i < size-1; i++) {\n if (board[i][size-i-1] != board[i+1][size-i-2]) {\n diag2Win = false;\n break;\n }\n }\n if (diag2Win && board[0][size-1] != '*') {\n return true;\n }\n\n return false;\n }\n\n void printBoard() {\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\nint main() {\n int size;\n cout << "Enter the size of the board: ";\n cin >> size;\n TicTacToe game(size);\n game.startGame();\n return 0;\n

C++ Tic-Tac-Toe Game with AI:  A Complete Guide

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

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