2014-01-05 135 views
-2

我的問題是,如果滿足獲勝條件而不是結束程序,它會返回到決定是否採取某個職位的循環。我嘗試了沒有四個休息時間,也沒有工作。tic-toe-program循環不關閉

#include <iostream> 
using namespace std; 

int main() 
{ 
    int a,b,c,d,e,f,g,h,i,x,y,winning,repeat,q,r; 
    int array[3][3] = {{a,b,c},{d,e,f},{g,h,i}}; 

    winning=6; 

for(q = 0;q<=2;q++) 
{ 
     for(r = 0;r<=2;r++) 
      { 
       array[q][r]=0; 
       cout << "Row: " << q << "Column: " << r << "Value: " << array[q][r] << endl; 
      } 
} 


while (winning = 6)  
{ 
    cout << "Player 1 Row: "; 
    cin >> x; 

    cout << "Player 1 Column: "; 
    cin >> y; 

    cout << " " << endl; 

    if (array[x][y] != 0) 
    { 
    repeat=1; 
      while (repeat=1) 
      { 
      cout << "That row is taken: "; 

      cout << "Player 1 Row: "; 
      cin >> x; 

      cout << "Player 1 Column: "; 
      cin >> y; 

      cout << " " << endl; 

      if (array[x][y] == 0) 
      { 
       repeat=0; 
      } 
      } 
    } 

    array[x][y] = 1; 
    cout << array[x][y]; 


    cout << "Player 2 Row: "; 
    cin >> x; 

    cout << "Player 2 Column: "; 
    cin >> y; 

    cout << " " << endl; 

    if (array[x][y] != 0) 
    { 
    repeat=1; 
      while (repeat=1) 
      { 
      cout << "That row is taken: " << endl; 

      cout << "Player 2 Row: "; 
      cin >> x; 

      cout << "Player 2 Column: "; 
      cin >> y; 

      cout << " " << endl; 

      if (array[x][y] == 0) 
      { 
       repeat=0; 
      } 
      } 
    } 

    array[x][y] = 2; 

    if (array[0][0] != 0) 
    { 
    if (array[0][1] != 0) 
    { 
    if (array[0][0] == array[0][1]) 
    { 
    if (array[0][1] == array[0][2]) 
    { 
    winning=array[0][0]; 
    break; 
    break; 
    break; 
    break; 
    } 
    } 
    } 
    } 

    if (array[1][0] != 0) 
    { 
    if (array[1][1] != 0) 
    {  
    if (array[1][0] == array[1][1]) 
    { 
    if (array[1][1] == array[1][2]) 
    { 
    winning=array[1][0]; 
    break; 
    break; 
    break; 
    break; 
    } 
    } 
    } 
    } 

    if (array[1][0] != 0) 
    { 
    if (array[1][1] != 0) 
    {   
    if (array[2][0] == array[2][1]) 
    { 
    if (array[2][1] == array[2][2]) 
    { 
    winning=array[2][0]; 
    break; 
    break; 
    break; 
    break; 
    } 
    } 


    if (array[0][0] == array[1][0]) 
    { 
    if (array[1][0] == array[2][0]) 
    { 
    winning=array[0][0]; 
    break; 
    break; 
    break; 
    break; 
    } 
    } 
    } 
    } 

    if (array[1][0] != 0) 
    { 
    if (array[1][1] != 0) 
    {  
    if (array[0][1] == array[1][1]) 
    { 
    if (array[1][1] == array[2][1]) 
    { 
    winning=array[0][1]; 
    break; 
    break; 
    break; 
    break; 
    } 
    } 
    } 
    } 

    if (array[1][0] != 0) 
    { 
    if (array[1][1] != 0) 
    {  
    if (array[0][2] == array[1][2]) 
    { 
    if (array[1][2] == array[2][2]) 
    { 
    winning=array[0][2]; 
    break; 
    break; 
    break; 
    break; 
    } 
    } 
    } 
    } 

    if (array[1][0] != 0) 
    { 
    if (array[1][1] != 0) 
    {  
    if (array[0][1] == array[1][1]) 
    { 
    if (array[1][1] == array[2][2]) 
    { 
    winning=array[0][1]; 
    break; 
    break; 
    break; 
    break; 
    } 
    } 
    } 
    } 

    if (array[1][0] != 0) 
    { 
    if (array[1][1] != 0) 
    {  
    if (array[0][2] == array[1][1]) 
    { 
    if (array[1][1] == array[2][2]) 
    { 
    winning=array[0][2]; 
    break; 
    break; 
    break; 
    break; 
    } 
    } 
    } 
    }  
} 
cin.ignore(); 
cin.get(); 
} 
+0

break; break; break; break;? -1 – HAL9000

回答

1

while(winning = 6)將始終返回true,你想要的是while(winning == 6)

表達winning = 6將返回6,鑄造到bool將等於true。你在循環中做了很多事情,記得=(賦值)和==相等。

+0

謝謝關閉程序時,我改變了,但只有當它通過循環決定是否再次採取一個位置幾次。 – user3144703