//Tic-Tac-Toe

#include <iostream>
#include <iomanip>

using namespace std;

const char COL = 3,
           ROW = 3;
          
typedef int tttb[ROW][COL]; //tttb tic-tac-toe board

bool winner(tttb, int,bool); //funtion name winner
bool move(bool);

int main()
{
 bool isWinner;
 isWinner = false;
     
         cout<<"isWinner= "<<isWinner<<endl;
         isWinner = move(isWinner);                    
         cout<<"isWinner= "<<isWinner<<endl;
         if (isWinner = true)
         {   system ("pause");
            return 0; }
                             
}

bool winner(tttb my_Table, int ROW, bool isWin)
{   isWin = false;
    cout<<"Checking for winner"<<endl;

    if (my_Table[0][0]== my_Table[1][0]  && my_Table[1][0] == my_Table[2][0])
      {   
          if (my_Table[0][0] == 1)
          {
               cout<<"X is the winner"<<endl;
               isWin = true;
               cout<<"isWin= "<<isWin<<endl;
               return isWin;                  
          }     
          else if (my_Table[0][0] == 0)
          {
               cout<<"O is the winner"<<endl; 
               isWin = true;
               return isWin;                  
          }     
      }
      
    else if (my_Table[0][1]==my_Table[1][1] && my_Table[1][1] == my_Table[2][1])
      {  
          if (my_Table[0][1] == 1)
          {
               cout<<"X is the winner"<<endl;
               isWin = true;
               return isWin;                  
          }
          else if (my_Table[0][1] == 0)
          {
               cout<<"O is the winner"<<endl; 
               isWin = true;
               return isWin;                  
          }          
      }
     
    else if  (my_Table[0][2]==my_Table[1][2] && my_Table[1][2] == my_Table[2][2])
      { 
          if (my_Table[0][2] == 1)
          {
               cout<<"X is the winner"<<endl;
               isWin = true;
               return isWin;                  
          }
          else if (my_Table[0][2] == 0)
          {
               cout<<"O is the winner"<<endl; 
               isWin = true;
               return isWin;                  
          } 
      }
   
    else if  (my_Table[0][0]== my_Table[0][1] && my_Table[0][1] == my_Table[0][2])
      { 
          if (my_Table[0][0] == 1)
          {
               cout<<"X is the winner"<<endl;
               isWin = true;
               return isWin;                  
          }
          else if (my_Table[0][0] == 0)
          {
               cout<<"O is the winner"<<endl; 
               isWin = true;
               return isWin;                  
          } 
      }
  
    else if  (my_Table[1][0]==my_Table[1][1] && my_Table[1][1] == my_Table[1][2])
      { 
          if (my_Table[1][0] == 1)
          {
               cout<<"X is the winner"<<endl;
               isWin = true;
               return isWin;                  
          }
          else if (my_Table[1][0] == 0)
          {
               cout<<"O is the winner"<<endl; 
               isWin = true;
               return isWin;                  
          }
      }
  
    else if  (my_Table[2][0]==my_Table[2][1] && my_Table[2][1] == my_Table[2][2])
      {   
          if (my_Table[2][0] == 1)
          {
               cout<<"X is the winner"<<endl;
               isWin = true;
               return isWin;                  
          }
          else if (my_Table[2][0] == 0)
          {
               cout<<"O is the winner"<<endl; 
               isWin = true;
               return isWin;                  
          } 
      }
  
    else if  (my_Table[0][0]==my_Table[1][1] && my_Table[1][1] == my_Table[2][2])
      {
          if (my_Table[0][0] == 1)
          {
               cout<<"X is the winner"<<endl;
               isWin = true;
               return isWin;                  
          }
          else if (my_Table[0][0] == 0)
          {
               cout<<"O is the winner"<<endl; 
               isWin = true;
               return isWin;                  
          }
      }
  
    else if  (my_Table[2][0]==my_Table[1][1] && my_Table[1][1] == my_Table[0][2])
      {                
          if (my_Table[2][0] == 1)
          {
               cout<<"X is the winner"<<endl;
               isWin = true;
               return isWin;               
          }
          else if (my_Table[2][0] == 0)
          {
               cout<<"O is the winner"<<endl; 
               isWin = true;
               return isWin;              
          }    
      }
    else
      { 
           cout<<"No Winner Yet"<<endl<<endl;
           isWin = false;
           return isWin;
      }  
cout<<"isWin= "<<isWin<<endl; 
}


