简体   繁体   English

while循环无法结束(C ++)

[英]Can't end while loop (C++)

I can't seem to end this while loop and I have tried every single way I know but it's still not working. 我似乎无法在while循环中结束此操作,并且尝试了所有已知方法,但仍然无法正常工作。 The variable "contGame" just kept changing to "true" even if I have already changed it to "false". 即使我已经将变量“ contGame”更改为“ false”,它仍保持更改为“ true”。 Can you guys help me! 你们能帮我吗! Note that this inside a class. 注意,这在一个类里面。 Here is my code: 这是我的代码:

while (contGame == true)
    {
        if (turn == 1)
        {
            cout << Board._player1Name <<"'s turn!!\n";
            X = getMoveX();
            Y = getMoveY();

            Board.Board[X][Y] = player1Sign;
            Board.printBoard();
            turn = turn + 1;
            Board.checkGame(contGame, player1Sign, player2Sign);
            if (checkGame(contGame, player1Sign, player2Sign) == false)
            {
                bool contGame = false;
                cout << contGame << endl;
            }
        } 
        cout << contGame << endl;
        if(turn == 2)
        {
            cout << Board._player2Name <<"'s turn!!\n";
            X = getMoveX();
            Y = getMoveY();

            Board.Board[X][Y] = player2Sign;
            Board.printBoard();
            turn = turn - 1;
            Board.checkGame(contGame, player1Sign, player2Sign);
            if (checkGame(contGame, player1Sign, player2Sign) == false)
            {
                int contGame = 1;
                cout << contGame << endl;
            }
        }
    }  

And this is checkGame: 这是checkGame:

bool contGameA = true;
for (int k = 0; k < 3; k++)
    {

        if (Board[k][0] == Board[k][1] && Board[k][1] == Board[k][2] && Board[k][2] == player1Sign)
        {
            cout << "\\\\" << _player1Name <<" wins!////\n";
            contGameA = false;
        }
        else if (Board[k][0] == Board[k][1] && Board[k][1] == Board[k][2] && Board[k][2] == player2Sign)
        {
            cout << "\\\\" << _player2Name << " wins!////\n";
            contGameA = false;
        }
    }

for (int r = 0; r < 3; r++)
    {
        if (Board[0][r] == Board[1][r] && Board[1][r] == Board[2][r] && Board[2][r] == player1Sign)
        {
            cout << "\\\\" << _player1Name << " wins!////\n";
            contGameA = false;
        }
        else if (Board[0][r] == Board[1][r] && Board[1][r] == Board[2][r] && Board[2][r] == player2Sign)
        {
            cout << "\\\\" << _player2Name << " wins!////\n";
            contGameA = false;
        }   
    }

if (Board[0][0] == Board[1][1] && Board[1][1] == Board[2][2] && Board[2][2] == player1Sign)
    {
        cout << "\\\\" << _player1Name << " wins!////\n";
        contGameA = false;
    } else if (Board[0][0] == Board[1][1] && Board[1][1] == Board[2][2] && Board[2][2] == player2Sign)
    {
        cout << "\\\\" << _player2Name << " wins!////\n";
        contGameA = false;
    }

if (Board[0][2] == Board[1][1] && Board[1][1] == Board[2][0] && Board[2][0] == player1Sign)
    {
        cout << "\\\\" << _player1Name << " wins!////\n";
        contGameA = false;
    } else if (Board[0][2] == Board[1][1] && Board[1][1] == Board[2][0] && Board[2][0] == player2Sign)
    {
        cout << "\\\\" << _player2Name << " wins!////\n";
        contGameA = false;
    }
return contGameA;

You are actually creating contGame variable in the nested scope of your if statement and this is hiding contGame variable declared in the outer scope. 您实际上是在if语句的嵌套作用域中创建contGame变量,而这隐藏了在外部作用域中声明的contGame变量。 As a result you never change the outer contGame variable 结果,您永远不会更改外部的contGame变量

eg 例如

if (checkGame(contGame, player1Sign, player2Sign) == false)
{
    // bool contGame = false; should be:
    contGame = false;
    cout << contGame << endl;
}

and

if (checkGame(contGame, player1Sign, player2Sign) == false)
{
    // int contGame = 1; should be
    contGame = true;
    cout << contGame << endl;
}

You keep declaring new variables with the name contGame in nested scopes, and modifying those. 您在嵌套作用域中不断声明名称为contGame新变量,并对其进行修改。 These are entirely separate variables, which (in their scope) hide the outer variable contGame . 这些是完全独立的变量,(在它们的范围内) 隐藏了外部变量contGame It's that outer one on whose value the while loop depends. while循环所依赖的值就是外部值。

Remove the bool and int from the lines: 从行中删除boolint

bool contGame = false;

// and

int contGame = 1;

to have these lines affect the outer contGame variable. 使这些行影响外部的contGame变量。 Also note that 1 is converted to true . 另请注意, 1转换为true

The scope of the variables only falls within the brackets when declared inside a any loop. 变量的范围仅在任何循环内声明时才放在方括号内。 Remove the bool and int and just put 删除boolint然后放入

if (checkGame(contGame, player1Sign, player2Sign) == false)
{
contGame = false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM