pretty much where i have many of my comments are where i need to call the functions from the player and cell class in the tictactoe class? anyone have any ideas on how to do this?

#include <iostream>
#include <cstdlib>
using namespace std;

class TicTacToe
{
public:
	TicTacToe()//contructor initializes the board
	{
		board[0][0] = '1';
		board[0][1] = '2';
		board[0][2] = '3';
		board[1][0] = '4';
		board[1][1] = '5';
		board[1][2] = '6';
		board[2][0] = '7';
		board[2][1] = '8';
		board[2][2] = '9';
	}
	
	void playTicTacToe()//function to play the game
	{
		srand(time(0));
		int playerTurn = 1 + rand() % 2;//randomly choose who goes first and then pass it to the player class? yes/ no???
		bool gameEnded = true;
		TicTacToe myTicTacToe;
		
		// do...while continue the game play until there is a winner or draw
		do{
			myTicTacToe.Player(displayTurn());// here i want to call the display turn from the player class

			myTicTacToe.displayBoard();// call displaybooard from ttt class
			// call setinfo() here from cell class
			//cal trackcell() here from cell class		
			myTicTacToe.displayBoard( playerLetter );// displayBoard from ttt class
			gameEnded = myTicTacToe.checkBoard();// Checks game status

			if (gameEnded)
			{
				myTicTacToe.displayBoard();// problem need to change something in the displayboard function

			} else
			{ 
				//here i want to call the display turn from the player class
			}
		}while (!gameEnded);
		

	}	
	bool checkBoard()//function checks the board for a winner or tells if it is a draw
	{
		bool gameEnded = false;
		//Check for winner
		if ( board[0][0] != '1')//Checks top row and first column
			{
				if ( board[0][0] == board[0][1] && board[0][0] == board[0][2] )
				{
					gameEnded = true;
					cout << "The winner is Player" << board[0][0] << "!" << endl;
				}
				if ( board[0][0] == board[1][0] && board[0][0] == board[2][0] )
				{
					gameEnded = true;
					cout << "The winner is Player" << board[0][0] << "!" << endl;
				}	
			}
		if ( board[1][1] != '5')//Checks diagonally, middle row, and middle column
			{
				if ( board[1][1] == board[0][0] && board[1][1] == board[2][2] )
				{
					gameEnded = true;
					cout << "The winner is Player" << board[1][1] << "!" << endl;
				}
				if ( board[1][1] == board[0][1] && board[1][1] == board[2][1] )
				{
					gameEnded = true;
					cout << "The winner is Player" << board[1][1] << "!" << endl;
				}	
				if ( board[1][1] == board[1][0] && board[1][1] == board[1][2] )
				{
					gameEnded = true;
					cout << "The winner is Player" << board[1][1] << "!" << endl;
				}
				if ( board[1][1] == board[0][2] && board[1][1] == board[2][0] )
				{
					gameEnded = true;
					cout << "The winner is Player" << board[1][1] << "!" << endl;
				}	
			}

		if ( board[2][2] != '9')//Checks bottom row and last column
			{
				if ( board[2][2] == board[0][2] && board[2][2] == board[1][2] )
				{
					gameEnded = true;
					cout << "The winner is Player" << board[2][2] << "!" << endl;
				}
				if ( board[2][2] == board[2][0] && board[2][2] == board[2][1] )
				{
					gameEnded = true;
					cout << "The winner is Player" << board[2][2] << "!" << endl;
				}	
			}

		//Checks to see if there is a draw
		if ( board[0][0] != '1' && board[0][1] != '2' && board[0][2] != '3' && 		     
		     board[1][0] != '4' && board[1][1] != '5' && board[1][2] != '6' &&
		     board[2][0] != '7' && board[2][1] != '8' && board[2][2] != '9' &&		     
		     !gameEnded )
		{
			gameEnded = true;
			cout << "It is a draw!" << endl;
		}

		return gameEnded;
	}

