Having some problems with two of my classes

Tic Tac Toe game in c++

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1364 Views - Last Post: 14 October 2009 - 02:03 PM Rate Topic: -----

#1 countrygirl1970   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-October 08

Having some problems with two of my classes

Posted 26 September 2009 - 12:50 PM

The classes that I am having some trouble with are highlighted in yellow and red:

In the char ticTac::checkWinner() area is to check to see if the player is the winner or if it is a tie game. The winner part is working but I am unble to get the tie part working. After the first move it is saying "Tie Game" and asking if you would like to play again.

In the void ticTac::checkPosn(int spot, char player) area is to check to see if the position is taken. The only part that is working is puting in the X or O into the position and saying "Nice Move".

Is there anyway I can get some suggestion on these problems.

Thank you for your help,


#include <iostream>
#include <cstdlib>
using namespace std;
class ticTac
{
	  private:
			 int counter;
			 char posn[9];
			 char player;
	  public:
			 //void nextPlayer();
			 char getPlayer();
			 void newBoard();
			 void startGame();
			 char checkWinner();
			 void checkPosn(int spot, char player);
			 void updateBoard();
			 void nextPlayer();
						
};
 
int main()
{
	char ch;
	ticTac Toe;
	do{
		   Toe.newBoard();		 //Brings up the new board
		   Toe.startGame();		//Starts the new game
		   cout << "Would you like to play again (Enter 'y')? ";
		   cin >> ch;
		   }while (ch == 'y');
 
	return 0;	
}

//The new board before the game starts
void ticTac::newBoard()
{
 
	 char posndef[9] = {'0', '1', '2', '3', '4', '5', '6', '7', '8'};
	 int i;
	 counter = 0;
	 player = 'x';
	 for (i=0; i<9; i++) posn[i]=posndef[i];
	 return;
 
};

//This will update the board with the 'X' or 'O'
void ticTac::updateBoard()
{
	 cout << "\n\n" <<endl;
	 cout << "\n\n\t\t" <<posn [0]<< "   | " <<posn [1]<< "  | " <<posn [2]<<endl;
	 cout << " \t\t	|	|   " <<endl;
	 cout << " \t\t ___|____|___ " <<endl;
	 cout << "\n\n\t\t" <<posn [3]<< "   | " <<posn [4]<< "  | " <<posn [5]<<endl;
	 cout << " \t\t	|	|   " <<endl;
	 cout << " \t\t ___|____|___ " <<endl;
	 cout << "\n\n\t\t" <<posn [6]<< "   | " <<posn [7]<< "  | " <<posn [8]<<endl;
	 cout << " \t\t	|	|   " <<endl;
	 cout << " \t\t	|	|   " <<endl;
	 cout << "\n" <<endl;
 
};

//Starts the game
void ticTac::startGame()
{
	 int spot;
	 char blank = ' ';
	  
	 cout << "Player " << getPlayer() <<" will be the first to play." <<endl;
 
	 do {
		 updateBoard();			  // display update board   
 
		 cout << "\n\n Player " << getPlayer() <<" choose a position between (0 - 8)." <<endl;
		 cin >> spot;
		 
		 checkPosn(spot, getPlayer());			// to check if the position is taken
					  
		 system("CLS"); 
				  
		 nextPlayer();  
		 }while ( checkWinner() == blank );
 
		 return;
 
};
//Checks to see who the winner will be	 
char ticTac::checkWinner()
{
	 char Winner = ' ';
 
	 // Check if X wins   
	 if (posn[0] == 'x' && posn[1] == 'x' && posn[2] == 'x') Winner = 'x';
	 if (posn[3] == 'x' && posn[4] == 'x' && posn[5] == 'x') Winner = 'x';
	 if (posn[6] == 'x' && posn[7] == 'x' && posn[8] == 'x') Winner = 'x';
	 if (posn[0] == 'x' && posn[3] == 'x' && posn[6] == 'x') Winner = 'x';
	 if (posn[1] == 'x' && posn[4] == 'x' && posn[7] == 'x') Winner = 'x';
	 if (posn[2] == 'x' && posn[5] == 'x' && posn[8] == 'x') Winner = 'x';
	 if (posn[0] == 'x' && posn[4] == 'x' && posn[8] == 'x') Winner = 'x';
	 if (posn[2] == 'x' && posn[4] == 'x' && posn[6] == 'x') Winner = 'x';
	 if (Winner == 'x' )  
	   {cout << "Player1 wins the game." <<endl; return Winner; };
 
	 // Check if O wins	 
	 if (posn[0] == 'o' && posn[1] == 'o' && posn[2] == 'o') Winner = 'o';
	 if (posn[3] == 'o' && posn[4] == 'o' && posn[5] == 'o') Winner = 'o';
	 if (posn[6] == 'o' && posn[7] == 'o' && posn[8] == 'o') Winner = 'o';
	 if (posn[0] == 'o' && posn[3] == 'o' && posn[6] == 'o') Winner = 'o';
	 if (posn[1] == 'o' && posn[4] == 'o' && posn[7] == 'o') Winner = 'o';
	 if (posn[2] == 'o' && posn[5] == 'o' && posn[8] == 'o') Winner = 'o';
	 if (posn[0] == 'o' && posn[4] == 'o' && posn[8] == 'o') Winner = 'o';
	 if (posn[2] == 'o' && posn[4] == 'o' && posn[6] == 'o') Winner = 'o';
	 if (Winner == 'o' )
	   {cout << "Player2 wins the game." <<endl; return Winner; };
			  
	 // Check if tie
	 if (Winner != 'x' || Winner != 'o') Winner = 'tie';
	 if (Winner == 'tie')
	   {cout << "Tie Game." <<endl; return Winner; }; 
		  
	 
	 
	 
	 return Winner;		
};
//Checks to see if the position is taken	 
void ticTac::checkPosn(int spot, char player)
{
 
	 if (posn[spot] == 'x' || posn[spot] == 'o'){
		  cout << "Position already taken, please choose another position." <<endl;
				  }
				  else {
					   posn[spot] = player;
								  cout << "Nice move." <<endl;
				   
		  };
 
	 counter++;
 
	 return;
 
};
//Tells the next player that it's thier turn
void ticTac::nextPlayer()
{
	 if (player == 'x')
		 player = 'o';
	 else player = 'x';
	 return;
};

