#include #include using namespace std;

class TicTacToe { private: int size; vector<vector> board; char computerSymbol; char playerSymbol;

public: TicTacToe(int s) { size = s; board.resize(size, vector(size, '*')); }

void startGame() {
    cout << "Welcome to Tic Tac Toe!" << endl;
    cout << "Choose your symbol (X/O): ";
    cin >> playerSymbol;
    while (playerSymbol != 'X' && playerSymbol != 'O') {
        cout << "Invalid symbol. Please choose again: ";
        cin >> playerSymbol;
    }

    cout << "Do you want to play first? (Y/N): ";
    char choice;
    cin >> choice;
    while (choice != 'Y' && choice != 'N') {
        cout << "Invalid choice. Please choose again: ";
        cin >> choice;
    }

    if (choice == 'Y') {
        cout << "You play first." << endl;
        computerSymbol = (playerSymbol == 'X') ? 'O' : 'X';
        printBoard();
        playGame();
    } else {
        cout << "Computer plays first." << endl;
        computerSymbol = (playerSymbol == 'X') ? 'X' : 'O';
        playGame();
    }
}

void playGame() {
    bool playerTurn = (playerSymbol == 'X');
    bool gameOver = false;
    while (!gameOver) {
        if (playerTurn) {
            playerMove();
            printBoard();
            if (isGameOver()) {
                cout << "You win!" << endl;
                gameOver = true;
            }
            playerTurn = false;
        } else {
            computerMove();
            printBoard();
            if (isGameOver()) {
                cout << "Computer wins!" << endl;
                gameOver = true;
            }
            playerTurn = true;
        }
    }
}

void playerMove() {
    int row, col;
    cout << "Enter the row and column to place your symbol (1-" << size << "): ";
    cin >> row >> col;
    while (row < 1 || row > size || col < 1 || col > size || board[row-1][col-1] != '*') {
        cout << "Invalid move. Please enter again: ";
        cin >> row >> col;
    }
    board[row-1][col-1] = playerSymbol;
}

void computerMove() {
    int maxScore = 0;
    int maxRow, maxCol;
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            if (board[i][j] == '*') {
                int score = calculateScore(i, j);
                if (score > maxScore) {
                    maxScore = score;
                    maxRow = i;
                    maxCol = j;
                }
            }
        }
    }
    board[maxRow][maxCol] = computerSymbol;
    cout << "Computer placed symbol at row " << maxRow+1 << ", column " << maxCol+1 << endl;
}

int calculateScore(int row, int col) {
    int score = 0;
    // Check row
    int xCount = 0, oCount = 0, emptyCount = 0;
    for (int j = 0; j < size; j++) {
        if (board[row][j] == 'X') {
            xCount++;
        } else if (board[row][j] == 'O') {
            oCount++;
        } else {
            emptyCount++;
        }
    }
    score += (xCount == size-1 && emptyCount == 1) ? 50 : 0;
    score += (oCount == size-1 && emptyCount == 1) ? 25 : 0;
    score += (xCount == size-2 && emptyCount == 2) ? 10 : 0;
    score += (oCount == size-2 && emptyCount == 2) ? 8 : 0;
    score += (emptyCount == size) ? 4 : 0;

    // Check column
    xCount = 0, oCount = 0, emptyCount = 0;
    for (int i = 0; i < size; i++) {
        if (board[i][col] == 'X') {
            xCount++;
        } else if (board[i][col] == 'O') {
            oCount++;
        } else {
            emptyCount++;
        }
    }
    score += (xCount == size-1 && emptyCount == 1) ? 50 : 0;
    score += (oCount == size-1 && emptyCount == 1) ? 25 : 0;
    score += (xCount == size-2 && emptyCount == 2) ? 10 : 0;
    score += (oCount == size-2 && emptyCount == 2) ? 8 : 0;
    score += (emptyCount == size) ? 4 : 0;

    // Check diagonal (top-left to bottom-right)
    if (row == col) {
        xCount = 0, oCount = 0, emptyCount = 0;
        for (int i = 0; i < size; i++) {
            if (board[i][i] == 'X') {
                xCount++;
            } else if (board[i][i] == 'O') {
                oCount++;
            } else {
                emptyCount++;
            }
        }
        score += (xCount == size-1 && emptyCount == 1) ? 50 : 0;
        score += (oCount == size-1 && emptyCount == 1) ? 25 : 0;
        score += (xCount == size-2 && emptyCount == 2) ? 10 : 0;
        score += (oCount == size-2 && emptyCount == 2) ? 8 : 0;
        score += (emptyCount == size) ? 4 : 0;
    }

    // Check diagonal (top-right to bottom-left)
    if (row + col == size - 1) {
        xCount = 0, oCount = 0, emptyCount = 0;
        for (int i = 0; i < size; i++) {
            if (board[i][size-i-1] == 'X') {
                xCount++;
            } else if (board[i][size-i-1] == 'O') {
                oCount++;
            } else {
                emptyCount++;
            }
        }
        score += (xCount == size-1 && emptyCount == 1) ? 50 : 0;
        score += (oCount == size-1 && emptyCount == 1) ? 25 : 0;
        score += (xCount == size-2 && emptyCount == 2) ? 10 : 0;
        score += (oCount == size-2 && emptyCount == 2) ? 8 : 0;
        score += (emptyCount == size) ? 4 : 0;
    }

    return score;
}

bool isGameOver() {
    // Check rows
    for (int i = 0; i < size; i++) {
        bool rowWin = true;
        for (int j = 0; j < size-1; j++) {
            if (board[i][j] != board[i][j+1]) {
                rowWin = false;
                break;
            }
        }
        if (rowWin && board[i][0] != '*') {
            return true;
        }
    }

    // Check columns
    for (int j = 0; j < size; j++) {
        bool colWin = true;
        for (int i = 0; i < size-1; i++) {
            if (board[i][j] != board[i+1][j]) {
                colWin = false;
                break;
            }
        }
        if (colWin && board[0][j] != '*') {
            return true;
        }
    }

    // Check diagonal (top-left to bottom-right)
    bool diag1Win = true;
    for (int i = 0; i < size-1; i++) {
        if (board[i][i] != board[i+1][i+1]) {
            diag1Win = false;
            break;
        }
    }
    if (diag1Win && board[0][0] != '*') {
        return true;
    }

    // Check diagonal (top-right to bottom-left)
    bool diag2Win = true;
    for (int i = 0; i < size-1; i++) {
        if (board[i][size-i-1] != board[i+1][size-i-2]) {
            diag2Win = false;
            break;
        }
    }
    if (diag2Win && board[0][size-1] != '*') {
        return true;
    }

    return false;
}

void printBoard() {
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            cout << board[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
}

};

int main() { int size; cout << "Enter the size of the board: "; cin >> size; TicTacToe game(size); game.startGame(); return 0;

写c++程序:2 人机下棋:在3×3棋盘上计算机为一方用户为一方交替画X和O在行、列、对角线上谁先练成一条直线谁就获得胜利。用类的思想完成具体功能要求如下:1状态显示:计算机可正确显示棋盘给出提示信息和胜负判断。2用户选择:用户可以选择是先下还是后下并选择棋子是X还是O。3每一步都用图形化方式顺序输出例如O XX X O 棋盘初始化为表示空格 4棋盘大小设置:完成上述功能后考虑灵活设

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

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