在井字游戏中测试可能的胜利

Test for possible wins in tictactoe game

我正在制作一款井字游戏,每走一步我都需要测试玩家是否赢了,这给我带来了很多麻烦。我有一个包含所有可能获胜组合的二维向量:

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 向量并将 player1 和 player2 向量附加到它们标记的任何单元格中的每一步:

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++;
    }
}

然后我尝试测试每个玩家单元向量中的单元是否是单元的获胜组合,这对我来说很难:

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;
                }
            }
        }
    }
}

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

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

你让事情有点复杂了。
您不需要先收集玩家使用过哪些位置,然后查看其中有多少位置处于获胜位置。
由于您已经知道获胜位置,因此您只需要检查是否有一个玩家占据了所有位置。

假设' '标记了一个空方块,

for(const auto& win: possibleWins)
{
    if (board[win[0]] == board[win[1]] 
     && board[win[1]] == board[win[2]]
     && board[win[0]] != ' ')
    {
        // if board[win[0]] == 'X' , then X won
        // if board[win[0]] == 'O' , then O 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

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

   |   |
---+---+---
   |   |   
---+---+---
   |   |   

是一个 int myArray[9];然后你把玩家移动到它上面 myArray[played_position] = playerValue;,然后你可以使用你的 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}}; 检查 3 次移动后位置是否对应于相同的值(玩家!)...

for(int idx=0; idx<possibleWins.size(); idx++)
{
   if(myArray[possibleWins[idx][0]] == myArray[possibleWins[idx][1]] == myArray[possibleWins[idx][2]])
      {
          return myArray[possibleWins[idx][0];
      }
 }

只是一个想法,希望对你的阐述有所帮助....