GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Feb-2010, 14:28
Hothrous Hothrous is offline
New Member
 
Join Date: Feb 2010
Posts: 4
Hothrous is on a distinguished road
Question

expected primary-expression before '{' token


Hi I am having trouble with an assignment for my advanced C++ class. I am using dev-C++ to compile it and it is giving me a bunch of errors along the lines of "expected primary-expression before '{' token "

I have been searching for a week now. I even re-wrote the entire project trying to fix this.

Here is the code, it is a bit sloppy, partially because the instructions called for solving the problem with a 2d Array. This is not how I would have originally solved it so I had to do it in a different way:


Main file:
CPP / C++ / C Code:
#include <iostream>
// Allows for the creation of a game
#include "TicTacToe.h"

using namespace std;

// Main Program
int main()
{
   TicTacToe *game;
   bool playing = true;
   char playAgain = 'n';
   
   while(playing)
   {
                 game = new TicTacToe;
                 cout << " Play Again? y/n: ";
                 cin >> playAgain;
                 while(playAgain != 'y' && playAgain != 'n')
                 {
                      cout << " I didn't understand your response. /n Play Again? y/n: ";
                      cin >> playAgain;
                 }
                 if(playAgain == 'y')
                              playing = true;
                 else
                              playing = false;
                 delete game;
   }
   
   cout << " I hope you enjoyed the game. /n/n";
   system("Pause");
   return 0;
  
}

Class Header:
CPP / C++ / C Code:
#ifndef TICTACTOE_H
#define TICTACTOE_H
#include <iostream>


using namespace std;

// Class Definition
class TicTacToe
{
      private:
              int intBoard[3][3]; // 2D array of the board
              bool boolComputerPlayer; // Stores if the second player is computer
              bool boolWinner; // Stores status of game
              int intMove, intPlayer; // Current move and current player
      public:
              TicTacToe();
              void gameLoop();
              void setComputerPlayer();
              void setPlayer();
              void updateBoard();
              void displayBoard();
              void getMove();
              bool checkWinner();
              int getComputerMove();
              bool validateMove();
              

}; // End Class

#endif

Class .cpp file

CPP / C++ / C Code:
// Preprocessor Statements
#include <iostream>
using namespace std;

#include "TicTacToe.h"


// Constructor calls the gameLoop
TicTacToe::TicTacToe()
{
            intBoard = {{0,0,0}, {0,0,0}, {0,0,0}};
            setComputerPlayer();
            gameLoop();           
};

// Controls the game
void TicTacToe::gameLoop()
{
     bool winner;
     
            do
            {
                  displayBoard();
                  getMove();
                  updateBoard();
                  winner = checkWinner();
                  if(!winner)
                             setPlayer();
            }while(!winner)
            
};


// Determine if there needs to be a computer player
void TicTacToe::setComputerPlayer()
{
            
            cout << " One player or Two? /n 1: 1 player and a computer /n 2: 2 players /n/n";                   
            cin >> intPlayer;
            
            while (intPlayer < 1 || intPlayer > 2)
            {
                              cout << " One player or Two? /n 1: 1 player and a computer /n 2: 2 players /n/n";                   
                              cin >> intPlayer;
            }
            
            if (intPlayer == 1)
               boolComputerPlayer = true;
            else
               boolComputerPlayer = false;
            
            intPlayer = 1;
}     ;      

// Changes to current player
void TicTacToe::setPlayer()
{
            if(intPlayer == 1)
                         intPlayer = 2;
            else
                         intPlayer = 1;
};

// Updates the board with the current move
void TicTacToe::updateBoard()
{
     switch(intMove)
     {
     case 0:
          intBoard[0][0] = intPlayer;
          break;
     case 1:
          intBoard[0][1] = intPlayer;
          break;
     case 2:
          intBoard[0][2] = intPlayer;
          break;
     case 3:
          intBoard[1][0] = intPlayer;
          break;
     case 4:
          intBoard[1][1] = intPlayer;
          break;
     case 5:
          intBoard[1][2] = intPlayer;
          break;
     case 6:
          intBoard[2][0] = intPlayer;
          break;
     case 7:
          intBoard[2][1] = intPlayer;
          break;
     case 8:
          intBoard[2][2] = intPlayer;
          break;
     }
};
   
// Displays the current board
void TicTacToe::displayBoard()
{
     int count = 0;
     for(int row = 0; row < 3; row++)
     {
             for(int col = 0; col < 3; col++)
             {
                     switch(intBoard[row][col])
                     {
                     case 0:
                          cout << count;
                          break;
                     case 1:
                          cout << "X";
                          break;
                     case 2:
                          cout << "O";
                          break;
                     }
                     count++;
             }
             cout << endl;
     }
};

