Tic-Tac-Toe Errors (Again)

I have made many corrections and cannot figure this out...

Page 1 of 1

8 Replies - 680 Views - Last Post: 13 May 2010 - 11:05 PM Rate Topic: -----

#1 Ctemp912  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-January 10

Tic-Tac-Toe Errors (Again)

Posted 13 May 2010 - 09:10 PM

This is a Tic Tac Toe game with a class I cannot figure out why I keep getting syntex errors and undefined errors. Any thoughts. Many thanks..


#include <iostream>
#include <string>
#include <cmath>
#include "Tic_Tac.h" // header file created
	 
using namespace std;
	 
class Tic_Tac // class
{

	  private:
		    int row;
			int column;
			int player;
			int board[3][3];
			char display_board[3][3];
			int Pick_Player();   
			int Pick_Row();    
			int Pick_Column();  
			int Check_Board();    
			
            void Choice_by_Player(int);  
		    void Choice_of_Row(int);   
		    void Choice_of_Column(int);  
		    void Tic_Tac_Board(); 
		    bool Check_Move(int,int);


      public:
		    Tic_Tac();
	};
    

	Tic_Tac()::Tic_Tac()
	{
		row = 0;
		column = 0;
		player = 1;
		int i = 0;
		int j = 0;
		for ( i = 0; i < 3; i++)
		{
			for ( j = 0; j < 3; j++)
			{
				board[i][j] = 0;
				display_board[i][j] = ' ';
			}
		}	
	}
	 
	 Tic_Tac()::Pick_Player()
	{
		return player;
	}
	 
	 Tic_Tac()::Pick_Row()
	{
		return row;
	}
	 
	 Tic_Tac()::Pick_Column()
	{
		return column;
	}
	 
	void Tic_Tac::Choice_by_Player(int a)
	{
		player = a;
	}
	 
	void Tic_Tac::Choice_of_Row(int B)/>
	{
		row = b;
	}
	 
	void Tic_Tac::Choice_of_Column(int c)
	{
		column = c;
	}
	 
	bool Tic_Tac::Check_Move(int row, int column)
	{
		row = 0;
		column = 0;
	 
		if ( row != 0  && row != 1 && row != 2 ) 
		{
			cout << " Invalid choice!!";
			cout << endl;
			return 0;
		}
		else if ( column != 0 && column != 1 && column != 2 )
		{
			cout << " Invalid choice!! " << endl;
			return 0;
		}
		else if ( board[row][column] == 1 || board[row][column] == 2)
		{
			cout << " Space already used. Try Again. " << endl;
			return 0;
		}
		else 
		{
			board[row][column] = player;
			return 1;
		}
	}   
	 
	int Tic_Tac::Check_Board()
	{
		int i = 0;
		int j = 0;
		int sum = 0;
		int test = 0;
		int count = 0;
	 
		for (i = 0; i < 3; i++)
		{
			sum = 0;
			for ( j = 0; j < 3; j++)
			{
				if (board[i][j] == 0)
				{
					count++;
				}	
				sum += (board[i][j] * board[i][j]);
			}
	 
		 	if ( sum == 3 || sum == 12)
		 	{
				test = sum;
				break;
			}
			sum = 0;
		}    	
	 
		for ( j = 0; j < 3; j++)
		{
			sum = 0;
			for ( i = 0; i < 3; i++)
			{
				sum += (board[i][j] * board[i][j]);
			}
	 
			if ( sum == 3 || sum == 12)
			{
				test = sum;
				break;
			}
	 
			sum = 0;
		}   
	 
		if ( test != 3 || test != 12)
		{
	 
        sum = (board[0][0] * board[0][0])+ (board[1][1] * board[1][1]) + (board[2][2] * board[2][2]);
	 
			if ( sum == 3 || sum == 12)
			{
				test  = sum;
	 
			}
		}  
	 
		if (test != 3 || test != 12)
		{
	 
				sum = (board[2][0] * board[2][0])+ (board[1][1] * board[1][1]) + (board[0][2] * board[0][2]);
	 
				if ( sum == 3 || sum == 12 )
				{
					test = sum;
				}
		}  
	 
		}
	 
		if ( test == 3)
		{
			test = 1;
		}
	 
		else if ( test == 12)
		{
			test = 2;
		}
	 
		else if ( count == 0)
		{
		 	test = 3;
		}
		else
		{
			test = 0;
		}
		return test;
	 
	} 
	 
	void Tic_Tac::Tic_Tac_Board()
	{
		for ( int row = 0; row < 3; row ++)
		{
	 
			for ( int column = 0; column < 3; column++)
			{
	 
				if ( board[row][column] == 0)
				{
					display_board[row][column] = ' ';
				}
				if ( board[row][column] == 1)
				{
					display_board[row][column] = 'X';
				}
				if ( board[row][column] == 2)
				{
					display_board[row][column] = 'O';
				}
			} 
		}  
	 
		cout << " 		 Tic-Tac-Toe!			" << endl;

		cout << "	Current Player: X	    Current Player:  O	"   << endl;
		cout << endl;
		cout << "		|		|		"  << endl;
		cout << " 		|		|		"  << endl;
		cout <<    display_board[0][0] << " 		|   "  << display_board[0][1]  <<  " 		| "  << display_board[0][2] << "   "  << endl;
		cout << "		|		|		" << endl;

		cout << "-----------------------------------------------" << endl;

		cout << "		|		|		" << endl;
		cout << "		|		|		" << endl;
		cout << 		  		   display_board[1][0] << " 		|   "  << display_board[1][1]  <<  " 		| "  << display_board[1][2] << "   "  << endl;
		cout << "		|		|		" << endl;

	        cout << "-----------------------------------------------" << endl;

		cout << " 		|		|		" << endl;
		cout <<			  	       	display_board[2][0] << " 		|   "  << display_board[2][1]  <<  " 		| "  << display_board[2][2] << "   "  << endl;
	 	cout << "		|		|		" << endl;
		cout << "		|		|		" << endl;
	 
	}  
	 
	 
	 
	 
	int main() //main loop
	{
	 
	 
		
		bool test;
		bool more = true;
		int row = 0;
		int column= 0;
		int player;
		int check = 0;
	 
	 
		Tic_Tac();
	 
		while ( more )
		{
			Tic_Tac_Board();
			player = Pick_Player();
	 
	 
			cout << " Current Player " << player;
			cout << endl;
			cout << " Enter Row Index ( 0,1,2):  " ;
			cout << endl;
			cin >> row;
			cout << " Enter Column Index (0,1,2): " << endl;
			cin >> column;
	 
			Choice_of_Row(row);
			Choice_of_Column(column);
	 
			test = Check_Move( Pick_Row(), Pick_Column());
	 
			if ( test == 1)
			{
	 
				check = Check_Board();
			}
	 
	 
			else
			{
				while ( test == 0 ) 
				{
	 
					cout << " Current Player " << Pick_Player() <<"  Invalid Choice" << endl;
	 
					cout << " Enter Row Index ( 0,1,2): " ;
	 
					cin >> row;
					cout << endl;
					cout << " Enter Column Index ( 0,1,2): " ;
	 
					cin >> column;
					cout << endl;
					Choice_of_Row(row);
					Choice_of_Column(column);
	 
					test = Check_Move(Pick_Row(),Pick_Column());
				} 
				check = Check_Board();
			}
	 
			if ( check == 1 || check == 2)
			{	
				break;
			}
	 
			else if ( check == 3 ) 
			{
				Tic_Tac_Board();
				cout << " The game is tied. " << endl;
	 
			}
	 
			if ( player == 1)
			{
				player = 2;
			}
			else
			{
				player = 1;
			}
			Choice_by_Player(player);
	 
		} 
	 
		Tic_Tac_Board();
	 
		cout << " Player " << check << " wins. " << endl;
	 
		return 0;
	}

 



