六子棋棋盘博弈树

发布时间:2024年01月10日

#include <iostream>
#include <vector>

// 定义棋盘大小
const int BOARD_SIZE = 6;

// 定义棋盘状态
enum class BoardState { Empty, X, O };

// 定义棋盘
using Board = std::vector<std::vector<BoardState>>;

// 定义博弈树节点
struct Node {
    Board board;  // 当前棋盘状态
    std::vector<Node> children;  // 子节点
};

// 检查是否有玩家获胜
bool checkWin(const Board& board, BoardState player) {
    // 检查行
    for (int i = 0; i < BOARD_SIZE; i++) {
        int count = 0;
        for (int j = 0; j < BOARD_SIZE; j++) {
            if (board[i][j] == player) {
                count++;
            } else {
                count = 0;
            }
            if (count >= 4) {
                return true;
            }
        }
    }

    // 检查列
    for (int i = 0; i < BOARD_SIZE; i++) {
        int count = 0;
        for (int j = 0; j < BOARD_SIZE; j++) {
            if (board[j][i] == player) {
                count++;
            } else {
                count = 0;
            }
            if (count >= 4) {
                return true;
            }
        }
    }

    // 检查对角线
    for (int i = 0; i < BOARD_SIZE - 3; i++) {
        for (int j = 0; j < BOARD_SIZE - 3; j++) {
            if (board[i][j] == player && board[i+1][j+1] == player && board[i+2][j+2] == player && board[i+3][j+3] == player) {
                return true;
            }
        }
    }

    // 检查反对角线
    for (int i = 0; i < BOARD_SIZE - 3; i++) {
        for (int j = BOARD_SIZE - 1; j >= 3; j--) {
            if (board[i][j] == player && board[i+1][j-1] == player && board[i+2][j-2] == player && board[i+3][j-3] == player) {
                return true;
            }
        }
    }

    return false;
}

// 生成博弈树
Node generateGameTree(const Board& board, BoardState currentPlayer) {
    Node node;
    node.board = board;

    // 生成所有可能的下一步
    for (int i = 0; i < BOARD_SIZE; i++) {
        for (int j = 0; j < BOARD_SIZE; j++) {
            if (board[i][j] == BoardState::Empty) {
                Board newBoard = board;
                newBoard[i][j] = currentPlayer;
                node.children.push_back(generateGameTree(newBoard, currentPlayer == BoardState::X ? BoardState::O : BoardState::X));
            }
        }
    }

    return node;
}

// 打印棋盘
void printBoard(const Board& board) {
    for (int i = 0; i < BOARD_SIZE; i++) {
        for (int j = 0; j < BOARD_SIZE; j++) {
            switch (board[i][j]) {
                case BoardState::Empty:
                    std::cout << ".";
                    break;
                case BoardState::X:
                    std::cout << "X";
                    break;
                case BoardState::O:
                    std::cout << "O";
                    break;
            }
        }
        std::cout << std::endl;
    }
}

int main() {
    // 创建初始棋盘
    Board board(BOARD_SIZE, std::vector<BoardState>(BOARD_SIZE, BoardState::Empty));

    // 生成博弈树
    Node gameTree = generateGameTree(board, BoardState::X);

    // 打印博弈树
    printBoard(gameTree.board);

    return 0;
}

生成一个六子棋棋盘的博弈树,并打印出所有可能的下一步棋盘状态。

文章来源:https://blog.csdn.net/LX200384/article/details/135511834
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。