以下是一个满足上述要求的 C++ 代码示例:

#include <iostream>
#include <vector>

using namespace std;

class TicTacToe {
private:
    vector<vector<char>> board; // 棋盘
    char player; // 当前玩家
    char computer; // 计算机玩家
    bool isComputerTurn; // 是否计算机下棋

public:
    TicTacToe() {
        // 初始化棋盘
        board.resize(3, vector<char>(3, '*'));
        player = ' ';
        computer = ' ';
        isComputerTurn = false;
    }

    // 设置玩家和计算机
    void setPlayers(char playerChoice, char computerChoice) {
        player = playerChoice;
        computer = computerChoice;
        if (computer == 'X') {
            isComputerTurn = true;
        } else {
            isComputerTurn = false;
        }
    }

    // 显示棋盘
    void displayBoard() {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                cout << board[i][j] << " ";
            }
            cout << endl;
        }
    }

    // 检查是否获胜
    bool checkWin() {
        // 检查行
        for (int i = 0; i < 3; i++) {
            if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != '*') {
                return true;
            }
        }

        // 检查列
        for (int i = 0; i < 3; i++) {
            if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != '*') {
                return true;
            }
        }

        // 检查对角线
        if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != '*') {
            return true;
        }
        if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != '*') {
            return true;
        }

        return false;
    }

    // 检查是否平局
    bool checkTie() {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (board[i][j] == '*') {
                    return false;
                }
            }
        }
        return true;
    }

    // 计算空位得分
    int calculateScore(int row, int col) {
        int score = 0;
        char opponent = (player == 'X') ? 'O' : 'X';

        // 检查行
        if (board[row][0] == board[row][1] && board[row][1] == player) {
            score += 50;
        } else if (board[row][0] == board[row][1] && board[row][1] == opponent) {
            score += 25;
        } else if (board[row][0] == player || board[row][1] == player) {
            score += 10;
        } else if (board[row][0] == opponent || board[row][1] == opponent) {
            score += 8;
        } else {
            score += 4;
        }

        // 检查列
        if (board[0][col] == board[1][col] && board[1][col] == player) {
            score += 50;
        } else if (board[0][col] == board[1][col] && board[1][col] == opponent) {
            score += 25;
        } else if (board[0][col] == player || board[1][col] == player) {
            score += 10;
        } else if (board[0][col] == opponent || board[1][col] == opponent) {
            score += 8;
        } else {
            score += 4;
        }

        // 检查对角线
        if (row == col) {
            if (board[0][0] == board[1][1] && board[1][1] == player) {
                score += 50;
            } else if (board[0][0] == board[1][1] && board[1][1] == opponent) {
                score += 25;
            } else if (board[0][0] == player || board[1][1] == player) {
                score += 10;
            } else if (board[0][0] == opponent || board[1][1] == opponent) {
                score += 8;
            } else {
                score += 4;
            }
        }

        if (row + col == 2) {
            if (board[0][2] == board[1][1] && board[1][1] == player) {
                score += 50;
            } else if (board[0][2] == board[1][1] && board[1][1] == opponent) {
                score += 25;
            } else if (board[0][2] == player || board[1][1] == player) {
                score += 10;
            } else if (board[0][2] == opponent || board[1][1] == opponent) {
                score += 8;
            } else {
                score += 4;
            }
        }

        return score;
    }

    // 计算下一步最优位置
    void calculateNextMove() {
        int bestScore = -1;
        int bestRow = -1;
        int bestCol = -1;

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (board[i][j] == '*') {
                    int score = calculateScore(i, j);
                    if (score > bestScore) {
                        bestScore = score;
                        bestRow = i;
                        bestCol = j;
                    }
                }
            }
        }

        board[bestRow][bestCol] = computer;
        isComputerTurn = false;
    }

    // 玩家下棋
    void playerMove() {
        int position;
        cout << "请输入要下棋的位置(1-9):";
        cin >> position;

        int row = (position - 1) / 3;
        int col = (position - 1) % 3;

        if (board[row][col] != '*') {
            cout << "该位置已经有棋子,请重新输入!" << endl;
            playerMove();
        } else {
            board[row][col] = player;
            isComputerTurn = true;
        }
    }

    // 游戏循环
    void play() {
        char choice;
        cout << "请选择先下棋还是后下棋(先下请输入'Y'或'y',后下请输入'N'或'n'):";
        cin >> choice;

        if (choice == 'Y' || choice == 'y') {
            isComputerTurn = false;
        } else if (choice == 'N' || choice == 'n') {
            isComputerTurn = true;
        } else {
            cout << "输入无效,请重新选择!" << endl;
            play();
        }

        cout << "请选择棋子('X'或'O'):";
        cin >> choice;

        if (choice == 'X' || choice == 'x') {
            setPlayers('X', 'O');
        } else if (choice == 'O' || choice == 'o') {
            setPlayers('O', 'X');
        } else {
            cout << "输入无效,请重新选择!" << endl;
            play();
        }

        while (true) {
            displayBoard();

            if (isComputerTurn) {
                cout << "计算机下棋:" << endl;
                calculateNextMove();
            } else {
                playerMove();
            }

            if (checkWin()) {
                if (isComputerTurn) {
                    cout << "计算机获胜!" << endl;
                } else {
                    cout << "玩家获胜!" << endl;
                }
                displayBoard();
                break;
            }

            if (checkTie()) {
                cout << "平局!" << endl;
                displayBoard();
                break;
            }
        }
    }
};

int main() {
    TicTacToe game;
    game.play();

    return 0;
}

这个代码使用了类TicTacToe来表示游戏,其中包含了棋盘、玩家、计算机等相关信息和方法。在play方法中,根据用户的选择设置游戏参数,然后通过循环交替进行玩家和计算机的下棋,并检查胜利和平局条件。在计算机下棋时,使用了简单的得分计算方法来选择下一步最优位置。最后,根据游戏结果输出相应的信息。

C++ 实现人机对战井字棋游戏

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

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