10 Replies - 18017 Views - Last Post: 15 November 2007 - 06:11 PM Rate Topic: -----

#1 alisyed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-September 07

Tic tac toe with classes

Post icon  Posted 25 October 2007 - 11:21 PM

hi, i have an upcoming project which requires to make tic tac toe game using classes... im kinda lost ... can you tell me what my object in this class would be, my guess is that it would be the board but how would i use the boad as an object to play the game???
Is This A Good Question/Topic? 0
  • +

Replies To: Tic tac toe with classes

#2 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Tic tac toe with classes

Posted 26 October 2007 - 12:16 AM

I think that you may be a little unclear about what classes and objects are.

An object is an instance of a class. It is not something that is contained within a class. A class describes the data members that are to be held by objects instantiated from a class, and the methods required for manipulating that data. Just like there is no such thing as an int in a program, only variables of the int data type, you can't have a "class" that exists in a program and does anything - it's the objects, the instantiations, that can hold data and be manipulated.

As for your actual assignment, the entire game can be a class unto itself. The class could describe data members for storing the state of the board, and member functions for deciding what moves are valid, which move a computer opponent will take, methods for updating the state of the board, display methods, etc. And some sort of "play" method that controls the entire flow of the game and the sequence of other methods that are called.

Hope that helps,

-jjh
Was This Post Helpful? 0
  • +
  • -

#3 alisyed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-September 07

Re: Tic tac toe with classes

Posted 26 October 2007 - 12:43 AM

so ok i think i get what you mean how objects are not actually in the class but they use the data in a class(ie member functions) to operate... so i guess my first question was kinda stupid ... but im still confused on how to access the data in the .h and .cpp files into my main.cpp where the flow of the game would be.
Was This Post Helpful? 0
  • +
  • -

#4 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Tic tac toe with classes

Posted 26 October 2007 - 01:09 AM

not quite...

Objects contain their own data and methods. Each object contains (potentially) unique data, so the object posesses the data all to itself - just like for the initialization int x=10, the variable x can be thought of as having ownership of the data (i.e. the number 10). The methods are the basically the same for any object of a given class, so in some senses they can be thought of as being accessed from the class, by the object, whenever they are needed. Similar to the above analogy, the operators + and - are defined for the int data type, and the variable x doesn't need to access a method that it has all to itself - it just performs the addition or subtraction as any other integer data type would.

You should probably look over the object-oriented programming tutorial written by born2code - the general descriptions of classes, objects, etc. should provide you with some more info.


As for your question, you don't really access the "data" in the header and source files - you create an object in your main routine, and then call the objects methods to proceed with the game.

The following could be the entirety of the program you have to write, except for the tic-tac-toe class itself:
#include "TicTacToe.h"

int main() {
	TicTacToe gameObject;
	gameObject.playGame();
	return 0;
}


The program instantiates an object of the TicTacToe class, and then calls the method playGame(). It would be up to you to define the class itself, including the method playGame(), which contains all of the code to actually play a game, and the various other methods you'll need to define, such as how a computerized opponent will choose appropriate moves, and whether a given move (by the computer or the player) is valid. In your class, you will also likely define an array to hold the state of the game board (each cell being unoccupied/X/O).

I would imagine that it's not going to be all that helpful, but there is a current thread in the Java forum on a tic-tac-toe game that you might look at for some ideas: http://www.dreaminco...wtopic36060.htm Much of the syntax of java is similar to c++, so you may be able to understand quite a bit of it. The only part that will be really confusing is that in java, everything is a class - even the main() must be defined in a class. But the other methods may be helpful in providing an idea of some of the methods you are probably going to need to implement your C++ version.

Hope that helps,

-jjh

This post has been edited by jjhaag: 26 October 2007 - 01:09 AM

Was This Post Helpful? 0
  • +
  • -

#5 alisyed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-September 07

Re: Tic tac toe with classes

Posted 10 November 2007 - 05:42 PM

hi.... sorry my code is a bit of a mess ... i did it in a hurry .... could you please tell where where exactly am i wrong ... i know there might be a problem in my looping ...

/*You must represent the TicTacToe board with a class, which is split into a .h and .cpp file. The
TicTacToe board class can do the following things (at minimum):
store a 3-by-3 grid, which contains the status of each grid position (blank, X or O
nothing else can be stored on the grid)
print the board
put an X or O on a specified grid position
determine if there is a winner (3-in-a-row of X or O), and determine which player won
determine if the board is full
A more sophisticated version of the class will
determine if a player's move is valid (that is, is the player trying to put their X or O in a
blank position?)
If you want to, you can use classes for other aspects of the program; however, the TicTacToe board
class is the only required class.
The flow of game should be in a seperate file.
*/


