tic-toe-program循环没有关闭

时间:2014-01-05 18:18:29

标签: c++

我的问题是,当满足获胜条件而不是结束程序时,它会回到循环,这决定了是否采取了一个位置。我尝试了没有四次休息,它也没有用。

#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();
}

1 个答案:

答案 0 :(得分:1)

while(winning = 6)将始终返回true,您想要的是while(winning == 6)

表达式winning = 6将返回6,其中bool将等于true。你在循环中做了很多,记住=(赋值)和==相等。