//This switches between players		 
char ticTac::getPlayer()
{
	 return player;
};




This post has been edited by countrygirl1970: 26 September 2009 - 12:53 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Having some problems with two of my classes

#2 Enter_Kratos   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 72
  • Joined: 26-September 09

Re: Having some problems with two of my classes

Posted 26 September 2009 - 01:12 PM

Hey I only really looked at your checkWinner function if I'm honest.
From what I saw it looks like you need to have a read up on the char data type and then look back at your function and see what you think is wrong.

You'll probably hit yourself after :) lol

If I get a chance I'll have a little look through the rest of your code
Was This Post Helpful? 0
  • +
  • -

#3 poncho4all   User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,422
  • Joined: 15-July 09

Re: Having some problems with two of my classes

Posted 26 September 2009 - 01:15 PM

You need to remove the semicolon after the function declaration.
Here
#include <iostream>
#include <cstdlib>
using namespace std;
class ticTac
{
	  private:
			 int counter;
			 char posn[9];
			 char player;
	  public:
			 //void nextPlayer();
			 char getPlayer();
			 void newBoard();
			 void startGame();
			 char checkWinner();
			 void checkPosn(int spot, char player);
			 void updateBoard();
			 void nextPlayer();
					   
};
 
int main()
{
	char ch;
	ticTac Toe;
	do{
		   Toe.newBoard();		 //Brings up the new board
		   Toe.startGame();		//Starts the new game
		   cout << "Would you like to play again (Enter 'y')? ";
		   cin >> ch;
		   }while (ch == 'y');
 
	return 0;	
}

//The new board before the game starts
void ticTac::newBoard()
{
 
	 char posndef[9] = {'0', '1', '2', '3', '4', '5', '6', '7', '8'};
	 int i;
	 counter = 0;
	 player = 'x';
	 for (i=0; i<9; i++) posn[i]=posndef[i];
	 return;
 
}

//This will update the board with the 'X' or 'O'
void ticTac::updateBoard()
{
	 cout << "\n\n" <<endl;
	 cout << "\n\n\t\t" <<posn [0]<< "   | " <<posn [1]<< "  | " <<posn [2]<<endl;
	 cout << " \t\t	|	|   " <<endl;
	 cout << " \t\t ___|____|___ " <<endl;
	 cout << "\n\n\t\t" <<posn [3]<< "   | " <<posn [4]<< "  | " <<posn [5]<<endl;
	 cout << " \t\t	|	|   " <<endl;
	 cout << " \t\t ___|____|___ " <<endl;
	 cout << "\n\n\t\t" <<posn [6]<< "   | " <<posn [7]<< "  | " <<posn [8]<<endl;
	 cout << " \t\t	|	|   " <<endl;
	 cout << " \t\t	|	|   " <<endl;
	 cout << "\n" <<endl;
 
}

//Starts the game
void ticTac::startGame()
{
	 int spot;
	 char blank = ' ';
	 
	 cout << "Player " << getPlayer() <<" will be the first to play." <<endl;
 
	 do {
		 updateBoard();			  // display update board  
 
		 cout << "\n\n Player " << getPlayer() <<" choose a position between (0 - 8)." <<endl;
		 cin >> spot;
		 
		 checkPosn(spot, getPlayer());			// to check if the position is taken
					 
		 system("CLS");
				 
		 nextPlayer();  
		 }while ( checkWinner() == blank );
 
		 return;
 
}
//Checks to see who the winner will be	
char ticTac::checkWinner()
{
	 char Winner = ' ';
 
	 // Check if X wins  
	 if (posn[0] == 'x' && posn[1] == 'x' && posn[2] == 'x') Winner = 'x';
	 if (posn[3] == 'x' && posn[4] == 'x' && posn[5] == 'x') Winner = 'x';
	 if (posn[6] == 'x' && posn[7] == 'x' && posn[8] == 'x') Winner = 'x';
	 if (posn[0] == 'x' && posn[3] == 'x' && posn[6] == 'x') Winner = 'x';
	 if (posn[1] == 'x' && posn[4] == 'x' && posn[7] == 'x') Winner = 'x';
	 if (posn[2] == 'x' && posn[5] == 'x' && posn[8] == 'x') Winner = 'x';
	 if (posn[0] == 'x' && posn[4] == 'x' && posn[8] == 'x') Winner = 'x';
	 if (posn[2] == 'x' && posn[4] == 'x' && posn[6] == 'x') Winner = 'x';
	 if (Winner == 'x' )  
	   {cout << "Player1 wins the game." <<endl; return Winner; };
 
	 // Check if O wins	
	 if (posn[0] == 'o' && posn[1] == 'o' && posn[2] == 'o') Winner = 'o';
	 if (posn[3] == 'o' && posn[4] == 'o' && posn[5] == 'o') Winner = 'o';
	 if (posn[6] == 'o' && posn[7] == 'o' && posn[8] == 'o') Winner = 'o';
	 if (posn[0] == 'o' && posn[3] == 'o' && posn[6] == 'o') Winner = 'o';
	 if (posn[1] == 'o' && posn[4] == 'o' && posn[7] == 'o') Winner = 'o';
	 if (posn[2] == 'o' && posn[5] == 'o' && posn[8] == 'o') Winner = 'o';
	 if (posn[0] == 'o' && posn[4] == 'o' && posn[8] == 'o') Winner = 'o';
	 if (posn[2] == 'o' && posn[4] == 'o' && posn[6] == 'o') Winner = 'o';
	 if (Winner == 'o' )
	   {cout << "Player2 wins the game." <<endl; return Winner; };
			 
	 // Check if tie
	 if (Winner != 'x' || Winner != 'o') Winner = 'tie';
	 if (Winner == 'tie')
	   {cout << "Tie Game." <<endl; return Winner; };
		 
	 
	 
	 
	 return Winner;		
}
//Checks to see if the position is taken	
void ticTac::checkPosn(int spot, char player)
{
 
	 if (posn[spot] == 'x' || posn[spot] == 'o'){
		  cout << "Position already taken, please choose another position." <<endl;
				  }
				  else {
					   posn[spot] = player;
								  cout << "Nice move." <<endl;
				   
		  };
 
	 counter++;
 
	 return;
 
}
//Tells the next player that it's thier turn
void ticTac::nextPlayer()
{
	 if (player == 'x')
		 player = 'o';
	 else player = 'x';
	 return;
}