bool move(bool placement)
{
  int y = 0,
      n = 0,
      locationX,
      locationO,
      myTable[ROW][COL]  =  {{3,3,3},
                             {3,3,3},
                             {3,3,3}};
     
  char g_table[ROW][COL]  = {{0,0,0},
                             {0,0,0},
                             {0,0,0}};  
     
   for(int place=0;place < 9;)
     {       
             // 7 | 8 | 9        0,0 | 0,1 | 0,2
             // ---------        ---------------
             // 4 | 5 | 6        1,0 | 1,1 | 1,2
             // ---------        ---------------
             // 1 | 2 | 3        2,0 | 2,1 | 2,2

             cout<<" 7 " "|" " 8 " "|" " 9 "<<endl; 
      
             cout<<"_""_""_""_""_""_""_""_""_""_""_"<<endl;
      
             cout<<" 4 " "|" " 5 " "|" " 6 "<<endl; 
      
             cout<<"_""_""_""_""_""_""_""_""_""_""_"<<endl;
      
             cout<<" 1 " "|" " 2 " "|" " 3 " ""<<endl<<endl; 
     
             cout<<"Enter the where you want 'X' to go"<<endl<<endl; 
                    cin >> locationX;
                    cout<<endl;
             
             switch (locationX)
              {     
                    case 1: 
                              if (g_table [2][0] == 0)
                              {    
                                   g_table[2][0] = 'X';
                                   myTable[2][0] = 1;
                                   placement = winner(myTable,3,placement);
                                   break;
                              }
                      
                              else
                              {    cout<<"Spot taken. Enter new one"<<endl;   
                                  cin >> locationX;                
                                   placement = winner(myTable,3,placement);           
                                   break;
                             }                               
                                  break;  
                         
                     case 2: 
                              if (g_table[2][1] == 0)
                               {   g_table[2][1] = 'X';
                                   myTable[2][1] = 1;
                                   placement = winner(myTable,3,placement); 
                                  break;
                              }
                       
                              else
                              {    cout<<"Spot taken. Enter new one"<<endl;   
                                  cin >> locationX;                
                                   placement = winner(myTable,3,placement); 
                                   break;
                              }                              
                                  break;  
                         
                     case 3: 
                              if (g_table[2][2] == 0)
                              {    g_table[2][2] = 'X';
                                   myTable[2][2] = 1;
                                   placement = winner(myTable,3,placement); 
                                  break;
                               }
                       
                              else
                              {    cout<<"Spot taken. Enter new one"<<endl;   
                                  cin >> locationX;                
                                   placement = winner(myTable,3,placement); 
                                   break;                                  
                              }                              
                                  break;        
                
    
                     case 4: 
                              if (g_table[1][0] == 0)
                               {   g_table[1][0] = 'X';
                                   myTable[1][0] = 1; 
                                   placement = winner(myTable,3,placement); 
                                   break;
                              }
                           
                              else
                              {    cout<<"Spot taken. Enter new one"<<endl;   
                                  cin >> locationX;                
                                   placement = winner(myTable,3,placement); 
                                   break;
                              }                               
                                  break;  
                        
                     case 5: 
                              if (g_table[1][1] == 0)
                                {  g_table[1][1] = 'X';
                                   myTable[1][1] = 1;
                                   placement = winner(myTable,3,placement); 
                                  break;
                                }
                              
                              else
                              {    cout<<"Spot taken. Enter new one"<<endl;   
                                  cin >> locationX;                
                                   placement = winner(myTable,3,placement); 
                                   break;
                              }                              
                                  break;  
                         
                     case 6: 
                              if (g_table[1][2] == 0)
                               {   g_table[1][2] = 'X';
                                   myTable[1][2] = 1;
                                   placement = winner(myTable,3,placement); 
                                    break;
                               }
                              else
                              {    cout<<"Spot taken. Enter new one"<<endl;   
                                  cin >> locationX;
                                   placement = winner(myTable,3,placement); 
                                   break;
                              }
                                  break;  
                                              
                     case 7: 
                              if (g_table[0][0] == 0)
                              {   g_table[0][0] = 'X';
                                   myTable[0][0] = 1;
                                   placement = winner(myTable,3,placement); 
                                 break;
                              }
                     
                              else
                              {    cout<<"Spot taken. Enter new one"<<endl;   
                                  cin >> locationX;                
                                   placement = winner(myTable,3,placement); 
                                   break;
                              }
                                  break;                                
                              
                     case 8: 
                              if (g_table[0][1] == 0)
                              {   g_table[0][1] = 'X';
                                   myTable[0][1] = 1;
                                   placement = winner(myTable,3,placement); 
                                 break;
                              }
                      
                              else
                              {    cout<<"Spot taken. Enter new one"<<endl;   
                                  cin >> locationX;                
                                   placement = winner(myTable,3,placement); 
                                   break;
                              }
                                  break;                                                             
                     
                     case 9: 
                              if (g_table[0][2] == 0)
                               {   g_table[0][2] = 'X';
                                   myTable[0][2] = 1;
                                   placement = winner(myTable,3,placement); 
                                  break;
                               }
                        
                              else
                              {    cout<<"Spot taken. Enter new one"<<endl;   
                                  cin >> locationX;                
                                   placement = winner(myTable,3,placement); 
                                   break;
                              }
                                  break;                                
                                                                                                             
                     default:
                           { cout<<"Pick one of the options."<<endl<<endl;
                            cin >>locationX;
                                   placement = winner(myTable,3,placement); 
                                   break;
                            }break;
               }                     
                          
              cout<<g_table[0][0]<< "|"<< g_table[0][1]<< "|"<< g_table[0][2]<<endl; 
              
              cout<<"___________"<<endl;
              
              cout<<g_table[1][0]<< "|" <<g_table[1][1]<< "|"<< g_table[1][2]<<endl; 
              
              cout<<"___________"<<endl;
              
              cout<<g_table[2][0]<< "|" <<g_table[2][1]<< "|"<< g_table[2][2]<<endl<<endl;                     
              
                         cout<<endl;              
           ++n;
          ++place;
           if (n == 5)
           {
             place = 10;
             cout<<"Cats Game"<<endl<<endl;         
             }   
          else 
          {      
                 cout<<"Enter the where you want 'O' to go"<<endl<<endl; 
                 cin >> locationO; 
              switch (locationO)
              {                        
                    case 1: 
                              if (g_table [2][0] == 0)
                               {   g_table[2][0] = 'O';
                                   myTable[2][1] = 0;                               
                                   placement = winner(myTable,3,placement); 
                                   break;}
                       
                              else
                              {    cout<<"Spot taken. Enter new one"<<endl;   
                                  cin >> locationO;                
                                   placement = winner(myTable,3,placement); 
                                   break;
                                  }
                                  break;                               
                         
                     case 2: 
                              if (g_table[2][1] == 0)
                               {   g_table[2][1] = 'O';
                                   myTable[2][1] = 0;
                                   placement = winner(myTable,3,placement); 
                                  break;}
                        
                              else
                              {    cout<<"Spot taken. Enter new one"<<endl;   
                                  cin >> locationO;                
                                   placement = winner(myTable,3,placement); 
                                   break;
                                         } 
                                  break;                                           
                                                                      
                     case 3: 
                              if (g_table[2][2] == 0)
                              {    g_table[2][2] = 'O';
                                   myTable[2][2] = 0;
                                   placement = winner(myTable,3,placement); 
                                  break;
                              }
                        
                              else
                              {    cout<<"Spot taken. Enter new one"<<endl;   
                                  cin >> locationO;                
                                   placement = winner(myTable,3,placement); 
                                   break;
                              } 
                                  break;                                
                                                     
                     case 4: 
                              if (g_table[1][0] == 0)
                               {   g_table[1][0] = 'O';
                                   myTable[1][0] = 0;
                                   placement = winner(myTable,3,placement); 
                                   break;
                               }
                         
                              else
                              {    cout<<"Spot taken. Enter new one"<<endl;   
                                  cin >> locationO;                
                                   placement = winner(myTable,3,placement); 
                                   break;
                              }
                                  break;                                
                                                                             
                     case 5: 
                              if (g_table[1][1] == 0)
                              {    g_table[1][1] = 'O';
                                    myTable[1][1] = 0;
                                   placement = winner(myTable,3,placement); 
                                  break;
                              }
                         
                              else
                              {    cout<<"Spot taken. Enter new one"<<endl;   
                                  cin >> locationO;                
                                   placement = winner(myTable,3,placement); 
                                   break;
                              }  
                                  break;                                
                                                                              
                     case 6: 
                              if (g_table[1][2] == 0)
                               {   g_table[1][2] = 'O';
                                     myTable[1][2] = 0;
                                   placement = winner(myTable,3,placement); 
                                    break;
                               }
                      
                              else
                              {    cout<<"Spot taken. Enter new one"<<endl;   
                                  cin >> locationO;                
                                   placement = winner(myTable,3,placement); 
                                   break;
                              } 
                                  break;                                
                                                             
                     case 7: 
                              if (g_table[0][0] == 0)
                              {   g_table[0][0] = 'O';
                                    myTable[0][0] = 0;
                                   placement = winner(myTable,3,placement); 
                                 break;
                              }
                      
                              else
                              {    cout<<"Spot taken. Enter new one"<<endl;   
                                  cin >> locationO;                
                                   placement = winner(myTable,3,placement); 
                                   break;
                              }
                                  break;                                
                              
                     case 8: 
                              if (g_table[0][1] == 0)
                               {  g_table[0][1] = 'O';
                                     myTable[0][1] = 0;
                                   placement = winner(myTable,3,placement); 
                                 break;
                               }
                      
                              else
                              {    cout<<"Spot taken. Enter new one"<<endl;   
                                  cin >> locationO;                
                                   placement = winner(myTable,3,placement); 
                                   break;
                              } 
                                  break;                                
                                                                         
                     case 9: 
                              if (g_table[0][2] == 0)
                              {  g_table[0][2] = 'O';
                                     myTable[0][2] = 0;
                                   placement = winner(myTable,3,placement); 
                                  break;
                              }
                      
                              else
                              {    cout<<"Spot taken. Enter new one"<<endl;   
                                  cin >> locationO;                
                                   placement = winner(myTable,3,placement); 
                                   break;
                              }                     
                                    break;    
          
                     default:
                      {      cout<<"Pick one of the options."<<endl<<endl;
                             cin >>locationO;
                                   placement = winner(myTable,3,placement); 
                     break; }
            }
          }       
          cout<<g_table[0][0]<< "|"<< g_table[0][1]<< "|"<< g_table[0][2]<<endl;  
          
          cout<<"___________"<<endl;
          
          cout<<g_table[1][0]<< "|" <<g_table[1][1]<< "|"<< g_table[1][2]<<endl; 
          
          cout<<"___________"<<endl;
          
          cout<<g_table[2][0]<< "|" <<g_table[2][1]<< "|"<< g_table[2][2]<<endl<<endl;           

            }
            cout<<"placement= "<<placement<<endl;
            return placement;
}

