AI-c++小游戏-五子棋

#include <iostream>
#include <vector>

using namespace std;

const int BOARD_SIZE = 15;

enum class Player {
    EMPTY,
    BLACK,
    WHITE
};

class Game {
private:
    vector<vector<Player>> board;
    Player currentPlayer;

public:
    Game() : board(BOARD_SIZE, vector<Player>(BOARD_SIZE, Player::EMPTY)), currentPlayer(Player::BLACK) {}

    void play() {
        bool gameOver = false;
        int row, col;

        while (!gameOver) {
            printBoard();
            cout << "Player " << static_cast<int>(currentPlayer) << "'s turn. Enter row and column (0-14): ";
            cin >> row >> col;

            if (isValidMove(row, col)) {
                board[row][col] = currentPlayer;

                if (isWinningMove(row, col)) {
                    gameOver = true;
                    cout << "Player " << static_cast<int>(currentPlayer) << " wins!" << endl;
                } else {
                    switchPlayer();
                }
            } else {
                cout << "Invalid move. Try again." << endl;
            }
        }
    }

private:
    void printBoard() {
        for (int row = 0; row < BOARD_SIZE; ++row) {
            for (int col = 0; col < BOARD_SIZE; ++col) {
                switch (board[row][col]) {
                    case Player::EMPTY:
                        cout << ".";
                        break;
                    case Player::BLACK:
                        cout << "X";
                        break;
                    case Player::WHITE:
                        cout << "O";
                        break;
                }
                cout << " ";
            }
            cout << endl;
        }
    }

    bool isValidMove(int row, int col) {
        return (row >= 0 && row < BOARD_SIZE) && (col >= 0 && col < BOARD_SIZE) && (board[row][col] == Player::EMPTY);
    }

    bool isWinningMove(int row, int col) {
        Player player = currentPlayer;

        // 检查行
        int count = 1;
        int i = col - 1;
        while (i >= 0 && board[row][i] == player) {
            count++;
            i--;
        }
        i = col + 1;
        while (i < BOARD_SIZE && board[row][i] == player) {
            count++;
            i++;
        }
        if (count >= 5) {
            return true;
        }

        // 检查列
        count = 1;
        i = row - 1;
        while (i >= 0 && board[i][col] == player) {
            count++;
            i--;
        }
        i = row + 1;
        while (i < BOARD_SIZE && board[i][col] == player) {
            count++;
            i++;
        }
        if (count >= 5) {
            return true;
        }

        // 检查主对角线
        count = 1;
        int j = col - 1;
        i = row - 1;
        while (i >= 0 && j >= 0 && board[i][j] == player) {
            count++;
            i--;
            j--;
        }
        j = col + 1;
        i = row + 1;
        while (i < BOARD_SIZE && j < BOARD_SIZE && board[i][j] == player) {
            count++;
            i++;
            j++;
        }
        if (count >= 5) {
            return true;
        }

        // 检查副对角线
        count = 1;
        j = col - 1;
        i = row + 1;
        while (i < BOARD_SIZE && j >= 0 && board[i][j] == player) {
            count++;
            i++;
            j--;
        }
        j = col + 1;
        i = row - 1;
        while (i >= 0 && j < BOARD_SIZE && board[i][j] == player) {
            count++;
            i--;
            j++;
        }
        if (count >= 5) {
            return true;
        }

        return false;
    }

    void switchPlayer() {
        currentPlayer = (currentPlayer == Player::BLACK) ? Player::WHITE : Player::BLACK;
    }
};

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

    return 0;
}

五子棋人工智能(AI)的C++代码通常会包含以下几个关键部分: 1. **游戏规则引擎**:用于判断当前棋盘状态是否满足胜利条件,如形成连续的五个同色棋子。 2. **搜索算法**:比如最小最大搜索(MiniMax)、Alpha-Beta剪枝,甚至是更复杂的像蒙特卡洛树搜索(MCTS),它模拟大量随机走法以评估潜在的最优策略。 3. **评估函数**:AI需要一种方法来给棋局打分,通常是基于棋子的位置、形状和对手可能的反击等。 4. **下棋决策**:结合搜索结果和评估函数,选择最佳下一步棋。 5. **用户界面**:接收用户的输入,展示棋盘状态,并显示AI的行动。 编写这样的代码需要对计算机科学和博弈论有深入理解,特别是对于搜索算法和数据结构的运用。这里提供一个简化版的框架: ```cpp #include <iostream> #include <vector> // 棋盘表示 struct Board { std::vector<std::string> grid; }; // AI函数原型 int minimax(Board& board, bool isPlayer); // 主函数示例 int main() { Board board = ... // 初始化棋盘 int move = minimax(board, false); // 开始AI思考并获取其移动 printMove(move); // 输出AI的移动 return 0; } // MiniMax核心算法 int minimax(Board& board, bool isPlayer) { if (isGameOver(board)) { return evaluateBoard(board); } int bestScore = -INFINITY; for (int i = 0; i < numMoves; i++) { makeMove(i, board); bestScore = std::max(bestScore, minimax(board, !isPlayer)); undoMove(); // 回溯到上一步 } return bestScore; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值