//This switches between players		
char ticTac::getPlayer()
{
	 return player;
}



Try that and let us know if the error still there or if you had new ones ;)
Was This Post Helpful? 0
  • +
  • -

#4 countrygirl1970   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-October 08

Re: Having some problems with two of my classes

Posted 26 September 2009 - 01:27 PM

Ponch4all,

I did what you said and still getting the same as before. Thank you for looking at my code and giving me some suggestions.

Countrygirl1970
Was This Post Helpful? 0
  • +
  • -

#5 poncho4all   User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,422
  • Joined: 15-July 09

Re: Having some problems with two of my classes

Posted 26 September 2009 - 01:54 PM

Ok i looked more closely to your code and found to more errors here you go i think its gonna make it work better :)
 if (posn[spot] == 'x' || posn[spot] == 'o'){
		  cout << "Position already taken, please choose another position." <<endl;
				  }
				  else {
					   posn[spot] = player;
								  cout << "Nice move." <<endl;
				   
		  }//;take out the semicolon 



And more importantly

while ( checkWinner() == blank );


Should be
while ( checkWinner() != blank );


Hope that helps ;)

[EDIT]aww i failz :(
It still had a bug here its fixed :)
#include <iostream>
#include <cstdlib>
using namespace std;
class ticTac
{
	  private:
			 int counter;
			 char posn[9];
			 char player;
	  public:
			 //void nextPlayer();
			 char getPlayer();
			 void newBoard();
			 void startGame();
			 char checkWinner();
			 void checkPosn(int spot, char player);
			 void updateBoard();
			 void nextPlayer();
					   
};
 
int main()
{
	char ch;
	ticTac Toe;
	do{
		   Toe.newBoard();		 //Brings up the new board
		   Toe.startGame();		//Starts the new game
		   cout << "Would you like to play again (Enter 'y')? ";
		   cin >> ch;
		   }while (ch == 'y');
 
	return 0;	
}

//The new board before the game starts
void ticTac::newBoard()
{
 
	 char posndef[9] = {'0', '1', '2', '3', '4', '5', '6', '7', '8'};
	 int i;
	 counter = 0;
	 player = 'x';
	 for (i=0; i<9; i++) posn[i]=posndef[i];
	 return;
 
}

//This will update the board with the 'X' or 'O'
void ticTac::updateBoard()
{
	 cout << "\n\n" <<endl;
	 cout << "\n\n\t\t" <<posn [0]<< "   | " <<posn [1]<< "  | " <<posn [2]<<endl;
	 cout << " \t\t	|	|   " <<endl;
	 cout << " \t\t ___|____|___ " <<endl;
	 cout << "\n\n\t\t" <<posn [3]<< "   | " <<posn [4]<< "  | " <<posn [5]<<endl;
	 cout << " \t\t	|	|   " <<endl;
	 cout << " \t\t ___|____|___ " <<endl;
	 cout << "\n\n\t\t" <<posn [6]<< "   | " <<posn [7]<< "  | " <<posn [8]<<endl;
	 cout << " \t\t	|	|   " <<endl;
	 cout << " \t\t	|	|   " <<endl;
	 cout << "\n" <<endl;
 
}

//Starts the game
void ticTac::startGame()
{
	 int spot;
	 char blank = ' ';
	 
	 cout << "Player " << getPlayer() <<" will be the first to play." <<endl;
 
	 do {
		 updateBoard();			  // display update board  
 
		 cout << "\n\n Player " << getPlayer() <<" choose a position between (0 - 8)." <<endl;
		 cin >> spot;
		 
		 checkPosn(spot, getPlayer());			// to check if the position is taken
					 
		 system("CLS");
				 
		 nextPlayer();  
		 if(checkWinner()=='x' || checkWinner() == 'o')
			 blank ='v';
		 }while ( blank!='v' );
 
		 return;
 
}
//Checks to see who the winner will be	
char ticTac::checkWinner()
{
	 char Winner = ' ';
 
	 // Check if X wins  
	 if (posn[0] == 'x' && posn[1] == 'x' && posn[2] == 'x') Winner = 'x';
	 if (posn[3] == 'x' && posn[4] == 'x' && posn[5] == 'x') Winner = 'x';
	 if (posn[6] == 'x' && posn[7] == 'x' && posn[8] == 'x') Winner = 'x';
	 if (posn[0] == 'x' && posn[3] == 'x' && posn[6] == 'x') Winner = 'x';
	 if (posn[1] == 'x' && posn[4] == 'x' && posn[7] == 'x') Winner = 'x';
	 if (posn[2] == 'x' && posn[5] == 'x' && posn[8] == 'x') Winner = 'x';
	 if (posn[0] == 'x' && posn[4] == 'x' && posn[8] == 'x') Winner = 'x';
	 if (posn[2] == 'x' && posn[4] == 'x' && posn[6] == 'x') Winner = 'x';
	 if (Winner == 'x' )  
	   {cout << "Player1 wins the game." <<endl; return Winner; };
 
	 // Check if O wins	
	 if (posn[0] == 'o' && posn[1] == 'o' && posn[2] == 'o') Winner = 'o';
	 if (posn[3] == 'o' && posn[4] == 'o' && posn[5] == 'o') Winner = 'o';
	 if (posn[6] == 'o' && posn[7] == 'o' && posn[8] == 'o') Winner = 'o';
	 if (posn[0] == 'o' && posn[3] == 'o' && posn[6] == 'o') Winner = 'o';
	 if (posn[1] == 'o' && posn[4] == 'o' && posn[7] == 'o') Winner = 'o';
	 if (posn[2] == 'o' && posn[5] == 'o' && posn[8] == 'o') Winner = 'o';
	 if (posn[0] == 'o' && posn[4] == 'o' && posn[8] == 'o') Winner = 'o';
	 if (posn[2] == 'o' && posn[4] == 'o' && posn[6] == 'o') Winner = 'o';
	 if (Winner == 'o' )
	   {cout << "Player2 wins the game." <<endl; return Winner; };
			 
	 // Check if tie
	 if (Winner != 'x' || Winner != 'o') Winner = 'tie';
	 if (Winner == 'tie')
	   {cout << "Tie Game." <<endl; return Winner; };
		 
	 
	 
	 
	 return Winner;		
}
//Checks to see if the position is taken	
void ticTac::checkPosn(int spot, char player)
{
 
	 if (posn[spot] == 'x' || posn[spot] == 'o'){
		  cout << "Position already taken, please choose another position." <<endl;
				  }
				  else {
					   posn[spot] = player;
								  cout << "Nice move." <<endl;
				   
		  }
 
	 counter++;
 
	 return;
 
}
//Tells the next player that it's thier turn
void ticTac::nextPlayer()
{
	 if (player == 'x')
		 player = 'o';
	 else player = 'x';
	 return;
}