Header file Tic_Tac.h


#ifndef _Tic_Tac_h
#define _Tic_Tac_h


#include <iostream>
#include <string>


using namespace std;

class Tic_Tac
{

private:
        int row;
		int column;
		int player;
		int board[3][3];
		char display_board[3][3];
	    int Pick_Player();   
		int Pick_Row();    
		int Pick_Column();  
		int Check_Board();
		
		void Choice_by_Player(int);  
		void Choice_of_Row(int);   
		void Choice_of_Column(int);  
		void Tic_Tac_Board(); 
		bool Check_Move(int,int);

public:
        
        Tic_Tac();

		
		
};
#endif




Is This A Good Question/Topic? 0
  • +

Replies To: Tic-Tac-Toe Errors (Again)

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Tic-Tac-Toe Errors (Again)

Posted 13 May 2010 - 09:14 PM

This post is a lot like your previous post where you throw out code and leave it to us to fix it. How about taking an active role in your code. It's your code after all.

I mean, you talk about errors, but don't even bother listing them here. But so what? What about errors? What did you try? This shouldn't be the first time you have seen an error. Take the first error. How have you tried to fix the first error?
Was This Post Helpful? 0
  • +
  • -

#3 Edisun  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 13-May 10

Re: Tic-Tac-Toe Errors (Again)