//board.h
#ifndef BOARD_H
#define BOARD_H

class Board
{public:
	Board ( char newboard[3][3]);
	char getboard();
	void printboard();
	void printXmove(char newboard[3][3],int row, int column);
	void printOmove(char newboard[3][3],int row, int column);
	bool fullboard(char newboard[3][3]);
	bool playerwins(char newboard[3][3]);
	bool validmove(char newboard[3][3],int row, int column);
private:
	char mboard[3][3];
	

};
#endif 





//board.cpp
#include "board.h"
#include <iostream>
using namespace std;

Board::Board(char newboard[][3])
{
	mboard[][3] = newboard[][3];
}

char Board::getboard()
{
	return mboard[][3];
}

bool Board::validmove(char newboard[3][3],int row, int column)
{	
	if ( row>=3 && row<=1 && column>=3 && column<=1 && board[row-1][column-1] =! " ")
	return false;
return true;
}

void Board::printXmove(char newboard , int row, int column )
{
	board[row-1][column-1]= 'X';
}

void Board::printOmove(char newboard , int row, int column )
{
	board[row-1][column-1]= 'O';  
}

bool Board :: fullboard(char newboard[][3])
{   for (int row=0; row<3;row++ )
	  {for(int column =0; column<3;column++)
	  if(board[row][column]==' ') 
	  }
	  return false;
  return true;
}

bool Board :: playerwins(char newboard [][3])
{ 
	if (board[0][0]== board[0][1]==board [0][2] || board [0][0]== board [1][1]== board [2][2] || board [0][2]== board [1][1]==board [2][0] )
		return true;
	return false;
}

void Board::printboard()
{

char board[3][3]={0};
cout<<"\n   1   2   3\n";
cout<<"1 "<<board[0][0]<<" | "<<board[0][1]<<" | "<<board[0][2]<<"\n";
cout<<   " ---|---|---\n";
cout<<"2 "<<board[1][0]<<" | "<<board[1][1]<<" | "<<board[1][2]<<"\n";
cout<<   " ---|---|---\n";
cout<<"3 "<<board[2][0]<<" | "<<board[2][1]<<" | "<<board[2][2];

}

//playboard is a member variable





//playgame.cpp
#include "board.cpp"
#include <iostream>
using namespace std;

void main()
{
	
	char playagain = '';
	
	while(playagain == 'Y'|| playagian =='y')
		{	Board playboard (board[][3]);
			playboard.printboard();
			int row=0,column=0; 
		while(playboard.fullboard == false)
			{
				cout<<"Player X's turn, please enter column and row\n";
					while(true)
					{
						cout<<"row";
						cin>>row;
						cout<<"column";
						cin>>column;
						playboard.validmove(board[][3], row, column);
						cout<<"invalid move";
					}
					else 
						break;
				playboard.printXmove(board[][3],row,column);
				if(playboard.playerwins(board[][3])== true)
				cout<<"player X wins";
		cout<<"would you like to play again?";
		cin>>playagain;
			else
				cout<<"player O's turn, please enter row and column\n";
				while(true)
					{
						cout<<"row";
						cin>>row;
						cout<<"column";
						cin>>column;
						playboard.validmove(board[][3], row, column);
						cout<<"invalid move";
					}
					else 
						break;
				playboard.printOmove(board[][3],row,column);
				if(playboard.playerwins(board[][3])== true)
				cout<<"player O wins";
		cout<<"would you like to play again?";
		cin>>playagain;
		
			}			
		cout<<"Tie game";
		cout<<"would you like to play again (Y/N)\n";
		cin>>playagain;
		}		
}






Was This Post Helpful? 0
  • +
  • -

#6 alisyed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-September 07

Re: Tic tac toe with classes

Posted 12 November 2007 - 12:02 AM

can someone please help!
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5820
  • View blog
  • Posts: 12,672
  • Joined: 16-October 07

Re: Tic tac toe with classes

Posted 12 November 2007 - 06:33 AM

Board ( char newboard[3][3]);
void printXmove(char newboard[3][3],int row, int column);
void printOmove(char newboard[3][3],int row, int column);



I'm at a bit of a loss starting here. You keep on passing newboard[3][3] around, but you don't use it in anything other than the constructor. Why pass it, it makes the code confusing.

