测试tictactoe游戏中可能的胜利

时间:2016-03-08 17:00:27

标签: c++ tic-tac-toe

我正在创建一个tictactoe游戏,我需要测试一下玩家是否赢了,这给了我很多麻烦。我有一个所有可能的胜利组合的2d向量:

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

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

有没有办法可以更好地构建它以使其工作?我很丢失。

3 个答案:

答案 0 :(得分:2)

有两种方法。你的代码有点太复杂了,应该是一个简单的操作,所以我不打算去理解它。

我同意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

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

答案 1 :(得分:0)

你让事情变得复杂一点 您不需要首先收集玩家使用的位置,并查看其中有多少位于获胜位置。
由于您已经知道获胜位置,因此您只需要检查是否有任何一个玩家占用了所有位置。

假设' '标记为空方格,

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

应该这样做。

答案 2 :(得分:0)

myArray[played_position] = playerValue;

是一个int myArray [9];并且你让玩家移动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}};,然后如果位置对应相同的值(玩家!),你可以使用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]; } } 在3移动后进行检查......

{{1}}

这只是一个想法,希望它可以帮助你详细说明......