	void displayBoard(char pLetter) // fucntion displays the board**********feel like i need to change something here?
	{
		cout << board[0][0] << "|" << board[0][1] << "|" << board[0][2] << endl;
		cout << "-+-+-" << endl;
		cout << board[1][0] << "|" << board[1][1] << "|" << board[1][2] << endl;
		cout << "-+-+-" << endl;
		cout << board[2][0] << "|" << board[2][1] << "|" << board[2][2] << endl;

	}
private:
	Cell board[3][3];//NEW board as a data member and type Cell
	Player players[2];// NEW players declared as type Player
};

class Cell
{
public:
void setCellInfo()
{
	cout << "Please choose where you would like to enter your letter:" << endl;
	cin >> numOfCell;
	
}
int getCellInfo()
{
	return numOfCell;
}
void trackCell(char pName)
{
	numOfCell = getCellInfo();
	if ( numOfCell == '1')
		numOfCell = pName;
	else if ( numOfCell == '2')
		numOfCell = pName;
	else if ( numOfCell == '3')
		numOfCell = pName;
	else if ( numOfCell == '4')
		numOfCell = pName;
	else if ( numOfCell == '5')
		numOfCell = pName;
	else if ( numOfCell == '6')
		numOfCell = pName;
	else if ( numOfCell == '7')
		numOfCell = pName;
	else if ( numOfCell == '8')
		numOfCell = pName;
	else if ( numOfCell == '9')
		numOfCell = pName;
	else 
		cout << "Invalid Move. Try Again.";
}

private:
	int numOfCell;
};

class Player
{
public:
void setLetter()// change which letter will be displayed
{
	if (playerTurn == 1) 
		{
			pName = 'X';
			playerTurn++;
		} else 
		{
			pName = 'O';
			playerTurn--;
		}
}

char getLetter()
{
	return pName;
}

void displayTurn()
{
	setLetter();
	getLetter();
	cout << "It is player " << pName << "'s turn. ";
}
private:
	int playerTurn;
	char pName;
};

int main()//main function
{
	char gameboard;
	TicTacToe myTicTacToe;
	
	bool play = true;
	char d;// players decision whether to continue or not

	// Continue to play the game until the user does not enter y
	while ( play ) 
	{
		cout << " Would you like to play Tic Tac Toe?(y/n) ";
		cin >> d;

		if ( d == 'Y' || d == 'y' ) 
		{ 
			cout << "Let the games begin!" << endl;
			cout << "Player X will go first and Player O will go second." << endl;
			myTicTacToe.playTicTacToe();//function that begins game
		} else 
		{
			cout << endl << "See you next time!" << endl;
			play = false;
		}
	}
	
	return 0;
}// end main

Create a new variable of type Player class, make sure the function for solving your problems are PUBLIC methods, and then do var.MethodName(params);

So in Player class, if you had a function like this:

public int PlayerTurn()
{
return playerTurn;
}

in TicTacToe, you would do this:

Player myPlayer = new Player();
myPlayer.PlayerTurn();

Create a new variable of type Player class, make sure the function for solving your problems are PUBLIC methods, and then do var.MethodName(params);

So in Player class, if you had a function like this:

public int PlayerTurn()
{
return playerTurn;
}

in TicTacToe, you would do this:

Player myPlayer = new Player();
myPlayer.PlayerTurn();

cool. just some questions. is there any way to do this by referencing and second do i need to state new before Player():

Out of curiosity, why do you create another tic-tac-toe object within the tic-tac-toe class?

To call the methods of the other class just instantiate it:

MyOtherClass myinstance;
myinstance.methodNeeded();

