954,135 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Tic-Tac-Toe

Hey guys,
I'm new here. And I was wondering if you could help me with a question i have. You see i have to write a C++ program that plays the game of tic-tac-toe. And the class contains as private data a 3-by-3 double subscripted array of integers. And i have to allow 2 players to use it. You see my problem is that i don't know how to make the 2 players access the array. Maybe you give me an example or some kind of advice so i have a better idea on how to do it.
Hey,
Do you know any good books that teach C++ for beginners?

Please, help me =)

cybergirl
Light Poster
40 posts since Apr 2004
Reputation Points: 15
Solved Threads: 0
 

Try the C++ primer plus ---by Stephen Prata.
It's ideal for beginners.
It's not me who says it. Look at the reviews in amazon.com on this book. It's really quite a hit.

Asif_NSU
Posting Whiz
353 posts since Apr 2004
Reputation Points: 113
Solved Threads: 3
 

well im not sure how to help your problem but for recommending a book. i always recommend O'Reilly books, they seem to have some of the best books out there for programming and computers in general O'Reilly's Website and then here is the link to there c/c++ section http://cprog.oreilly.com/
good luck on getting your problem fixed im sure someone here will beable to help out shortly :)

big_k105
PFO Founder
Team Colleague
357 posts since May 2003
Reputation Points: 36
Solved Threads: 2
 

Thank you guys!!

cybergirl
Light Poster
40 posts since Apr 2004
Reputation Points: 15
Solved Threads: 0
 

Here is an example of Tic-Tac-Toe.

But here u play against the computer.:sad:
See if it is of any help. It's an exapmle taken from herbert Schield's book on turbo C/C++--the complete reference.
Take care...

Attachments TIC.C (2.04KB)
Asif_NSU
Posting Whiz
353 posts since Apr 2004
Reputation Points: 113
Solved Threads: 3
 

is that example in C or C++?

cybergirl
Light Poster
40 posts since Apr 2004
Reputation Points: 15
Solved Threads: 0
 

what is "scanf"?

cybergirl
Light Poster
40 posts since Apr 2004
Reputation Points: 15
Solved Threads: 0
 

the example is for C. But it will work for C++ as well.
Consider scanf() as equivalent of cin. scanf is used in C. C doesnt support cin. So u have to depend on scanf(). But not to worry; bcoz c++ can handle scanf() as well.

Asif_NSU
Posting Whiz
353 posts since Apr 2004
Reputation Points: 113
Solved Threads: 3
 

Thank you,
if you were using cin, how would you write

int x,y;
printf("Enter coordinates for your X: ");
scanf("%d%d",&x,&y);

I think
printf is cout right?
so this will look in C++ like:

int x,y;
cout<<"Enter coordinates for your X: "<> ?? (This part i don't know, what are the % for?, &x and &y are pointers right?)

i don't know C.

cybergirl
Light Poster
40 posts since Apr 2004
Reputation Points: 15
Solved Threads: 0
 

Hello,

I do not claim to be an expert on C++.

scanf(%d%d,&x,&y) means it is looking for decimal integer. So, %d%d means "here come 2 decimal integers, and they are called variables x and y" The & symbol is the address operator of the variable. C is more cryptic than C++, but I think you will like it better than FORTRAN.

I think you can write the code:

// Let's declare and initialize the values
int x = 0, y = 0;

// User interaction to gather X and Y
// Put me in a loop though for error checking
cout << "Enter coordinates for your X: ";
cin >> x >> y;


If I were you, I would put these in loops to ensure error checking... what happens if someone tried to enter in -3, 7? Your code would probably fail.

Enjoy.

Christian

kc0arf
Posting Virtuoso
Team Colleague
1,937 posts since Mar 2004
Reputation Points: 121
Solved Threads: 57
 

Thank you Christian.

If i'm writing a program (C++) in which i'm using an array[x][y] and its a 3 by 3 array
How do i loop for error checking?
can i do it like this?

int x = 0, y = 0;

if (x<=3)
cout<<"invalid move"<> x >>y:

cybergirl
Light Poster
40 posts since Apr 2004
Reputation Points: 15
Solved Threads: 0
 

Perhaps,

int x,y;

cout << "Enter coordinates for your X: ";
while(true) {
  cin >> x;
  if ((x>=3) || (x<0))
    cout<<endl<<"invalid move. try again with a number from 0 to 2: ";
  else break;
}

and then the same for y.

asqueella
Newbie Poster
9 posts since Apr 2004
Reputation Points: 10
Solved Threads: 0
 

Hi,

I like your answer asqueella. Thank you for the hint there. As she is just beginning C++, she might not know what the code means. I'd break it down for her to help her understand where you are going with it.

// This declares the integers.  Don't need to initialize them to 
// zero, because we fill them right away, and no decisions are made on them.
int x,y;


cout << "Enter coordinates for your X: ";
// note that since you are missing a \n, there will be no "return"

// while (true) means "loop forever unless we break out of it"
while (true)
     {
     cin >> x;
     
     // check to see if (X is greater or equal to 3)  OR  "||"  (X is less than 0)
     // this is the error check code.  The lines || mean OR

     cout << endl .......

     // the else "break" command means if you get here, break out of the while loop

     }

Hope that helps you out with the comments on the code. I also hope I didn't insult you by telling things you already knew.

Christian

kc0arf
Posting Virtuoso
Team Colleague
1,937 posts since Mar 2004
Reputation Points: 121
Solved Threads: 57
 

Yes there is no need to initialize x, y to 0 for this particular program, bcoz we are eventually filling them up. But I would still suggest u initialize them to zero/something. It's a good programming practice, specially when u will be writing much bigger programs. For example u might have to move part of codes in or out of a loop. It is very difficult to carefully notice if all the variables are initialized /filled up or not. If u miss any of them it may cause unexpected problems. Therefore, I suggest u practice the habit of initializing all the varibles from now on-----I believe it will come in handy later on.

Asif_NSU
Posting Whiz
353 posts since Apr 2004
Reputation Points: 113
Solved Threads: 3
 

agreed, it is always a good habit to get into. the only time u will see good programmers avoiding the practice is when writing kernel code - when every instruction counts, and then it is ur responsibility to be absolutely positive to double check that every variable is not used before given a value.

infamous
Junior Poster in Training
77 posts since Mar 2004
Reputation Points: 47
Solved Threads: 2
 

Yea, that's bcoz kernel codes need to be efficient and as fast as possible; every unnecessary statement needs to be avoided for that purpose. Thanx for reminding me that "infamous".

Asif_NSU
Posting Whiz
353 posts since Apr 2004
Reputation Points: 113
Solved Threads: 3
 

Thank you guys!!:)