//This switches between players		
char ticTac::getPlayer()
{
	 return player;
}


Important changes on function startGame, added one if to change the while condition, that for some reason had a bug.
Also this
 if (Winner != 'x' || Winner != 'o') Winner = 'tie';

Winner is gonna be truncated and i guess you might get some unwanted bugs, your char variable can only store 1 character not a string. Maybe you want to use strings or change the tie to t . Well its just a take a note since you might get unwanted results ;)

This post has been edited by poncho4all: 26 September 2009 - 02:09 PM

Was This Post Helpful? 0
  • +
  • -

#6 countrygirl1970   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-October 08

Re: Having some problems with two of my classes

Posted 26 September 2009 - 02:21 PM

ponch4all,

Thank you for the help. I did the change on:

if (Winner != 'x' || Winner != 'o') Winner = 'tie';




the new change
if (Winner != 'x' || Winner != 'o') Winner = 't';




the olny thing I am getting is tie game at the top after
each move and it is still requesting a move after the board is filled.

This post has been edited by countrygirl1970: 26 September 2009 - 02:33 PM

Was This Post Helpful? 0
  • +
  • -

#7 poncho4all   User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,422
  • Joined: 15-July 09

Re: Having some problems with two of my classes

Posted 26 September 2009 - 02:33 PM

Did you read my EDIT?
Either way i have already played with this code and it works :)
#include <iostream>
#include <cstdlib>
using namespace std;
class ticTac
{
	  private:
			 int counter;
			 char posn[9];
			 char player;
	  public:
			 //void nextPlayer();
			 char getPlayer();
			 void newBoard();
			 void startGame();
			 char checkWinner();
			 void checkPosn(int spot, char player);
			 void updateBoard();
			 void nextPlayer();
					   
};
 
int main()
{
	char ch;
	ticTac Toe;
	do{
		   Toe.newBoard();		 //Brings up the new board
		   Toe.startGame();		//Starts the new game
		   cout << "Would you like to play again (Enter 'y')? ";
		   cin >> ch;
		   }while (ch == 'y');
 
	return 0;	
}

//The new board before the game starts
void ticTac::newBoard()
{
 
	 char posndef[9] = {'0', '1', '2', '3', '4', '5', '6', '7', '8'};
	 int i;
	 counter = 0;
	 player = 'x';
	 for (i=0; i<9; i++) posn[i]=posndef[i];
	 return;
 
}

//This will update the board with the 'X' or 'O'
void ticTac::updateBoard()
{
	 cout << "\n\n" <<endl;
	 cout << "\n\n\t\t" <<posn [0]<< "   | " <<posn [1]<< "  | " <<posn [2]<<endl;
	 cout << " \t\t	|	|   " <<endl;
	 cout << " \t\t ___|____|___ " <<endl;
	 cout << "\n\n\t\t" <<posn [3]<< "   | " <<posn [4]<< "  | " <<posn [5]<<endl;
	 cout << " \t\t	|	|   " <<endl;
	 cout << " \t\t ___|____|___ " <<endl;
	 cout << "\n\n\t\t" <<posn [6]<< "   | " <<posn [7]<< "  | " <<posn [8]<<endl;
	 cout << " \t\t	|	|   " <<endl;
	 cout << " \t\t	|	|   " <<endl;
	 cout << "\n" <<endl;
 
}

