Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

code check


// I'm having problems compiling this program I addedd more things but it still doesn't compile can you please help me.

#include <iostream>

using std::cout;
using std::endl;
using std::cin;


   void initGame(char[][3], int[]);
   bool validMove(char[][3], int, int);
   int gameOver(char[][3]);
   void display(char[][3]);
   void moveX(char[][3], int);
   void moveO(char[][3], int);
   bool check3(char[][3], char);
   int col;
   int row;
   int j;
   int i;
   int ttt;
   int player [2] = {1,2};
   int status = 0;
   int input;
   int won = 1;
   int lost = 0;
   char c[3][3];
   int main()
    {
   char ttt[3][3] ={ {' ',' ',' '}, {' ',' ',' '}, {' ',' ',' '} };
   int moves[9][2]= { {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0} };
   int player[2] = {1,2};
             
   int status = 0;      
   
   initGame(ttt, player);

   while (true) {
             
    status = gameOver(ttt);
    if (status) break;
    else
          {
            moveX(ttt, player[0]);
        display(ttt);
                                                                                             }
}
    status = gameOver(ttt);
while ( false ) {
            if (status) break;

            else
      {


   moveO(ttt, player[1]);
   display(ttt);


   
   }

   }

  if (status==1) cout << "The X player wins!" << endl;

  else

     if (status==2) cout << "The O player wins!" << endl;

        else cout << "This game is a draw!" << endl;

  display(ttt);

 return 0;

     }

     void initGame(char[][3], int[])
     {

     cout << " Enter Coordinate:  " << endl;
     cin >> row >> col;
}

     bool validMove(char[][3], int, int)
    {
      if (status == 1 )
      cout << " Enter Coordinate:  " << endl;
      
      else
      
      if (status == 2 )
      cout << "Invalid move:  "  << endl;
    return false;
    }


       void display(char[][3])
      {
      
      cout << col << row;               
}
       void moveX(char ttt[][3], int player)      
       {
       while (true)
       {
       int input;
       cout << "player " << player << ", Please Enter Next Move [1 ... 9 ] ==> ";
        cin >> input;
        if (input < 1 || input > 9)
        continue;
        int i = input-1;
        char c = ttt[i/3][i%3];

        if (c != ' ')
        {
          cout << "Invalid Move" << endl;
          continue;
}
        ttt[i/3][i%3] = 'X';
       }
       }
     

       void moveO(char ttt[][3], int player)
       {
       while (true)
       {
       int input;
       
       cout << "player " << player << ", Please Enter Next Move [1 ... 9 ] ==> ";
       cin >> input;
       if (input < 1 || input > 9)
       
       continue;
       int i = input-1;
       char c = ttt[i/3][i%3];
       
       if (c != ' ')
       {
       cout << "Invalid Move" << endl;
       continue;
       ttt[i/3][i%3] = 'X';
       }
     
       }
}

 int gameOver(char ttt[][3])

 {
   if ( check3(ttt, 'x'))
   return 1;

   else
   if ( check3(ttt, 'o'))
   return 2;

   
   

        bool check3(char ttt[][3], char x)
      
      
      {
        for (int j = 0; j < 3; j++)
       {
        for ( int i = 0; i < 3; i++ )
      {
       if ( ttt[j][i] == x )
         if ( i == 2 )
      
       return won;
        
        else
        break;
      }
      }
       for ( int a = 0; a < 3; a++ )
       {
         for ( int b = 0; b < 3; b++ )

         if (ttt [b][a] == x )
         if ( b == 2 )
        
       return won;

      else
      break;      
    }
      }

        for ( int d = 0; d < 3; d++ )
      if ( ttt [d][d] == x )
      if ( d == 2 )

      for ( int c = 2; c >= 0; c++ )
      {
        if (ttt [2-c][c] == x )
        if ( c == 0 )
        return won;
      }
 

      return lost;

      
}
0
Yesenia1
Asked:
Yesenia1
1 Solution
 
jkrCommented:
You forgot the closing bracket at the end of 'gameOver()', and 'check3()' was missing a couple of brackets regarding the 'for' loops:

bool check3(char ttt[][3], char x)
{
      for (int j = 0; j < 3; j++)
      {
            for ( int i = 0; i < 3; i++ )
            {
                  if ( ttt[j][i] == x )
                  if ( i == 2 )

                  return won;

                  else
                  break;
            }
      }
      for ( int a = 0; a < 3; a++ )
      {
            for ( int b = 0; b < 3; b++ )
            {
            if (ttt [b][a] == x )
            if ( b == 2 )

            return won;

            else
            break;    
            }
      }

      for ( int d = 0; d < 3; d++ )
            if ( ttt [d][d] == x )
            if ( d == 2 )

      for ( int c = 2; c >= 0; c++ )
      {
            if (ttt [2-c][c] == x )
            if ( c == 0 )
            return won;
      }

return lost;
}

The following compiles:

#include <iostream>

using std::cout;
using std::endl;
using std::cin;


   void initGame(char[][3], int[]);
   bool validMove(char[][3], int, int);
  int gameOver(char[][3]);
   void display(char[][3]);
  void moveX(char[][3], int);
  void moveO(char[][3], int);
  bool check3(char[][3], char);
   int col;
  int row;
  int j;
  int i;
  int ttt;
  int player [2] = {1,2};
  int status = 0;
  int input;
  int won = 1;
  int lost = 0;
  char c[3][3];
  int main()
   {
  char ttt[3][3] ={ {' ',' ',' '}, {' ',' ',' '}, {' ',' ',' '} };
  int moves[9][2]= { {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0} };
  int player[2] = {1,2};
           
   int status = 0;      
 
   initGame(ttt, player);

  while (true) {
           
    status = gameOver(ttt);
   if (status) break;
   else
         {
           moveX(ttt, player[0]);
        display(ttt);
                                                                               }
}
   status = gameOver(ttt);
while ( false ) {
           if (status) break;

           else
    {


  moveO(ttt, player[1]);
   display(ttt);


 
   }

  }

 if (status==1) cout << "The X player wins!" << endl;

 else

     if (status==2) cout << "The O player wins!" << endl;

       else cout << "This game is a draw!" << endl;

 display(ttt);

return 0;

    }

    void initGame(char[][3], int[])
    {

    cout << " Enter Coordinate:  " << endl;
    cin >> row >> col;
}

    bool validMove(char[][3], int, int)
   {
    if (status == 1 )
     cout << " Enter Coordinate:  " << endl;
   
     else
   
     if (status == 2 )
     cout << "Invalid move:  "  << endl;
   return false;
   }


      void display(char[][3])
    {
   
     cout << col << row;              
}
      void moveX(char ttt[][3], int player)    
       {
      while (true)
      {
     int input;
     cout << "player " << player << ", Please Enter Next Move [1 ... 9 ] ==> ";
      cin >> input;
      if (input < 1 || input > 9)
      continue;
      int i = input-1;
      char c = ttt[i/3][i%3];

      if (c != ' ')
      {
        cout << "Invalid Move" << endl;
        continue;
}
      ttt[i/3][i%3] = 'X';
      }
      }
     

      void moveO(char ttt[][3], int player)
      {
      while (true)
      {
     int input;
     
       cout << "player " << player << ", Please Enter Next Move [1 ... 9 ] ==> ";
      cin >> input;
      if (input < 1 || input > 9)
     
       continue;
      int i = input-1;
      char c = ttt[i/3][i%3];
     
       if (c != ' ')
      {
     cout << "Invalid Move" << endl;
     continue;
     ttt[i/3][i%3] = 'X';
      }
     
      }
}

int gameOver(char ttt[][3])

{
  if ( check3(ttt, 'x'))
  return 1;

  else
  if ( check3(ttt, 'o'))
  return 2;
}
 
   
bool check3(char ttt[][3], char x)
{
      for (int j = 0; j < 3; j++)
      {
            for ( int i = 0; i < 3; i++ )
            {
                  if ( ttt[j][i] == x )
                  if ( i == 2 )

                  return won;

                  else
                  break;
            }
      }
      for ( int a = 0; a < 3; a++ )
      {
            for ( int b = 0; b < 3; b++ )
            {
            if (ttt [b][a] == x )
            if ( b == 2 )

            return won;

            else
            break;    
            }
      }

      for ( int d = 0; d < 3; d++ )
            if ( ttt [d][d] == x )
            if ( d == 2 )

      for ( int c = 2; c >= 0; c++ )
      {
            if (ttt [2-c][c] == x )
            if ( c == 0 )
            return won;
      }

return lost;
}
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now