Posted 13 May 2010 - 09:22 PM

You don't need the header file if you're already declaring your class objects and members in the .cpp. Though I'm sure you've got more problems than that, like Oler1s said, we need more info on what kinds of errors you are getting before anyone would bother to stop and try to help.
Was This Post Helpful? 0
  • +
  • -

#4 Ctemp912  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-January 10

Re: Tic-Tac-Toe Errors (Again)

Posted 13 May 2010 - 10:33 PM

Geez harsh crowd..But I understand the critisism.. I tried to full around with the header file because of my undeclared errors I am getting of course thats not helping.I fliped the private and the public functions thinking I had them backwards. I then started going through my code one error at a time, First thing first "Class type redefinition" ??? Its throwing me off here.

errors:

Error 1 error C2011: 'Tic_Tac' : 'class' type redefinition
Error 2 error C2059: syntax error : ')'
Error 3 error C2143: syntax error : missing ';' before '{'
Error 5 error C2059: syntax error : ')'
Error 6 error C2039: 'Pick_Player' : is not a member of '`global namespace''
Error 7 error C2143: syntax error : missing ';' before '{'
Error 8 error C2447: '{' : missing function header (old-style formal list?)
Error 9 error C2059: syntax error : ')'
Error 10 error C2039: 'Pick_Row' : is not a member of '`global namespace''
Error 11 error C2143: syntax error : missing ';' before '{'
Error 12 error C2447: '{' : missing function header (old-style formal list?)
Error 13 error C2059: syntax error : ')'
Error 14 error C2039: 'Pick_Column' : is not a member of '`global namespace''
Error 15 error C2143: syntax error : missing ';' before '{'
Error 16 error C2447: '{' : missing function header (old-style formal list?)
Error 17 error C2027: use of undefined type 'Tic_Tac'
Error 18 error C2065: 'player' : undeclared identifier
Error 19 error C2027: use of undefined type 'Tic_Tac'
Error 20 error C2065: 'row' : undeclared identifier
Error 21 error C2027: use of undefined type 'Tic_Tac'
Error 22 error C2065: 'column' : undeclared identifier
Error 23 error C2027: use of undefined type 'Tic_Tac'
Error 24 error C2065: 'board' : undeclared identifier
Error 25 error C2027: use of undefined type 'Tic_Tac'
Error 26 error C2059: syntax error : 'if'
Error 27 error C2143: syntax error : missing ';' before '{'
Error 28 error C2447: '{' : missing function header (old-style formal list?)
Error 29 error C2059: syntax error : 'else'
Error 30 error C2143: syntax error : missing ';' before '{'
Error 31 error C2447: '{' : missing function header (old-style formal list?)
Error 32 error C2059: syntax error : 'else'
Error 33 error C2143: syntax error : missing ';' before '{'
Error 34 error C2447: '{' : missing function header (old-style formal list?)
Error 35 error C2059: syntax error : 'else'
Error 36 error C2143: syntax error : missing ';' before '{'
Error 43 error C2143: syntax error : missing ';' before '{'
Error 44 error C2447: '{' : missing function header (old-style formal list?)
Error 45 error C3861: 'Tic_Tac_Board': identifier not found
Error 46 error C3861: 'Pick_Player': identifier not found
Error 47 error C3861: 'Pick_Row': identifier not found
Error 48 error C3861: 'Pick_Column': identifier not found
Error 49 error C3861: 'Pick_Player': identifier not found
Error 50 error C3861: 'Pick_Row': identifier not found
Error 51 error C3861: 'Pick_Column': identifier not found
Error 52 error C3861: 'Tic_Tac_Board': identifier not found
Error 53 error C3861: 'Tic_Tac_Board': identifier not found
Was This Post Helpful? 0
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Tic-Tac-Toe Errors (Again)