It is the simple tic-tac-toe game. The thing is that when a winner is declared it continues with the game underwhich is suppose to stop completely. I have to use array's for the game.

What does this function do, what does it return, and what do the parameters represent? Have you checked whether it works?

bool winner(tttb my_Table, int ROW, bool isWin)

You immediately assign isWin to be false, it's not passed by value, so why pass the parameter?

Also, at the very end, you have this line:

cout<<"isWin= "<<isWin<<endl;

then the function ends and nothing (neither true nor false) is returned from your boolean function.

Actually, it should never get to that print line...it returns from inside all over the function. (Though I'm not sure that the returns in all of the test are required.)

Is the board required to be in an int tttb[3][3] or could we store it in an int tttb[9] and just display it in a 3 x 3 grid?

There is a lot of repeated code, creating a few more functions would clean this up a lot. The function move() is especially large.

Oh, and please:

When posting c++ code, please use c++ code tags
[code=c++] // Your code here

[/code]

It would appear that inside move, placement is actually getting set to true when the game is 'won' but nothing is looking at placement to stop the game. The only stop case is when the board is full.

Actually, it should never get to that print line...it returns from inside all over the function. (Though I'm not sure that the returns in all of the test are required.)

It definitely gets to that point. There are many ways to fall through the entire function without hitting a return. 3 apparently represents a blank, my_Table gets initialized to all 3's, so it goes into the initial if statement since if you don't make a move on one of those three spots, they all remain 3:

if (my_Table[0][0]== my_Table[1][0]  && my_Table[1][0] == my_Table[2][0])
      {   
          if (my_Table[0][0] == 1)
          {
               cout<<"X is the winner"<<endl;
               isWin = true;
               cout<<"isWin= "<<isWin<<endl;
               return isWin;                  
          }     
          else if (my_Table[0][0] == 0)
          {
               cout<<"O is the winner"<<endl; 
               isWin = true;
               return isWin;                  
          }     
      }

then it flunks both nested-if tests and passes all the way to the last line, without ever going through the "else" clause, which probably shouldn't exist since there is only a winner if one of the earlier NESTED if-statements is true. But that won't solve it either, because you could have X's in, say, the last row, and you'd never make it to the "else if" statement since you already went through the top if statement. I say change the "else if" statements to "if" statements so all the tests are done before declaring that there is no winner, and get rid of the "else" clause.

@VernonDozier
You're right (again), I missed the fact that they would all be 3's to start.

Not that it is an excuse, but my convention for TTT win checks is to check for matches against the last player to move so it wouldn't have seen the 'empty' locations.

@jesse_johnson2
Why do you maintain both g_table and mytable?

I know g_table is to display and mytable is to check for wins, but couldn't you just as easily check for wins against g_table?

Yeah g_table should work just as well. I just liked have one for display and one to check for winner but it should all work the same.

This question has already been answered. Start a new discussion instead.