c++ - 测试井字游戏中可能获胜的可能性

标签 c++ tic-tac-toe

我正在创建一个井字棋游戏,我的每一步都需要测试玩家是否获胜,这给我带来了很多麻烦。我有一个所有可能的获胜组合的二维 vector :

vector<vector<int>> possibleWins {{1,2,3},{4,5,6},{7,8,9},{1,4,7},{2,5,8},{3,6,9},{1,5,9},{3,5,7}};

我循环遍历 2d vector 的每一个 Action ,并附加player1和player2 vector 及其标记的任何单元格:

vector<int> totalX {};
vector<int> totalO {};
int count = 1;
for(int i=0; i<3; i++) {
    for(int j=0; j<3; j++) {
        if(board[i][j] == 'x') {
            if(totalX.size() == 0) {
                totalX.push_back(count);
            }
            for(int k=0; k<totalX.size(); k++) {
                if(count == totalX[k]) {
                    break;
                }
                else {
                    totalX.push_back(count);
                }
            }
        }
        else if(board[i][j] == 'o') {
            if(totalO.size() == 0) {
                totalO.push_back(count);
            }
            for(int k=0; k<totalO.size(); k++) {
                if(count == totalO[k]) {
                    break;
                }
                else {
                    totalO.push_back(count);
                }
            }
        }
        count++;
    }
}

然后,我尝试测试每个玩家细胞 vector 中的细胞是否是获胜的细胞组合,事实证明这对我来说很困难:

int xInRow = 0;
for(int x=0; x<totalX.size(); x++) {
    for(int y=0; y<possibleWins.size(); y++) {
        xInRow = 0;
        for(int z=0; z<3; z++) {
            if(totalX[x] == possibleWins[y][z]) {
                xInRow++;
                if(xInRow == 3) {
                    return X_WON;
                }
            }
        }
    }
}

这不起作用,我尝试过以多种不同的方式实现它,但老实说,我不知道如何枚举所有可能的胜利并测试玩家是否拥有这些组合之一。

有没有办法可以更好地构建它以使其发挥作用?我对此很迷茫。

最佳答案

有两种方法。您的代码对于一个简单的操作来说有点太复杂了,所以我不会尝试理解它。

我同意 YSC 的观点,即你不需要 std::vector。您知道每次都是 3x3 网格,因此 3x3 枚举数组应该更好。类似的东西

enum TTTState {
    EMPTY=0,
    X,
    O
}

TTState board[3][3];

可以帮你省去很多麻烦。您可以说 board[0][0] 是左上角,board[2][2] 是右下角。

选项 1

我喜欢您关于 possibleWins 的想法,因此使用新的 board[3][3] 数据结构,您可以使用 int Solutions[8][3][ 存储一对数字2] 但这已经有点困惑了。

for each solution in solutions
    for each triplet in solution
        for each pair in triplet
            if board[pair[0]][pair[1]] matches all other pair in triplet
                then whoever has pieces in the row has won

选项 2

这可能更干净。有 3 种可能的获胜方式。水平、垂直和对角线。您可以分别检查这三种方式。

for i = 0 ; i != 3; i++
    type = board[i][0]
    won = true
    for ii = 1; ii != 3; ii++
        if board[i][ii] is not type
            won = false
    if won then you can return the function with who won

for i = 0 ; i != 3; i++
    type = board[0][i]
    won = true
    for ii = 1; ii != 3; ii++
        if board[ii][i] is not type
            won = false
    if won then you can return the function with who won

对角线只能进行硬编码,因为只有两种可能的胜利位置..

关于c++ - 测试井字游戏中可能获胜的可能性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35873422/

相关文章:

java - sleep 功能不会落后于人工智能的轮次

c++ - Tic Tac Toe 和 Minimax - 在微 Controller 上创建不完美的 AI

python - 如何在 Python 中实现 Tic Tac Toe AI 的搜索算法?

c++ - Tic Tac Toe 失败的 MiniMax 算法

c++ - 如何多态地使用copy_if()?

c++ - 如何使用具有两列的模型获取 QComboBox 的当前值?

c++ - 如果你有两个键并且不能使用 boost,首选数据结构?

C++比较2个文件夹中的文件

c++ - GDI 与 Direct2D

artificial-intelligence - 带有 alpha-beta 修剪的量子井字棋 - 状态的最佳表示?