Well, here it is what i have so far for my TicTacToe game(C++). It not running, I don't know what's wrong with it. Take a look at it. Maybe you can help me.
Remember i had to create a class TicTacToe that will enable me to write a complete program to play the game tictactoe. The class has to contain as private data a 3-by-3 doublesubscripted array of integers. The constructor should initialize the empty board to all Zeros. i had to allow 2 players to used it. Wherever the player1 moves, i have to place a 1 in the specified square. For player2, i have to place a 2.

# include <iostream.h>
class TicTacToe
{
public:
 TicTacToe::TicTacToe(int array[][3]);//constructor
 void disp_array(void);
 void get_player1_move(int x1, int y1);
 void get_player2_move(int x2, int y2);
 int winner();
private:
int array[3][3];
};
//constructor
 TicTacToe::TicTacToe(int array[][3])
{
for (int i=0; i<3; i++) //loop array
for (int j=0; j<3; j++)
array[i][j]=0;
}
void TicTacToe::disp_array(void) //display board game
{
 cout<<"Tic-Tac-Toe Game"<<endl<<endl;
 array[3][3]=0;
for (int i=0; i<3; i++)
{
cout<<array[i][0]<<" | "<<array[i][1]<<" | "<<array[i][2];
if (i!=2)
{cout<<"\n---------\n";}
}
cout<<endl<<endl;
}

void TicTacToe:: get_player1_move(int x1, int y1)
{cout<<"Enter your coordinates player1: ";

cin>>x1;
x1--;
while(true) {
  if ((x1>=3) || (x1<0))
	cout<<endl<<"Invalid move. Try again with a number from 1 to 3: ";
  else break;}
cin>>y1;
y1--;
while(true) {
  if ((y1>=3) || (y1<0))
	cout<<endl<<"Invalid move. Try again with a number from 1 to 3: ";
  else break;}
array[x1][y1]=1;
get_player1_move();

}
void TicTacToe::get_player2_move(int x2, int y2)
{
cout<<"\nEnter your coordinates player2: ";
cin>>x2;
x2--;
while(true) {
  if ((x2>=3) || (x2<0))
	cout<<endl<<"Invalid move. Try again with a number from 1 to 3: ";
  else break;}
cin>>y2;
y2--;
while(true) {
  if ((y2>=3) || (y2<0))
	cout<<endl<<"Invalid move. Try again with a number from 1 to 3: ";
  else break;}
array[x2][y2]=2;
get_player2_move();
}
int TicTacToe:: winner(void); //see if there is a winner
{
int *p;
for (int t=0; t<3; t++) //rows
{p=&array[t][0];
if (*p==*(p+1) && *(p=1)==*(p+2))
return *p;
}
for(t=0; t<3; t++)//columns
{p=&array[0][t];
if (*p==*(p+3)  &&  *(p+3)==*(p+6))
return *p;
}
if(array[0][0]==array[1][1] && array[1][1]==array[2][2])//diagonal
return array[0][0];
if(array[0][2]==array[1][1] && array[1][1]==array[2][0])
return array[0][2];
}
 
int main()
{ int end;
 TicTacToe x;
 do {
  x.disp_array();
  x.get_player1_move();
  x.get_player2_move();
  x.winner();
 }
 while(end==0);
 if(end==1)
 {cout<<"player1 wins\n";}
 else
 {cout<<"player2 wins\n";}

 
return 0;
}
cybergirl
Light Poster
40 posts since Apr 2004
Reputation Points: 15
Solved Threads: 0
 

I also made a tic tac toe program in C++, if your intrested in my code feel free to email me :)

BountyX
Posting Whiz in Training
230 posts since Mar 2004
Reputation Points: 28
Solved Threads: 9
 

cybergirl << could u at least tell us WHY it isn't running?? can u compile it? does it fault when it runs? where does it fault?? some INFO

infamous
Junior Poster in Training
77 posts since Mar 2004
Reputation Points: 47
Solved Threads: 2
 

I didn't try to run it, or even compile, but the most obvious issues for me are: do..while loop in main() doesn't assign any value to end. Perhaps it should have this line:

end = x.winner();
Your TicTacToe class doesn't have any default constructor (TicTacToe::TicTacToe()), only quite a strange TicTacToe::TicTacToe(int array[][3]). What I would do is write: class TicTacToe { public: TicTacToe(); // constructor <snip> }; <snip> //constructor TicTacToe::TicTacToe() { for (int i=0; i<3; i++) //loop array for (int j=0; j<3; j++) array[i][j]=0; } Also the get_player1_move() functions have their parameters passed "by value", which means that any changes you make to x1 or y1 inside the function will not affect the variable in calling code - you should to use references or pointers here instead. The same applies to get_player2_move() - I'd merge them into one.
asqueella
Newbie Poster
9 posts since Apr 2004
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You