//Starts the game
void ticTac::startGame()
{
	 int spot;
	 char blank = ' ';
	 
	 cout << "Player " << getPlayer() <<" will be the first to play." <<endl;
 
	 do {
		 updateBoard();			  // display update board  
 
		 cout << "\n\n Player " << getPlayer() <<" choose a position between (0 - 8)." <<endl;
		 cin >> spot;
		 
		 checkPosn(spot, getPlayer());			// to check if the position is taken
					 
		 system("CLS");
				 
		 nextPlayer();  
		 if(checkWinner()=='x' || checkWinner() == 'o')
			 blank ='v';
		 }while ( blank!='v' );
 
		 return;
 
}
//Checks to see who the winner will be	
char ticTac::checkWinner()
{
	 char Winner = ' ';
 
	 // Check if X wins  
	 if (posn[0] == 'x' && posn[1] == 'x' && posn[2] == 'x') Winner = 'x';
	 if (posn[3] == 'x' && posn[4] == 'x' && posn[5] == 'x') Winner = 'x';
	 if (posn[6] == 'x' && posn[7] == 'x' && posn[8] == 'x') Winner = 'x';
	 if (posn[0] == 'x' && posn[3] == 'x' && posn[6] == 'x') Winner = 'x';
	 if (posn[1] == 'x' && posn[4] == 'x' && posn[7] == 'x') Winner = 'x';
	 if (posn[2] == 'x' && posn[5] == 'x' && posn[8] == 'x') Winner = 'x';
	 if (posn[0] == 'x' && posn[4] == 'x' && posn[8] == 'x') Winner = 'x';
	 if (posn[2] == 'x' && posn[4] == 'x' && posn[6] == 'x') Winner = 'x';
	 if (Winner == 'x' )  
	   {cout << "Player1 wins the game." <<endl; return Winner; };
 
	 // Check if O wins	
	 if (posn[0] == 'o' && posn[1] == 'o' && posn[2] == 'o') Winner = 'o';
	 if (posn[3] == 'o' && posn[4] == 'o' && posn[5] == 'o') Winner = 'o';
	 if (posn[6] == 'o' && posn[7] == 'o' && posn[8] == 'o') Winner = 'o';
	 if (posn[0] == 'o' && posn[3] == 'o' && posn[6] == 'o') Winner = 'o';
	 if (posn[1] == 'o' && posn[4] == 'o' && posn[7] == 'o') Winner = 'o';
	 if (posn[2] == 'o' && posn[5] == 'o' && posn[8] == 'o') Winner = 'o';
	 if (posn[0] == 'o' && posn[4] == 'o' && posn[8] == 'o') Winner = 'o';
	 if (posn[2] == 'o' && posn[4] == 'o' && posn[6] == 'o') Winner = 'o';
	 if (Winner == 'o' )
	   {cout << "Player2 wins the game." <<endl; return Winner; };
			 
	 // Check if tie
	 if (Winner != 'x' || Winner != 'o') Winner = 'tie';
	 if (Winner == 'tie')
	   {cout << "Tie Game." <<endl; return Winner; };
		 
	 
	 
	 
	 return Winner;		
}
//Checks to see if the position is taken	
void ticTac::checkPosn(int spot, char player)
{
 
	 if (posn[spot] == 'x' || posn[spot] == 'o'){
		  cout << "Position already taken, please choose another position." <<endl;
				  }
				  else {
					   posn[spot] = player;
								  cout << "Nice move." <<endl;
				   
		  }
 
	 counter++;
 
	 return;
 
}
//Tells the next player that it's thier turn
void ticTac::nextPlayer()
{
	 if (player == 'x')
		 player = 'o';
	 else player = 'x';
	 return;
}

//This switches between players		
char ticTac::getPlayer()
{
	 return player;
}



Copy paste it and try ;)
Was This Post Helpful? 0
  • +
  • -

#8 countrygirl1970   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-October 08

Re: Having some problems with two of my classes

Posted 26 September 2009 - 02:35 PM

Enter_Kratos,

I did as you said and made the change.
Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#9 Enter_Kratos   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 72
  • Joined: 26-September 09

Re: Having some problems with two of my classes

Posted 26 September 2009 - 02:39 PM

No problem.

Is your code working well now or are you still having trouble with your other functions?


EDIT: I'll let poncho carry on helping as I he's looked more thoroughly into your code

This post has been edited by Enter_Kratos: 26 September 2009 - 02:51 PM

Was This Post Helpful? 0
  • +
  • -

#10 countrygirl1970   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-October 08

Re: Having some problems with two of my classes

Posted 26 September 2009 - 02:51 PM

ponch4all,

Thank you, I am using Dev-C++ for this program. I have done the changes as you said, I can still compile the program.

Here is the code with the changes:

#include <iostream>
#include <cstdlib>
using namespace std;
class ticTac
{
	  private:
			 int counter;
			 char posn[9];
			 char player;
	  public:
			 //void nextPlayer();
			 char getPlayer();
			 void newBoard();
			 void startGame();
			 char checkWinner();
			 void checkPosn(int spot, char player);
			 void updateBoard();
			 void nextPlayer();
						
};
 
int main()
{
	char ch;
	ticTac Toe;
	do{
		   Toe.newBoard();		 //Brings up the new board
		   Toe.startGame();		//Starts the new game
		   cout << "Would you like to play again (Enter 'y')? ";
		   cin >> ch;
		   }while (ch == 'y');
 
	return 0;	
}

//The new board before the game starts
void ticTac::newBoard()
{
 
	 char posndef[9] = {'0', '1', '2', '3', '4', '5', '6', '7', '8'};
	 int i;
	 counter = 0;
	 player = 'x';
	 for (i=0; i<9; i++) posn[i]=posndef[i];
	 return;
 
}

//This will update the board with the 'X' or 'O'
void ticTac::updateBoard()
{
	 cout << "\n\n" <<endl;
	 cout << "\n\n\t\t" <<posn [0]<< "   | " <<posn [1]<< "  | " <<posn [2]<<endl;
	 cout << " \t\t	|	|   " <<endl;
	 cout << " \t\t ___|____|___ " <<endl;
	 cout << "\n\n\t\t" <<posn [3]<< "   | " <<posn [4]<< "  | " <<posn [5]<<endl;
	 cout << " \t\t	|	|   " <<endl;
	 cout << " \t\t ___|____|___ " <<endl;
	 cout << "\n\n\t\t" <<posn [6]<< "   | " <<posn [7]<< "  | " <<posn [8]<<endl;
	 cout << " \t\t	|	|   " <<endl;
	 cout << " \t\t	|	|   " <<endl;
	 cout << "\n" <<endl;
	  
}

