Thread: making tic tac toe, need some help

  1. #1
    Programmer Frantic-'s Avatar
    Join Date
    Dec 2004
    Posts
    114

    making tic tac toe, need some help

    Code:
    /*
    WE NEED A CLASS NAMED PLAYER WITH METHOD TAKESQUARE
    WE NEED A CLASS NAMED BOARD WITH METHOD CLEAR AND SET SQUARE
    WE NEED AN ARRAY NAMED BOARDVECTOR [3][3]
    */
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    //=========================================================
    
    class PlayingArea
    {
    
    public:
    
    	PlayingArea();
    	~PlayingArea();
    	int ClearBoard(); //this will clear the board and start a new game
    	int SetSquare(int SquareTaken, string Symbol); //this will place an X or and O in the appropriate space
    
    private:
    
    	string BoardVector[3][3]; //this is the tic tac toe board
    
    };
    
    class Player
    {
    
    public:
    	Player();
    	~Player();
    	int TakeSquare(); //this will return which square the user wishes to take.
    	void Symbol();
    	int Wins; 
    	int Losses;
    	int Ties;
    	int SquareTaken;
    private:
    	string PlayerSymbol;
    
    };
    
    
    //==========================================================
    
    int PlayingArea::ClearBoard()
    {
    	int z=1;
    
    	for (int x=1; x < 4; x++){
    
    		int y=1;
    		
    		BoardVector[x][y] = z;
    		z++;
    		y++;
    		BoardVector[x][y] = z;
    		z++;
    		y++;
    		BoardVector[x][y] = z;
    		z++;
    		y++;
    	}
    
    	return(0);
    
    }
    
    //===========================================================
    
    int PlayingArea::SetSquare(int SquareTaken, string Symbol)
    {
    
    	int z=1;
    
    	for (int x=1; x < 4; x++){
    		
    		int y=1;
    
    		if (z == SquareTaken){
    			BoardVector[x][y] = Symbol;
    		}
    		z++;
    		y++;
    
    		if (z == SquareTaken){
    			BoardVector[x][y] = Symbol;
    		}
    		z++;
    		y++;
    
    		if (z == SquareTaken){
    			BoardVector[x][y] = Symbol;
    		}
    		z++;
    		y++;
    	}
    	
    return(0);
    }
    
    //===========================================================
    
    PlayingArea()
    {
    	return NULL;
    }
    
    //===========================================================
    
    ~PlayingArea()
    {
    	return NULL;
    }
    
    //===========================================================
    
    Player()
    {
    	return NULL;
    }
    
    //===========================================================
    
    ~Player()
    {
    	return NULL;
    }
    
    //===========================================================
    
    int main()
    {
    
    	PlayingArea Board;
    	Player PlayerA;
    	Player PlayerB;
    
    return(0);
    }
    --------------------Configuration: TicTacToe - Win32 Debug--------------------
    Compiling...
    Source.cpp
    C:\Documents and Settings\Owner\Desktop\Comp Prog\C++\Sams C++\Lesson 10\TicTacToe\Source.cpp(118) : error C2588: '::~PlayingArea' : illegal global destructor
    C:\Documents and Settings\Owner\Desktop\Comp Prog\C++\Sams C++\Lesson 10\TicTacToe\Source.cpp(119) : error C2084: function 'int __cdecl PlayingArea(void)' already has a body
    C:\Documents and Settings\Owner\Desktop\Comp Prog\C++\Sams C++\Lesson 10\TicTacToe\Source.cpp(132) : error C2588: '::~Player' : illegal global destructor
    C:\Documents and Settings\Owner\Desktop\Comp Prog\C++\Sams C++\Lesson 10\TicTacToe\Source.cpp(133) : error C2084: function 'int __cdecl Player(void)' already has a body
    C:\Documents and Settings\Owner\Desktop\Comp Prog\C++\Sams C++\Lesson 10\TicTacToe\Source.cpp(142) : error C2146: syntax error : missing ';' before identifier 'Board'
    C:\Documents and Settings\Owner\Desktop\Comp Prog\C++\Sams C++\Lesson 10\TicTacToe\Source.cpp(142) : error C2065: 'Board' : undeclared identifier
    C:\Documents and Settings\Owner\Desktop\Comp Prog\C++\Sams C++\Lesson 10\TicTacToe\Source.cpp(143) : error C2146: syntax error : missing ';' before identifier 'PlayerA'
    C:\Documents and Settings\Owner\Desktop\Comp Prog\C++\Sams C++\Lesson 10\TicTacToe\Source.cpp(143) : error C2065: 'PlayerA' : undeclared identifier
    C:\Documents and Settings\Owner\Desktop\Comp Prog\C++\Sams C++\Lesson 10\TicTacToe\Source.cpp(144) : error C2146: syntax error : missing ';' before identifier 'PlayerB'
    C:\Documents and Settings\Owner\Desktop\Comp Prog\C++\Sams C++\Lesson 10\TicTacToe\Source.cpp(144) : error C2065: 'PlayerB' : undeclared identifier
    Error executing cl.exe.

    TicTacToe.exe - 10 error(s), 0 warning(s)

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Did you miss the part where you ask a question?

    [edit]By the way, your function definations could use some work. Take a look at this:
    Code:
    int PlayingArea::SetSquare(int SquareTaken, string Symbol)
    Compare that with this:
    Code:
    PlayingArea()
    You need to put the methods of your class into the scope of the class.
    Last edited by pianorain; 06-15-2005 at 08:01 AM.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Programmer Frantic-'s Avatar
    Join Date
    Dec 2004
    Posts
    114
    how do i correct those errors.

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I made a tic tac toe game the other day... 2 hours of coding.. 1 hour to debug/tweak. I'll post up the code of what I came up with after you've demonstrated that you have a working program. So far, the compiler is giving you good advice on what needs to be fixed.
    Last edited by The Brain; 06-15-2005 at 08:52 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    9
    Quote Originally Posted by Frantic-
    how do i correct those errors.
    all your compilation errors come from invalid declarations of constructors and destructors.

    So please replace

    Code:
    PlayingArea()
    {
    return NULL;
    }
    
    //==================================================  =========
    
    ~PlayingArea()
    {
    return NULL;
    }
    
    //==================================================  =========
    
    Player()
    {
    return NULL;
    }
    
    //==================================================  =========
    
    ~Player()
    {
    return NULL;
    }
    with:

    Code:
    PlayingArea::PlayingArea()
    {
    }
    
    //=========================
    
    PlayingArea::~PlayingArea()
    {
    }
    
    //=========================
    
    Player::Player()
    {
    }
    
    //=========================
    
    Player::~Player()
    {
    }
    (remember that constructors and destructors cannot return any value)

    Best regards, bilbo

  6. #6
    Programmer Frantic-'s Avatar
    Join Date
    Dec 2004
    Posts
    114
    ahh thanks

    ill have to remember both of those 2 things.

  7. #7
    Programmer Frantic-'s Avatar
    Join Date
    Dec 2004
    Posts
    114
    when i call this function

    Code:
    int PlayingArea::ClearBoard()
    {
    	int z=1;
    
    	for (int x=1; x < 4; x++){
    
    		int y=1;
    		
    		BoardVector[x][y] = z;
    		z++;
    		y++;
    		BoardVector[x][y] = z;
    		z++;
    		y++;
    		BoardVector[x][y] = z;
    		z++;
    		y++;
    	}
    
    	return(0);
    }
    i get a send error report screen, but when i click dont send, the program finished running just fine and waits for you to press enter. Does this have anything to do with this function, or is it something with my computer?

  8. #8
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Oh, it's the function. You're overstepping your array bounds. Look at your declaration.
    Code:
    	string BoardVector[3][3]; //this is the tic tac toe board
    That declares a two-dimensional array, with each dimension having a range of 0 - 2. Now take a look at your function:
    Code:
    for (int x=1; x < 4; x++) //start x at 1, loop until x > 4
    //that's bad, since x = 3 is invalid
    { 
    	int y=1; //start y at 1
    		
    	BoardVector[x][y] = z;
    	z++;
    	y++; //y is now 2
    	BoardVector[x][y] = z;
    	z++;
    	y++; //y is now 3
    	BoardVector[x][y] = z; //accessing [x][3]...outside of array
    }
    You should start accessing your array at 0 instead of 1 and watch your array bounds.
    Last edited by pianorain; 06-15-2005 at 09:40 AM.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  9. #9
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Here is my version of tic tac toe


    Code:
    #include<iostream>
    #include<cstdlib>
    #include<iomanip>
    #include<cctype>
    using namespace std;
    
    
    class TicTac
    {
    
    public:
    
    	TicTac();
    	void load_grid();
    	void display_grid();
    	void prompt_player();
    	void update_grid();	
    	void set_turn();
    	bool check_winner();
    	void reset();
    
    private:
    
    	char grid[5][6];
    	char square;
    	int  turn;	
    	int	 turn_counter;
    	bool tie;
    };
    
    
    char again();
    
    int main()
    {
    	TicTac Toe;
    
    	Toe.load_grid();
    
    	do{
    		Toe.reset();		
    
    		Toe.display_grid();
    
    		do{	
    			Toe.prompt_player();			
    	
    			Toe.update_grid();	
    			
    			Toe.set_turn();
    
    			Toe.display_grid();			
    
    		}while(!Toe.check_winner());	 
    
    	}while(again()!='N');
    
    	return 0;
    }
    
    
    
    //FUNCTION DEFINITIONS
    
    TicTac::TicTac()
    {
    	for(int row=0; row<5; row++)
    		for(int column=0; column<5; column++)
    
    			grid[row][column]='0';
    
    	turn	= 1;
    	tie     = 0;
    }
    
    
    void TicTac::load_grid()
    {
    
    	for(int row=0;row<5;row+=2)
    	
    		grid[row][1]=grid[row][3]=(char)179;
    
    	for(row=1;row<4;row+=2)
    
    		grid[row][0]=grid[row][1]=grid[row][2]=grid[row][3]=grid[row][4]=(char)196;
    
    	for(row=0;row<6;row++)
    
    		grid[row][5]='\0';
    }
    
    	
    void TicTac::display_grid()
    {
    	int spacer;
    
    	if(turn==1)
    		spacer = 20;	
    	else 
    		spacer = 39;
    
    
    	system("cls");
    
    	if(tie)
    
    		cout << "\a\n\n\t   *** Tie!!  No Winners!!  ***\n";
    
    	else if(check_winner())
    
    		cout << "\a\n\n\t   *** Player " << turn << " weeens! ***\n";
    
    	else 
    	
    		cout << "\n\n\tPlayer 1 = X       Player 2 = O\n" << setw(spacer) << "------------";	
    		
    
    	cout << endl << endl << endl << endl;
    
    	for(int row=0;row<5;row++)
    	
    		cout << setw(25) << grid[row] << endl;
    }
    
    
    void TicTac::prompt_player()
    {
    	cout << endl << endl << endl; 
    
    	cout << "\tPlayer " << turn << ", Enter a Square: ";	
    	cin  >> square;
    	cin.ignore(1000,'\n');
    
    	turn_counter++;
    }
    
    
    void TicTac::update_grid()
    {
    	bool found = false;
    
    	for(int row=0;!found && row<5; row+=2)
    		for(int column=0;!found && column<5;column+=2)
    		{
    			if(grid[row][column]==square)
    			{
    				if(turn==1)				
    					grid[row][column]='X';
    
    				else 
    					grid[row][column]='O';
    
    				found=true;
    			}
    		}
    	
    }
    
    
    void TicTac::set_turn()
    {
    	if(!check_winner())
    	{
    		if(turn==1)
    		   turn= 2;	
    		else
    		   turn= 1;
    	}
    }
    
    
    bool TicTac::check_winner()
    {
    
    
    	//Row Check
    	for(int row=0,column=0; row<5; row+=2)
    	{
    		if(grid[row][column]==grid[row][column+2]&&grid[row][column+2]==grid[row][column+4])
    					
    			return true;		
    	}
    
    	//Column Check
    	for(row=0,column=0; column<5; column+=2)
    	{
    		if(grid[row][column]==grid[row+2][column]&&grid[row+2][column]==grid[row+4][column])
    					
    			return true;		
    	}
    
    
    	//Diag Check
    	if(  (grid[0][0]==grid[2][2]&&grid[2][2]==grid[4][4]) ||
    		 (grid[0][4]==grid[2][2]&&grid[2][2]==grid[4][0])   )
    				 
    			return true;
    	
    
    	//Tie Check
    	if(turn_counter==9)
    	{			
    		tie	 = true;
    		return true;
    	}
    
    	return false;
    }
    
    
    char again()
    {
    	char choice;
    	cout << "\n\n\n\tWould ye' like to try again? (Y/N): ";
    	cin  >> choice;
    	cout << endl << endl;
    
    	return toupper(choice);
    }
    
    
    void TicTac::reset()
    {
    	int square_number = '1';
    
    	for(int row=0;row<5;row+=2)
    		for(int column=0;column<5;column+=2)
    
    			grid[row][column]=square_number++;
    
    	turn   = 1;	
    	tie    = false;
    	turn_counter = 0;
    }
    Last edited by The Brain; 06-16-2005 at 09:08 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with my simple Tic tac toe prog
    By maybnxtseasn in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 06:25 PM
  2. Tic Tac Toe Neural Network
    By glo in forum General AI Programming
    Replies: 4
    Last Post: 06-15-2008, 03:39 PM
  3. Making Tic Tac Toe Smarter
    By abh!shek in forum C Programming
    Replies: 15
    Last Post: 06-05-2008, 10:43 AM
  4. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 08:33 AM
  5. Replies: 22
    Last Post: 11-08-2001, 11:01 PM