// Get current move
void TicTacToe::getMove()
{
     if(intPlayer == 1)
     { 
                  bool valid = false;
                  
                  cout << " Player 1, what is your move? ";
                  cin >> intMove;
                  valid = validateMove();
                  
                  while(!valid)
                  {
                        cout << " Invalid move, try again: ";
                        cin >> intMove;
                        valid = validateMove();                      
                       
                  }
     }
     else
     {
                  if(boolComputerPlayer)
                  {
                         intMove = getComputerMove();
                         cout << " Getting computer move... ";
                         for(int c = 0; c < 3000; c++)
                         { }
                  }
                  else
                  {      
                         bool valid = false;
                  
                         cout << " Player 2, what is your move? ";
                         cin >> intMove;
                         valid = validateMove();
                  
                         while(!valid)
                         {
                                      cout << " Invalid move, try again: ";
                                      cin >> intMove;
                                      valid = validateMove();                      
                         }
                  }
          
};

// Check to see if there is a winner
bool TicTacToe::checkWinner()
{
     if(intMove == intBoard[0][0])
     {
                if(intBoard[0][0] ==  intBoard[0][1] == intBoard[0][2])
                     return true;
                else if(intBoard[0][0] == intBoard[1][0] == intBoard[2][0])
                     return true;
                else if(intBoard[0][0] == intBoard[1][1] == intBoard[2][2])
                     return true;
                else
                     return false;
     }
     else if(intMove == intBoard[2][0])
     {
                if(intBoard[2][0] ==  intBoard[2][1] == intBoard[2][2])
                     return true;
                else if(intBoard[2][0] == intBoard[1][1] == intBoard[0][2])
                     return true;
                else
                     return false;
     }
     else if(intMove == intBoard[1][0])
     {
                if(intBoard[1][0] ==  intBoard[1][1] == intBoard[1][2])
                     return true;
                else
                     return false;
     }
     else if(intMove == intBoard[0][1])
     {
                if(intBoard[0][1] ==  intBoard[1][1] == intBoard[2][1])
                     return true;
                else
                     return false;
     }
     else if(intMove == intBoard[0][2])
     {
                if(intBoard[0][2] ==  intBoard[1][2] == intBoard[2][2])
                     return true;
                else
                     return false;
     }
     else
         return false;
};

// Calculates the computers move
int TicTacToe::getComputerMove()
{
    int currentPosition = 0;
    int currentLocationHold;
    
    // Determine if there is a location that will cause the computer to win
    for(int row = 0; row < 3; row++)
    {
            for(int col = 0; col < 3; col++)
            {
                    currentLocationHold = intBoard[row][col];
                    intBoard[row][col] = 2;
                    if(currentLocationHold == 0)
                    {
                         if(checkWinner())
                         {    
                              intBoard[row][col] = currentLocationHold;
                              return currentPosition; 
                         }
                    }     
                    intBoard[row][col] = currentLocationHold;
                    currentPosition++;
            }
    }
    
    currentPosition = 0;
    
    // Determine if there is a position that will allow the human to win
    for(int row = 0; row < 3; row++)
    {
            for(int col = 0; col < 3; col++)
            {
                    currentLocationHold = intBoard[row][col];
                    intBoard[row][col] = 1;
                    if(currentLocationHold == 0)
                    {
                         if(checkWinner())
                         {    
                              intBoard[row][col] = currentLocationHold;
                              return currentPosition; 
                         }
                    }     
                    intBoard[row][col] = currentLocationHold;
                    currentPosition++;
            }
    }
    
    // If the center is available choose that.
    if(intBoard[1][1] == 0)
           return 4;
    else // Else return first available position
    {     
           currentPosition = 0;
           for(int row = 0; row < 3; row++)
           {
                   for(int col = 0; col < 3; col++)
                   {
                    if(intBoard[row][col] == 0)
                         return currentPosition;
                         
                    currentPosition++;
                   }
           }
    
    return currentPosition;
         
       
};
   
// Validate the players move
bool TicTacToe::validateMove()
{
     if(intMove < 0 || intMove > 8)
                return false;
     else
     switch(intMove)
     {
     case 0:
          if(intBoard[0][0] != 0);
                            return false;
          break;
     case 1:
          if(intBoard[0][1] != 0);
                            return false;
          break;
     case 2:
          if(intBoard[0][2] != 0);
                            return false;
          break;
     case 3:
          if(intBoard[1][0] != 0);
                            return false;
          break;
     case 4:
          if(intBoard[1][1] != 0);
                            return false;
          break;
     case 5:
          if(intBoard[1][2] != 0);
                            return false;
          break;
     case 6:
          if(intBoard[2][0] != 0);
                            return false;
          break;
     case 7:
          if(intBoard[2][1] != 0);
                            return false;
          break;
     case 8:
          if(intBoard[2][2] != 0);
                            return false;
          break;
     default:
             return true;
     }
   
   return true;
};

It's probably something simple that I'm missing but it is driving me crazy that I can't find it. If somebody knows what is causing this I would greatly appreciate it if you would let me know.

Thanks for the help.


EDIT: The compiler is only showing errors in the Class .cpp file.

EDIT2: I have corrected a couple of syntax errors and updated the Class .cpp
  #2  
Old 07-Feb-2010, 16:16
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,280
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: expected primary-expression before '{' token


Semi-colons are not used after function definitions.

For example: (see the comment)
CPP / C++ / C Code:
TicTacToe::TicTacToe()
{
            intBoard = {{0,0,0}, {0,0,0}, {0,0,0}};
            setComputerPlayer();
            gameLoop();           
};  // <-- DON'T PLACE ; HERE

Also, you have a do-while loop that DOES need a semi-colon after the while. (inside function gameLoop)
CPP / C++ / C Code:
// ...
    do
    {
        displayBoard();
         getMove();
         updateBoard();
         winner = checkWinner();
         if(!winner)
         setPlayer();
    }while(!winner); // <-- NEED ; HERE
//...
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 07-Feb-2010, 16:42
Hothrous Hothrous is offline
New Member
 
Join Date: Feb 2010
Posts: 4
Hothrous is on a distinguished road

Re: expected primary-expression before '{' token


Thanks for that. I made those changes but that didn't correct the errors for me.
  #4  
Old 07-Feb-2010, 17:07
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,280
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: expected primary-expression before '{' token


What other errors do you have now?

That was just a start, from a quick look, but there could be more. If I get a chance in a moment, I'll try a compile of what you have, but post what current code you have, so that I [or others crossing this post to assist] have the updated code.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
Last edited by TurboPT : 07-Feb-2010 at 17:58.
  #5  
Old 07-Feb-2010, 17:13
Hothrous Hothrous is offline
New Member
 
Join Date: Feb 2010
Posts: 4
Hothrous is on a distinguished road

Re: expected primary-expression before '{' token


Same ones

In constructor `TicTacToe::TicTacToe()':
20 expected primary-expression before '{' token
20 expected `;' before '{' token
In member function `void TicTacToe::gameLoop()':
40 expected `;' before '}' token
In member function `void TicTacToe::getMove()':
181 expected primary-expression before "bool"
181 expected `;' before "bool"
229 expected primary-expression before "int"
229 expected `;' before "int"
347 `}' at end of input
Makefile.win [Build Error] [TicTacToe.o] Error 1

CPP / C++ / C Code:
TicTacToe::TicTacToe()
{
            intBoard = {{0,0,0}, {0,0,0}, {0,0,0}}; // <--- Line 20 for a point of reference
            setComputerPlayer();
            gameLoop();           
};
  #6  
Old 07-Feb-2010, 17:35
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,280
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: expected primary-expression before '{' token


Loops will be necessary to initialize the array there. The syntax you have is correct, IF it was done at the same time when declared, but since it can't be done within the class either, the looping will be needed.

There were some other things too:

1. In validateMove(), NONE of the if's should have a ; at their end.
2. Inside both getMove() and getComputerMove() there is an else block in each function missing a closing } brace.
3. Also, [this may, or may not be a problem for your compiler] mine complains about 'row' being declared twice [at the first two loops] inside getComputerMove().
[so, if you don't get an error about 'row', then ignore #3]
4. Within the output strings, the newlines should be: \n instead of: /n.

Post again if more help is needed. [don't forget to repost the latest code changes]
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #7  
Old 07-Feb-2010, 17:58
Hothrous Hothrous is offline
New Member
 
Join Date: Feb 2010
Posts: 4
Hothrous is on a distinguished road

Re: expected primary-expression before '{' token


Thanks. I was able to get it fixed. The ;s on the if statements were something I didn't notice. There were about 4 missing braces I kept miss counting. Now its just logic errors that I can figure out. Thanks for all of the help.
  #8  
Old 07-Feb-2010, 18:01
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,280
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: expected primary-expression before '{' token


No problem.

Post again, if needed.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Expected primary-expression before '{' token Saylor C++ Forum 3 10-Nov-2009 12:42
Expected primary-expression before '.' token dinh0wjr C Programming Language 2 30-Mar-2009 12:13
Error: expected unqualified-id before ‘{’ token ftmthy412 C++ Forum 5 12-Sep-2007 16:49
Converting PHP to C and I need a little help Allenport C Programming Language 4 14-Aug-2006 13:38
error: expected primary-expression before '.' token Honourable Mist C++ Forum 11 18-Feb-2006 12:15

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:04.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.