//Starts the game
void ticTac::startGame()
{
	 int spot;
	 char blank = ' ';
	  
	 cout << "\n\nPlayer " << getPlayer() <<" will be the first to play." <<endl;
 
	 do {
		 updateBoard();			  // display update board   
 
		 cout << "Player " << getPlayer() <<" choose a position between (0 - 8)." <<endl;
		 cin >> spot;
		 
		 checkPosn(spot, getPlayer());			// to check if the position is taken
					  
		 system("CLS"); 
				  
		 nextPlayer();  
		 if(checkWinner() == 'x' || checkWinner() == 'o')
			 blank = 'v';
		 }while ( blank != 'v' );

 
		 return;
 
}

//Checks to see who the winner will be	 
char ticTac::checkWinner()
{
	 char game = ' ';
 
	 // Check if X wins   
	 if (posn[0] == 'x' && posn[1] == 'x' && posn[2] == 'x') game = 'x';
	 if (posn[3] == 'x' && posn[4] == 'x' && posn[5] == 'x') game = 'x';
	 if (posn[6] == 'x' && posn[7] == 'x' && posn[8] == 'x') game = 'x';
	 if (posn[0] == 'x' && posn[3] == 'x' && posn[6] == 'x') game = 'x';
	 if (posn[1] == 'x' && posn[4] == 'x' && posn[7] == 'x') game = 'x';
	 if (posn[2] == 'x' && posn[5] == 'x' && posn[8] == 'x') game = 'x';
	 if (posn[0] == 'x' && posn[4] == 'x' && posn[8] == 'x') game = 'x';
	 if (posn[2] == 'x' && posn[4] == 'x' && posn[6] == 'x') game = 'x';
	 if (game == 'x' )  
	   {cout << "Player1 wins the game." <<endl; return game; };
 
	 // Check if O wins	 
	 if (posn[0] == 'o' && posn[1] == 'o' && posn[2] == 'o') game = 'o';
	 if (posn[3] == 'o' && posn[4] == 'o' && posn[5] == 'o') game = 'o';
	 if (posn[6] == 'o' && posn[7] == 'o' && posn[8] == 'o') game = 'o';
	 if (posn[0] == 'o' && posn[3] == 'o' && posn[6] == 'o') game = 'o';
	 if (posn[1] == 'o' && posn[4] == 'o' && posn[7] == 'o') game = 'o';
	 if (posn[2] == 'o' && posn[5] == 'o' && posn[8] == 'o') game = 'o';
	 if (posn[0] == 'o' && posn[4] == 'o' && posn[8] == 'o') game = 'o';
	 if (posn[2] == 'o' && posn[4] == 'o' && posn[6] == 'o') game = 'o';
	 if (game == 'o' )
	   {cout << "Player2 wins the game." <<endl; return game; };
			  
	 // Check if tie
	if (game != 'x' && game != 'o') game = 't';
	if (game == 't')
	   {cout << "Tie Game." <<endl; return game; }; 

		  
	 
	 
	 
	 return game;		
}

//Checks to see if the position is taken	 
void ticTac::checkPosn(int spot, char player)
{
 
	 if (posn[spot] == 'x' || posn[spot] == 'o'){
		  cout << "Position already taken, please choose another position." <<endl;
				  }
				  else {
					   posn[spot] = player;
								  cout << "Nice move." <<endl;
				   
		  }
 
	 counter++;
 
	 return;
 
}

//Tells the next player that it's thier turn
void ticTac::nextPlayer()
{
	 if (player == 'x')
		 player = 'o';
	 else player = 'x';
	 return;
}

//This switches between players		 
char ticTac::getPlayer()
{
	 return player;
}










Enter_Kratos,

I am still having problems with those functions.

This post has been edited by countrygirl1970: 26 September 2009 - 02:50 PM

Was This Post Helpful? 0
  • +
  • -

#11 poncho4all   User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,422
  • Joined: 15-July 09

Re: Having some problems with two of my classes

Posted 26 September 2009 - 02:59 PM

What problems or errors are you getting?
if (game != 'x' && game != 'o') 
		game = 't';
	if (game == 't'){
		cout << "Tie Game." <<endl; return game; 
	}//;remove this semicolon

Just a pointer this is gonna make your program run into 't' all the time, before doing this youll need to check if there are no left spaces to play in.

[EDIT]I added a for to check the board on function checkWinner(), and added || Winner == 't' in the startGame function
Try that and let me know :)
#include <iostream>
#include <cstdlib>
using namespace std;
class ticTac
{
	  private:
			 int counter;
			 char posn[9];
			 char player;
	  public:
			 //void nextPlayer();
			 char getPlayer();
			 void newBoard();
			 void startGame();
			 char checkWinner();
			 void checkPosn(int spot, char player);
			 void updateBoard();
			 void nextPlayer();
					   
};
 
int main()
{
	char ch;
	ticTac Toe;
	do{
		   Toe.newBoard();		 //Brings up the new board
		   Toe.startGame();		//Starts the new game
		   cout << "Would you like to play again (Enter 'y')? ";
		   cin >> ch;
		   }while (ch == 'y');
 
	return 0;	
}

//The new board before the game starts
void ticTac::newBoard()
{
 
	 char posndef[9] = {'0', '1', '2', '3', '4', '5', '6', '7', '8'};
	 int i;
	 counter = 0;
	 player = 'x';
	 for (i=0; i<9; i++) posn[i]=posndef[i];
	 return;
 
}

//This will update the board with the 'X' or 'O'
void ticTac::updateBoard()
{
	 cout << "\n\n" <<endl;
	 cout << "\n\n\t\t" <<posn [0]<< "   | " <<posn [1]<< "  | " <<posn [2]<<endl;
	 cout << " \t\t	|	|   " <<endl;
	 cout << " \t\t ___|____|___ " <<endl;
	 cout << "\n\n\t\t" <<posn [3]<< "   | " <<posn [4]<< "  | " <<posn [5]<<endl;
	 cout << " \t\t	|	|   " <<endl;
	 cout << " \t\t ___|____|___ " <<endl;
	 cout << "\n\n\t\t" <<posn [6]<< "   | " <<posn [7]<< "  | " <<posn [8]<<endl;
	 cout << " \t\t	|	|   " <<endl;
	 cout << " \t\t	|	|   " <<endl;
	 cout << "\n" <<endl;
	 
}