The printXmove and friends is somewhat unclear. Looks like setValue(int row, int column, char player); would make more sense.

I know, this isn't what's broken. However, by removing all the junk you're not using. Taking away everything you possibly can. It will highlight where your problems lie.

For specifics, your playerwins only checks three paths of success. There are eight. Also, it doesn't mark the difference between a player and an empty cell. In essence, you need to check each path, and that they're not empty.

You'd have an easier time, and more efficient code, if you just used int values for your grid cells.

In C++ I'd use and enum or constants for the players. C style, this is:
static const int CELL_EMPTY = 0;
static const int CELL_X = 1;
static const int CELL_Y = 2;



You also see #define used, but values that can be referenced are more in keeping with the OO style.

Hope this helps.

This post has been edited by baavgai: 12 November 2007 - 06:33 AM

Was This Post Helpful? 0
  • +
  • -

#8 alisyed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-September 07

Re: Tic tac toe with classes

Posted 12 November 2007 - 04:12 PM

" You keep on passing newboard[3][3] around"

dont i need the board as input to function, so in the playgame.cpp file the fuction would know what to work with... im confused..

i fixed the playerwins function:
   bool Board :: playerwins(char newboard [][3])
{	mboard[3][3] = newboard[3][3];
	for(int row=0; row<3;row++)
	{for(int column =0; column <3; column++)
		if (mboard[row][column]!=' ')	
		{if (mboard[0][0]== mboard[0][1]==mboard [0][2] || mboard [0][0]== mboard [1][1]== mboard [2][2] || mboard [0][2]== mboard [1][1]== mboard [2][0]|| mboard[0][0]== mboard[1][0]==mboard[2][0]||mboard[2][0]== mboard[2][1]==mboard[2][2] || mboard[0][2]==mboard[1][2]==mboard[2][2]|| mboard[0][1]==mboard[1][1]==mboard[2][1]||mboard[1][0]==mboard[1][1]==mboard[1][2])
		return true;
	return false;
		}
	}
	
} 


and the setValue fuction looks like this:

void Board::setValue(char newboard[][3], int row, int column, char player)
{	
	mboard[3][3] = newboard[3][3];
	mboard[row-1][column-1]= player;

}



playgame.cpp looks like this now

#include "board.cpp"
#include <iostream>
using namespace std;

void main()
{
	char playagain = 'Y';
	while(playagain == 'Y'|| playagain =='y')
		{	
			Board playboard(board[3][3]);
			playboard.printboard();
			char playerX = 'X';
			char playerO = 'O';
			int row=0,column=0; 
		while(playboard.fullboard(board[3][3])== false)
			{
				cout<<"Player X's turn, please enter column and row\n";
					while(true)
					{
						cout<<"row";
						cin>>row;
						cout<<"column";
						cin>>column;
						playboard.validmove(board[3][3], row, column);
						cout<<"invalid move";
					}
					break;
				playboard.setValue(board[3][3],row,column,playerX );
				if(playboard.playerwins(board[3][3])== true)
				cout<<"player X wins";
			break;
		cout<<"would you like to play again?";
		cin>>playagain;
				cout<<"player O's turn, please enter row and column\n";
				while(true)
					{
						cout<<"row";
						cin>>row;
						cout<<"column";
						cin>>column;
						playboard.validmove(board[3][3], row, column);
						cout<<"invalid move";
					}
					break;
				playboard.setValue(board[3][3],row,column,playerO);
				if(playboard.playerwins(board[3][3])== true)
				cout<<"player O wins";
		cout<<"would you like to play again?";
		cin>>playagain;
		
			}
			break;
		cout<<"Tie game";
		cout<<"would you like to play again (Y/N)\n";
		cin>>playagain;
		}		
}



Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5820
  • View blog
  • Posts: 12,672
  • Joined: 16-October 07

Re: Tic tac toe with classes

Posted 12 November 2007 - 05:50 PM

View Postalisyed, on 12 Nov, 2007 - 06:12 PM, said:

" You keep on passing newboard[3][3] around"

dont i need the board as input to function, so in the playgame.cpp file the fuction would know what to work with... im confused..


I'm confused that you're confused. :P

You defined the following class:
class Board{
public:
	Board ( char newboard[3][3]);
	char getboard();
	void printboard();
	void printXmove(char newboard[3][3],int row, int column);
	void printOmove(char newboard[3][3],int row, int column);
	bool fullboard(char newboard[3][3]);
	bool playerwins(char newboard[3][3]);
	bool validmove(char newboard[3][3],int row, int column);
private:
	char mboard[3][3];
};


