?
Solved

code check

Posted on 2005-03-03
1
Medium Priority
?
241 Views
Last Modified: 2010-04-01

// 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
Comment
Question by:Yesenia1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 13453801
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question