//Starts the game
void ticTac::startGame()
{
	 int spot;
	 char blank = ' ';
	 
	 cout << "\n\nPlayer " << getPlayer() <<" will be the first to play." <<endl;
 
	 do {
		 updateBoard();			  // display update board  
 
		 cout << "Player " << getPlayer() <<" choose a position between (0 - 8)." <<endl;
		 cin >> spot;
		 
		 checkPosn(spot, getPlayer());			// to check if the position is taken
					 
		 system("CLS");
				 
		 nextPlayer();  
		 if(checkWinner() == 'x' || checkWinner() == 'o'|| checkWinner() == 't')
			 blank = 'v';
		 }while ( blank != 'v' );

 
		 return;
 
}

//Checks to see who the winner will be	
char ticTac::checkWinner()
{	
	int check=1;
	 char game = ' ';
 
	 // Check if X wins  
	 if (posn[0] == 'x' && posn[1] == 'x' && posn[2] == 'x') game = 'x';
	 if (posn[3] == 'x' && posn[4] == 'x' && posn[5] == 'x') game = 'x';
	 if (posn[6] == 'x' && posn[7] == 'x' && posn[8] == 'x') game = 'x';
	 if (posn[0] == 'x' && posn[3] == 'x' && posn[6] == 'x') game = 'x';
	 if (posn[1] == 'x' && posn[4] == 'x' && posn[7] == 'x') game = 'x';
	 if (posn[2] == 'x' && posn[5] == 'x' && posn[8] == 'x') game = 'x';
	 if (posn[0] == 'x' && posn[4] == 'x' && posn[8] == 'x') game = 'x';
	 if (posn[2] == 'x' && posn[4] == 'x' && posn[6] == 'x') game = 'x';
	 if (game == 'x' ){
		 cout << "Player1 wins the game." <<endl; 
		 return game; 
	 }
 
	 // Check if O wins	
	 if (posn[0] == 'o' && posn[1] == 'o' && posn[2] == 'o') game = 'o';
	 if (posn[3] == 'o' && posn[4] == 'o' && posn[5] == 'o') game = 'o';
	 if (posn[6] == 'o' && posn[7] == 'o' && posn[8] == 'o') game = 'o';
	 if (posn[0] == 'o' && posn[3] == 'o' && posn[6] == 'o') game = 'o';
	 if (posn[1] == 'o' && posn[4] == 'o' && posn[7] == 'o') game = 'o';
	 if (posn[2] == 'o' && posn[5] == 'o' && posn[8] == 'o') game = 'o';
	 if (posn[0] == 'o' && posn[4] == 'o' && posn[8] == 'o') game = 'o';
	 if (posn[2] == 'o' && posn[4] == 'o' && posn[6] == 'o') game = 'o';
	 if (game == 'o' ){
		 cout << "Player2 wins the game." <<endl; 
		 return game; 
	 }
	 
	 for(int i=0;i<8;i++){
		 if(posn[i]>='1'&&posn[i]<='8'){
			 check=0;
			 break;
		 }
	 }		
	 // Check if tie
	if (game != 'x' && game != 'o'&&check==1) 
		game = 't';
	if (game == 't'){
		cout << "Tie Game." <<endl; 
		return game; 
	}	
}

//Checks to see if the position is taken	
void ticTac::checkPosn(int spot, char player)
{
 
	 if (posn[spot] == 'x' || posn[spot] == 'o'){
		  cout << "Position already taken, please choose another position." <<endl;
				  }
				  else {
					   posn[spot] = player;
								  cout << "Nice move." <<endl;
				   
		  }
 
	 counter++;
 
	 return;
 
}

//Tells the next player that it's thier turn
void ticTac::nextPlayer()
{
	 if (player == 'x')
		 player = 'o';
	 else player = 'x';
	 return;
}

//This switches between players		
char ticTac::getPlayer()
{
	 return player;
}



This post has been edited by poncho4all: 26 September 2009 - 03:23 PM

Was This Post Helpful? 0
  • +
  • -

#12 amplayer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-August 09

Re: Having some problems with two of my classes

Posted 26 September 2009 - 04:01 PM

Change the following line:
if (Winner != 'x' || Winner != 'o') Winner = 'tie';
to:
if (Winner != 'x' && Winner != 'o') Winner = 'tie';
Was This Post Helpful? 0
  • +
  • -

#13 countrygirl1970   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-October 08

Re: Having some problems with two of my classes

Posted 28 September 2009 - 12:58 PM

ponch4all and amplayer,

Thank you for your help.
Was This Post Helpful? 0
  • +
  • -

#14 poncho4all   User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,422
  • Joined: 15-July 09

Re: Having some problems with two of my classes

Posted 28 September 2009 - 01:14 PM

No problem :P
Is all working good now?
Was This Post Helpful? 0
  • +
  • -

#15 countrygirl1970   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-October 08

Re: Having some problems with two of my classes

Posted 06 October 2009 - 08:25 AM

View Postponcho4all, on 28 Sep, 2009 - 12:14 PM, said:

No problem :P
Is all working good now?




poncho4all,

I was able to get the program working to the way I needed it to work. Now I am working on getting to make sure the person playing the game inputs only the position numbers. As long as the person inputs the number, the program works. My instructor wants me to refine my program so that it wont crash my program under the part of ::startGame. Do you have any sugestions. I have tried a few things.


#include <iostream>
#include <cstdlib>
using namespace std;
class ticTac
{
	  private:
			 int counter;
			 char posn[9];
			 char player;
	  public:
			 void nextPlayer();
			 char getPlayer();
			 void newBoard();
			 void startGame();
			 char checkWinner();
			 bool checkPosn(int spot, char player);
			 void updateBoard();
									 
};
 