What does class Board represent? Presumably your tic tac toe board. It is the board, not this "newboard[3][3]" all over the place. How does printboard() work? It uses the privately defined array mboard[3][3]. Look at how you're passing newboard[3][3] and then referencing it again as mboard. Why?

You could write this and be fine:

class Board{
public:
	Board ();
	char getboard();
	void printboard();
	void setValue(int row, int column, char player);
	bool fullboard();
	bool playerwins();
	bool validmove(int row, int column);
private:
	char mboard[3][3];
};


In playerwins, what are you looping for? Also, you still don't allow for blanks.

Wait, I see it! The loop filters the blanks with mboard[row][column]!=' '. No, it doesn't. On the very first entry it exits the loop and your filter logic is ignored!

Try this. I don't know if the syntax will work for sure, but it should give you and idea.

bool Board :: playerwins() {
   return 
		mboard[0][0]==mboard[0][1]==mboard[0][2]!=' '
	  ||mboard[1][0]==mboard[1][1]==mboard[1][2]!=' '
	  ||mboard[2][0]==mboard[2][1]==mboard[2][2]!=' '
	  ||mboard[0][0]==mboard[1][0]==mboard[2][0]!=' '
	  ||mboard[0][1]==mboard[1][1]==mboard[2][1]!=' '
	  ||mboard[0][2]==mboard[1][2]==mboard[2][2]!=' '
	  ||mboard[0][0]==mboard[1][1]==mboard[2][2]!=' '
	  ||mboard[0][2]==mboard[1][1]==mboard[2][0]!=' '
  ;
}


Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#10 alisyed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-September 07

Re: Tic tac toe with classes

Posted 12 November 2007 - 11:18 PM

thanks for the help .... its reallyyy apreciated ....
i have a few more quetions though ;) ...

 Board playboard(board[3][3]);
... the compiler says board is undefined ...

and i made a few changes in my playgame.cpp..... but my looping is still messed up ... a little help on that would also be greatly appreaciated ... thanks

#include "board.cpp"
#include <iostream>
using namespace std;

void getvalidinput(int& row, int& column)
{
	Board playboard(board[3][3]);
while(true)
  {
	cout<<"row";
	cin>>row;
	cout<<"column";
	cin>>column;
	playboard.invalidmove(row, column);
	cout<<"invalid move";
  }
	break;
}


void main()
{
	char playagain = 'Y';
	while(playagain == 'Y'|| playagain =='y')
		{	
			Board playboard(board[3][3]);
			playboard.printboard();
			char playerX = 'X';
			char playerO = 'O';
			int row=0,column=0; 
		while(playboard.fullboard()== false)
			{
				cout<<"Player X's turn, please enter column and row\n";
				getvalidinput(row,column);	
				playboard.setValue(row,column,playerX );
				if(playboard.playerwins()== true)
				cout<<"player X wins";
			break;
		cout<<"would you like to play again?";
		cin>>playagain;
				cout<<"player O's turn, please enter row and column\n";
				getvalidinput(row,column);
				playboard.setValue(row,column,playerO);
				if(playboard.playerwins()== true)
				cout<<"player O wins";
		cout<<"would you like to play again?";
		cin>>playagain;
		
			}
			break;
		cout<<"Tie game";
		cout<<"would you like to play again (Y/N)\n";
		cin>>playagain;
		}		
}



Was This Post Helpful? 0
  • +
  • -

#11 cstewart128  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-November 07

Re: Tic tac toe with classes

Posted 15 November 2007 - 06:11 PM

View Postalisyed, on 10 Nov, 2007 - 05:42 PM, said:

hi.... sorry my code is a bit of a mess ... i did it in a hurry .... could you please tell where where exactly am i wrong ... i know there might be a problem in my looping ...

/*You must represent the TicTacToe board with a class, which is split into a .h and .cpp file. The
TicTacToe board class can do the following things (at minimum):
store a 3-by-3 grid, which contains the status of each grid position (blank, X or O
nothing else can be stored on the grid)
print the board
put an X or O on a specified grid position
determine if there is a winner (3-in-a-row of X or O), and determine which player won
determine if the board is full
A more sophisticated version of the class will
determine if a player's move is valid (that is, is the player trying to put their X or O in a
blank position?)
If you want to, you can use classes for other aspects of the program; however, the TicTacToe board
class is the only required class.
The flow of game should be in a seperate file.
*/

These requirements look alot similar to the one I have assigned in my class...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1