Posted 13 May 2010 - 10:41 PM

Quote

'Tic_Tac' : 'class' type redefinition
What do you think this means? It's talking about Tic_Tac, and it mentions a redefinition. So...what's a definition? A redefinition, based on how English works, means you did it more than once.... If it's an error, it probably means you can only do it once....
Was This Post Helpful? 0
  • +
  • -

#6 muballitmitte  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 174
  • View blog
  • Posts: 470
  • Joined: 05-November 08

Re: Tic-Tac-Toe Errors (Again)

Posted 13 May 2010 - 10:50 PM

just so you understand. this cpp separation into header files and implementation files (.cpp,...) requires you to declare the class inside the .h file with the implementation written in the cpp file. Something like this

//header file A.h
class A
{
public:
 A();
 void method1();
 int method2();
};



//cpp file A.cpp
#include "A.h"
A:A()
{
 //constructor body
}
void A::method1()
{
 //implement here
}
int A::method2()
{
 //implement here
}


If you declare the class in both files it`s normal you get a redefinition error.

This post has been edited by muballitmitte: 13 May 2010 - 10:51 PM

Was This Post Helpful? 0
  • +
  • -

#7 Ctemp912  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-January 10

Re: Tic-Tac-Toe Errors (Again)

Posted 13 May 2010 - 11:02 PM

Ok now im starting to get it alil clearer now. Thanks Muba. Oler1 I got what redefiniton means before just didnt know how to correct it. I got a few less errors now still Have syntex errors and afew undefined errors I am going through it to see what I can find.
Was This Post Helpful? 0
  • +
  • -

#8 Ctemp912  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-January 10

Re: Tic-Tac-Toe Errors (Again)

Posted 13 May 2010 - 11:02 PM

Ok now im starting to get it alil clearer now. Thanks Muba. Oler1 I got what redefiniton means before just didnt know how to correct it. I got a few less errors now still Have syntex errors and afew undefined errors I am going through it to see what I can find.
Was This Post Helpful? 0
  • +
  • -

#9 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Tic-Tac-Toe Errors (Again)

Posted 13 May 2010 - 11:05 PM

The 'class type redefinition' error should be fairly obvious if you think about it. When you #include a file, it is as if the contents of that file were located where the #include statement is. So, when you compile your class .cpp file, first the compiler finds the class definition of Tic_Tac from the header, and then it finds the definition of the class that you wrote in the .cpp file. So, to the compiler that's a redefinition. Either eliminate the header or delete the class definition from the .cpp file.

For the next error ['Pick_Player' : is not a member of '`global namespace''], look in your main function where you are calling Pick_Player as if it were a global function:
                       player = Pick_Player();



Pick_Player is a member function of the Tic_Tac class, so you need a Tic_Tac object to access that function, right? Tic_Tac is a data type, so first you have to create a Tic_Tac object:
    // ...
    // create a Tic_Tac object
    Tic_Tac tt;
    // now you can use that to call member functions
    player = tt.Pick_Player();
    //....



A lot (maybe all) of the other errors you listed result from the first one (the redefinition). Recompile & then start working your way through the next batch of errors one at a time from the beginning of the list.

This post has been edited by r.stiltskin: 13 May 2010 - 11:07 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1