int main()
{
	char ch;
	ticTac Toe;
	do{
		   Toe.newBoard();		 //Brings up the new board
		   Toe.startGame();		//Starts the new game
		   cout << "Would you like to play again (Enter 'y')? ";
		   cin >> ch;
		   }while (ch == 'y');
 
	return 0;	
}

//The new board before the game starts
void ticTac::newBoard()
{
 
	 char posndef[9] = {'0', '1', '2', '3', '4', '5', '6', '7', '8'};
	 int i;
	 counter = 0;
	 player = 'x';
	 for (i=0; i<9; i++) posn[i]=posndef[i];
	 return;
 
}

//This will update the board with the 'X' or 'O'
void ticTac::updateBoard()
{
	 cout << "\n\n" <<endl;
	 cout << "\n\n\t\t" <<posn [0]<< "   | " <<posn [1]<< "  | " <<posn [2]<<endl;
	 cout << " \t\t	|	|   " <<endl;
	 cout << " \t\t ___|____|___ " <<endl;
	 cout << "\n\n\t\t" <<posn [3]<< "   | " <<posn [4]<< "  | " <<posn [5]<<endl;
	 cout << " \t\t	|	|   " <<endl;
	 cout << " \t\t ___|____|___ " <<endl;
	 cout << "\n\n\t\t" <<posn [6]<< "   | " <<posn [7]<< "  | " <<posn [8]<<endl;
	 cout << " \t\t	|	|   " <<endl;
	 cout << " \t\t	|	|   " <<endl;
	 cout << "\n" <<endl;
	  
}

//Starts the game
void ticTac::startGame()
{
	 int spot;
	 char blank = ' ';
	 bool result = false;
	 char posndef[9] = {'0', '1', '2', '3', '4', '5', '6', '7', '8'};
	  
	 cout << "\n\nPlayer " << getPlayer() <<" will be the first to play." <<endl;
 
	 do {
		 updateBoard();			  // display update board   
 
		 cout << "Player " << getPlayer() <<" choose a position between (0 - 8)." <<endl;
		 cin >> spot;
		  
		 if (spot < 0 || spot > 8)
		 {
			  cout << "Invalid position! Please input a number of 0 thru 8." <<endl;
			  continue;}
			  
		  
								
		 result = checkPosn(spot, getPlayer());			// to check if the position is taken
					  
		 system("CLS"); 
		 
		 if (result == false){
			 nextPlayer(); }
		  
		 if(checkWinner() == 'x' || checkWinner() == 'o' || checkWinner () == 't')
			 blank = 'v';
		 }
		 while ( blank != 'v' );

 
		 return;
 
}

//Checks to see who the winner will be	 
char ticTac::checkWinner()
{
	 char game = ' ';
	 int check = 1;
 
	 // Check if X wins   
	 if (posn[0] == 'x' && posn[1] == 'x' && posn[2] == 'x') game = 'x';
	 if (posn[3] == 'x' && posn[4] == 'x' && posn[5] == 'x') game = 'x';
	 if (posn[6] == 'x' && posn[7] == 'x' && posn[8] == 'x') game = 'x';
	 if (posn[0] == 'x' && posn[3] == 'x' && posn[6] == 'x') game = 'x';
	 if (posn[1] == 'x' && posn[4] == 'x' && posn[7] == 'x') game = 'x';
	 if (posn[2] == 'x' && posn[5] == 'x' && posn[8] == 'x') game = 'x';
	 if (posn[0] == 'x' && posn[4] == 'x' && posn[8] == 'x') game = 'x';
	 if (posn[2] == 'x' && posn[4] == 'x' && posn[6] == 'x') game = 'x';
	 if (game == 'x' )  
	   {cout << "Player1 wins the game." <<endl; return game; };
 
	 // Check if O wins	 
	 if (posn[0] == 'o' && posn[1] == 'o' && posn[2] == 'o') game = 'o';
	 if (posn[3] == 'o' && posn[4] == 'o' && posn[5] == 'o') game = 'o';
	 if (posn[6] == 'o' && posn[7] == 'o' && posn[8] == 'o') game = 'o';
	 if (posn[0] == 'o' && posn[3] == 'o' && posn[6] == 'o') game = 'o';
	 if (posn[1] == 'o' && posn[4] == 'o' && posn[7] == 'o') game = 'o';
	 if (posn[2] == 'o' && posn[5] == 'o' && posn[8] == 'o') game = 'o';
	 if (posn[0] == 'o' && posn[4] == 'o' && posn[8] == 'o') game = 'o';
	 if (posn[2] == 'o' && posn[4] == 'o' && posn[6] == 'o') game = 'o';
	 if (game == 'o' )
	   {cout << "Player2 wins the game." <<endl; return game; };
	 
	 for(int i = 0; i < 8; i++ ){
		 if(posn[i] >= '1' && posn[i] <= '8' ){
			 check = 0;
			 break;
			} 
	  } 
			  
	 // Check if tie
	 if (game != 'x' && game != 'o' && check == 1) game = 't';
	 if (game == 't')
		{cout << "Tie Game." <<endl; return game; };
	  

		  
	 
	 
	 
	 return game;		
}

//Checks to see if the position is taken	 
bool ticTac::checkPosn(int spot, char player)
{
 
	 if (posn[spot] == 'x' || posn[spot] == 'o'){
		  cout << "Position already taken, please choose another position." <<endl;
		  system ("pause");
		  return true;
		  }
		  else {
					posn[spot] = player;
					cout << "Nice move." <<endl;
					system ("pause");
					counter++;
					return false;
				   
		  }
 
	 
 
	 return false;
 
}

//Tells the next player that it's thier turn
void ticTac::nextPlayer()
{
	 if (player == 'x')
		 player = 'o';
	 else player = 'x';
	 return;
}

//This switches between players		 
char ticTac::getPlayer()
{
	 return player;
}





Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2