2016-06-21 144 views
2

我似乎無法結束此while循環,我已經嘗試了我知道的每一種方式,但它仍然無法正常工作。即使我已將其更改爲「false」,變量「contGame」也會更改爲「true」。你們能幫我嗎?請注意,這裏面的一個類。這裏是我的代碼:不能結束while循環(C++)

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

這是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; 
+2

雖然我沒有回答這個問題,因爲我碰巧發現錯誤在您發佈的代碼牆,請注意您的問題還遠遠沒有提供[MCVE]因爲「最小」的部分。在發佈之前,你應該儘可能地縮短代碼(當然,保持它重現錯誤)。 – Angew

回答

2

你實際上是建立在你的if語句的嵌套範圍contGame變量,這是隱藏在外部聲明contGame變量。 因此,您永遠不會更改外部contGame變量

例如,

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

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

我刪除了「bool」和「int」,但循環仍然不會結束。任何想法爲什麼? –

1

你保持嵌套範圍的名稱contGame宣佈新的變量,並修改這些。這些是完全獨立的變量,它們(在它們的範圍內)隱藏外部變量contGame。這就是外部的一個,其值while循環取決於。

從線中刪除boolint

bool contGame = false; 

// and 

int contGame = 1; 

有這些行影響外contGame變量。另請注意,1轉換爲true

0

當在任何循環內聲明時,變量的作用域僅落在括號內。 取出boolint,只是把

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