(since you don't need pointers in this case you can put that on hold for a while)


in TicTacToe, you would do this:

Player myPlayer = new Player();
myPlayer.PlayerTurn();

If you are going to do it that way it would be Player * myPlayer = new Player(); myPlayer->PlayerTurn();

Out of curiosity, why do you create another tic-tac-toe object within the tic-tac-toe class?

To call the methods of the other class just instantiate it:

MyOtherClass myinstance;
myinstance.methodNeeded();

(since you don't need pointers in this case you can put that on hold for a while)

thats it? really? dam so simple lol. that would work with no compiler errors?

If you are going to do it that way it would be Player * myPlayer = new Player(); myPlayer->PlayerTurn();

so in that sense thats how you use the pointer with class to reference to the functions?

that would work with no compiler errors?

Only one way to find out. Yes, it should. It's the same thing you did with TicTacToe within its own class (which you should still look into btw).

so in that sense thats how you use the pointer with class to reference to the functions?

With a pointer to a class, yes, that's how you would access its methods.

Only one way to find out. Yes, it should. It's the same thing you did with TicTacToe within its own class (which you should still look into btw).


With a pointer to a class, yes, that's how you would access its methods.

ok cool. im gonna run it through the compiler right now

Only one way to find out. Yes, it should. It's the same thing you did with TicTacToe within its own class (which you should still look into btw).


With a pointer to a class, yes, that's how you would access its methods.

im gettin some errors maybe i did somethin wrong...
i put the errors i get at the end

#include <iostream>
#include <cstdlib>
using namespace std;

class TicTacToe
{
public:
	TicTacToe()//contructor initializes the board
	{
		board[0][0] = '1';
		board[0][1] = '2';
		board[0][2] = '3';
		board[1][0] = '4';
		board[1][1] = '5';
		board[1][2] = '6';
		board[2][0] = '7';
		board[2][1] = '8'; 
		board[2][2] = '9';
	}
	
	void playTicTacToe()//function to play the game
	{
		srand(time(0));
		int playerTurn = 1 + rand() % 2;//randomly choose who goes first and then pass it to the player class? yes/ no???
		bool gameEnded = true;
		TicTacToe myTicTacToe;
		Player players[2];

		
		// do...while continue the game play until there is a winner or draw
		do{
			
			Player myPlayer;
			Cell myCell;
			myPlayer.displayTurn();
			
			//myTicTacToe.Player(displayTurn());// here i want to call the display turn from the player class

			myTicTacToe.displayBoard();// call displaybooard from ttt class
			myCell.setInfo();// call setinfo() here from cell class
			myCell.trackCell(myPlayer.getLetter());//cal trackcell() here from cell class		
			myTicTacToe.displayBoard();// displayBoard from ttt class
			gameEnded = myTicTacToe.checkBoard();// Checks game status

			if (gameEnded)
			{
				myTicTacToe.displayBoard();// problem need to change something in the displayboard function

			} else
			{ 
				//here i want to call the display turn from the player class
			}
		}while (!gameEnded);
		

	}	
	bool checkBoard()//function checks the board for a winner or tells if it is a draw
	{
		bool gameEnded = false;
		//Check for winner
		if ( board[0][0] != '1')//Checks top row and first column
			{
				if ( board[0][0] == board[0][1] && board[0][0] == board[0][2] )
				{
					gameEnded = true;
					cout << "The winner is Player" << board[0][0] << "!" << endl;
				}
				if ( board[0][0] == board[1][0] && board[0][0] == board[2][0] )
				{
					gameEnded = true;
					cout << "The winner is Player" << board[0][0] << "!" << endl;
				}	
			}
		if ( board[1][1] != '5')//Checks diagonally, middle row, and middle column
			{
				if ( board[1][1] == board[0][0] && board[1][1] == board[2][2] )
				{
					gameEnded = true;
					cout << "The winner is Player" << board[1][1] << "!" << endl;
				}
				if ( board[1][1] == board[0][1] && board[1][1] == board[2][1] )
				{
					gameEnded = true;
					cout << "The winner is Player" << board[1][1] << "!" << endl;
				}	
				if ( board[1][1] == board[1][0] && board[1][1] == board[1][2] )
				{
					gameEnded = true;
					cout << "The winner is Player" << board[1][1] << "!" << endl;
				}
				if ( board[1][1] == board[0][2] && board[1][1] == board[2][0] )
				{
					gameEnded = true;
					cout << "The winner is Player" << board[1][1] << "!" << endl;
				}	
			}

		if ( board[2][2] != '9')//Checks bottom row and last column
			{
				if ( board[2][2] == board[0][2] && board[2][2] == board[1][2] )
				{
					gameEnded = true;
					cout << "The winner is Player" << board[2][2] << "!" << endl;
				}
				if ( board[2][2] == board[2][0] && board[2][2] == board[2][1] )
				{
					gameEnded = true;
					cout << "The winner is Player" << board[2][2] << "!" << endl;
				}	
			}

		//Checks to see if there is a draw
		if ( board[0][0] != '1' && board[0][1] != '2' && board[0][2] != '3' && 		     
		     board[1][0] != '4' && board[1][1] != '5' && board[1][2] != '6' &&
		     board[2][0] != '7' && board[2][1] != '8' && board[2][2] != '9' &&		     
		     !gameEnded )
		{
			gameEnded = true;
			cout << "It is a draw!" << endl;
		}

		return gameEnded;
	}

	void displayBoard() // fucntion displays the board**********feel like i need to change something here?
	{
		cout << board[0][0] << "|" << board[0][1] << "|" << board[0][2] << endl;
		cout << "-+-+-" << endl;
		cout << board[1][0] << "|" << board[1][1] << "|" << board[1][2] << endl;
		cout << "-+-+-" << endl;
		cout << board[2][0] << "|" << board[2][1] << "|" << board[2][2] << endl;

	}
private:
	Cell board[3][3];//NEW board as a data member and type Cell
	// NEW players declared as type Player
};

class Cell
{
public:
	Cell( int initCell)
	{
		setCellInfo( initCell);
	}
void setCellInfo(int initCell)
{
	numOfCell = initCell;
	cout << "Please choose where you would like to enter your letter:" << endl;
	cin >> numOfCell;
	
}
int getCellInfo()
{
	return numOfCell;
}
void trackCell(char pName)
{
	numOfCell = getCellInfo();
	if ( numOfCell == '1')
		numOfCell = pName;
	else if ( numOfCell == '2')
		numOfCell = pName;
	else if ( numOfCell == '3')
		numOfCell = pName;
	else if ( numOfCell == '4')
		numOfCell = pName;
	else if ( numOfCell == '5')
		numOfCell = pName;
	else if ( numOfCell == '6')
		numOfCell = pName;
	else if ( numOfCell == '7')
		numOfCell = pName;
	else if ( numOfCell == '8')
		numOfCell = pName;
	else if ( numOfCell == '9')
		numOfCell = pName;
	else 
		cout << "Invalid Move. Try Again.";
}

private:
	int numOfCell;
};

class Player
{
	Player(char initPname)
	{
		setLetter(initPname);
	}
public:
void setLetter(char initPname)// change which letter will be displayed
{
	pName = initPname;
	if (playerTurn == 1) 
		{
			pName = 'X';
			playerTurn++;
		} else 
		{
			pName = 'O';
			playerTurn--;
		}
}

char getLetter()
{
	return pName;
}

void displayTurn()
{
	setLetter(initPname);
	getLetter();
	cout << "It is player " << pName << "'s turn. ";
}
private:
	int playerTurn;
	char pName;
};

int main()//main function
{
	char gameboard;
	TicTacToe myTicTacToe;
	
	bool play = true;
	char d;// players decision whether to continue or not

	// Continue to play the game until the user does not enter y
	while ( play ) 
	{
		cout << " Would you like to play Tic Tac Toe?(y/n) ";
		cin >> d;

		if ( d == 'Y' || d == 'y' ) 
		{ 
			cout << "Let the games begin!" << endl;
			cout << "Player X will go first and Player O will go second." << endl;
			myTicTacToe.playTicTacToe();//function that begins game
		} else 
		{
			cout << endl << "See you next time!" << endl;
			play = false;
		}
	}
	
	return 0;
}// end main

NEWttt.cpp:135: error: âCellâ does not name a type
NEWttt.cpp: In constructor âTicTacToe::TicTacToe()â:
NEWttt.cpp:10: error: âboardâ was not declared in this scope
NEWttt.cpp: In member function âvoid TicTacToe::playTicTacToe()â:
NEWttt.cpp:27: error: âPlayerâ was not declared in this scope
NEWttt.cpp:27: error: expected â;â before âplayersâ
NEWttt.cpp:33: error: expected â;â before âmyPlayerâ
NEWttt.cpp:34: error: âCellâ was not declared in this scope
NEWttt.cpp:34: error: expected â;â before âmyCellâ
NEWttt.cpp:35: error: âmyPlayerâ was not declared in this scope
NEWttt.cpp:40: error: âmyCellâ was not declared in this scope
NEWttt.cpp: In member function âbool TicTacToe::checkBoard()â:
NEWttt.cpp:61: error: âboardâ was not declared in this scope
NEWttt.cpp:74: error: âboardâ was not declared in this scope
NEWttt.cpp:98: error: âboardâ was not declared in this scope
NEWttt.cpp:113: error: âboardâ was not declared in this scope
NEWttt.cpp: In member function âvoid TicTacToe::displayBoard()â:
NEWttt.cpp:127: error: âboardâ was not declared in this scope
NEWttt.cpp: In member function âvoid Player::displayTurn()â:
NEWttt.cpp:214: error: âinitPnameâ was not declared in this scope

In between lines 3-5 at the top, forward declare Cell and Player with

class Cell;
class Player;

or move their information up above TicTacToe but this is probably neater.

In between lines 3-5 at the top, forward declare Cell and Player with

class Cell;
class Player;

or move their information up above TicTacToe but this is probably neater.

that still didnt work. i get the same errors

Actually it did take care of some of the problems. However you have an interesting problem on your hands. Because your board is an array of Cells you need to have an overloaded == operator in your Cell class to be able to compare the spots on the board. It doesn't seem like you're to that point yet so you may have to redesign a little bit.

Actually it did take care of some of the problems. However you have an interesting problem on your hands. Because your board is an array of Cells you need to have an overloaded == operator in your Cell class to be able to compare the spots on the board. It doesn't seem like you're to that point yet so you may have to redesign a little bit.

ok im gonna try that

You need the != one too but that's just the NOT of the ==

You need the != one too but that's just the NOT of the ==

can u elaborate a little more about what u mean overloading the == operators in the cell class

Flip ahead a few chapters in your text and it should be there. I'm concerned that your instructor may not want you to use it yet. Basically you use the operator keyword to redefine what == means for your class (essentially your example breaks down to comparing two characters).
I think your best bet would be to just make your board an array of char instead of an array of cells.

Flip ahead a few chapters in your text and it should be there. I'm concerned that your instructor may not want you to use it yet. Basically you use the operator keyword to redefine what == means for your class (essentially your example breaks down to comparing two characters).
I think your best bet would be to just make your board an array of char instead of an array of cells.

ok no problem. im just gonna meet with my instructor tomorrow cause im, to put it simply, stuck lol. thanks for your help tho. i appreciate it

No problem, I mean I would take you through it all but you're probably not quite that far in the material yet. I looked around for a good straightforward tutorial on just the == operator and couldn't find one. It's always best to see what the instructor wants.

Well , for a good portion of those errors, you must INCLUDE the player class's header file, #include "Player.h" I presume, that would fix some errors I THINK. For the other errors, what up with the circumflex accent, and a few questions, did you get this code off of somewhere, because if you made this, then you should know how to use classes :D.

you must INCLUDE the player class's header file, #include "Player.h"

Agreed but I think he has them all laid out in one file.

what up with the circumflex accent

I've seen that on here before. I think if your compiler gives output with the word processor style angled quotes it's a character above 127 so the encoding gets messed up. Not sure where in the process that occurs.

oh ok I see :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.