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 =)

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.

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 :)

Thank you guys!!

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...

is that example in C or C++?

what is "scanf"?

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.

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: "<<endl;
cin>> ?? (This part i don't know, what are the % for?, &x and &y are pointers right?)

i don't know C.

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

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"<<endl;
if (y<=3)
cout<<"invalid move"<<endl;

cout << "Enter coordinates for your X: ";
cin >> x >>y:

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.

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

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.

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.

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".

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;
}

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

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

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.

asquella or anyone,
can you explain me what end = x.winner(); would do?

by reference do u mean something like:
void TicTacToe:: get_player1_move(int &, int &)
{ //something
}

????

BountyX
i will like to take a look at your tictactoe program.
thank you

As far as I can see, you have an int winner() function in TicTacToe class, which returns 1 if the first player has won, 2 if second player has won, 0 if nobody won yet.

end = x.winner() will invoke that winner() function, and assign its return value to end variable, which is later compared to 0 in the loop condition.

Yup, I meant

void TicTacToe:: get_player1_move(int &x1, int &x2) {
//instead of:
//void TicTacToe:: get_player1_move(int x1, int x2) {

Thank you asquella =)

Looki,Looki

I havent done a c++ tic-tac-toe yet,so I can only give some ideas.

Game Screen
[1][2][3]
[4][5][6]
[7][8][9]

You said it was 3x3 array.Dumping the user's need to enter a x,y loc would be a good idea.Just let them enter the single numbers like above and calculate the x,y loc internaly like:

int loc(int &x,int &y)
{
      int num,temp;

      if(num > 0 && num <10)
      {
         if(num<= 3)
         {
            x=0;
         }
        else if(num <= 6)
        {
             x=1;
        }
        else 
        {
            x=2;
        }

       if(num==1||num==4||num==7)y=0;
       else if(num==2||num==5||num==8)y=1;
       else if(num==3||num==6||num==9)y=2;
     return(0)
    }
    else
    {
         return(-1);
    }
}

ok a good bit of code but you should be able to put it into the code you have without major changes and you get an x and y coordinate without having to bother the users.It return -1 an in valid move is made and 0 if correct;

Call this funtion where you let the user enter the x,y and assign the values accordingly.

void TicTacToe:: get_player1_move(int &, int &)
{ //something
}

& is the refference operator which means it will take the memory address of the variable you pass to it instead of copying it.So any changes you make to a refferenced variable would change the value of the orginal variable(i.e the varible you use to call the funtion with).See below if you still have not got it.

void norm_fun(int x)
{
     x+=10;
        cout<<"\nn X="<<x;
}

void ref_fun(int &x)
{
   x+=10;
   cout<<"\nr X="<<x;
}
   

void main()
{
   int a=10;
   cout<<"\nA ="<<a;
   norm_fun(a);
   cout<<"\nA ="<<a;
   ref_fun(a);
   cout<<"\nA ="<<a;
   getch();
}

Run it and see the difference you will understand
I see you are displaying the board with cout.If you have a good compiler you could try mode 13 graphics(I have Borland C++ 5.02).It quite easy and fun once you get over the basics.[Complie with DOS platform and compact or huge memory mode].

Check out my site www.xlock.cjb.net. I just put it up but there is a mode 13 snake game there.It uses linked lists and stacks but you should be able to follow the code.[check it on 8th may 2004 if you want the full code]

Hi guys, :?:
I made some little changes to my code, but still not running. It comes out with 2 errors. It says:
error C2511: 'TicTacToe::TicTacToe' : overloaded member function 'void (int,int,int,int)' not found in 'TicTacToe'
C:\Program Files\Microsoft Visual Studio\MyProjects\t\1.cpp(5) : see declaration of 'TicTacToe'
error C2447: missing function header (old-style formal list?)
Error executing cl.exe.
What does that mean?

# include <iostream.h>

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

cout<<endl<<endl;
cout<<"Enter your coordinates player1: ";
cin>>x1>>y1;
 get_player1_move();
cout<<"\nEnter your coordinates player2: ";
cin>>x2<<y2;
 get_player2_move();
cout<<endl;

}

void TicTacToe:: get_player1_move(int &x1, int &y1)
{
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;
 
}
void TicTacToe::get_player2_move(int &x2, int &y2)
{
x2--;
while(true) {
  if ((x2>=3) || (x2<0))
	cout<<endl<<"Invalid move. Try again with a number from 1 to 3: ";
  else break;}
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;

}
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 {
  
  end=x.winner();
 }
 while(end==0);
 if(end==1)
 {cout<<"player1 wins\n";}
 else
 {cout<<"player2 wins\n";}

 
return 0; 
}

Why did you do that to your program??!
Your previous variant with

do {
  x.disp_array();
  x.get_player1_move();
  x.get_player2_move();
  x.winner();
 }

loop and TicTacToe constructor doing nothing but initialization seemed much better to me.

To heal your problem, you'll have to implement the constructor function with the same parameters you defined it (ie. with no parameters):

//constructor
TicTacToe::TicTacToe()
{
for (int i=0; i<3; i++) //loop array
for (int j=0; j<3; j++)
array[i][j]=0;

It is definitely a bad idea to have all the playing code in constructor, but anyways the get_move etc. should be in the loop as they were before.

i tried it that way, but it came out with more error messages, that's why i change it to this one, 'cause this way it show less error messages.

i tried it that way, but it came out with more error messages, that's why i change it to this one, 'cause this way it show less error messages.

Post some error messages will you,what you said is just like 'if you dont know why I am angry with you then I am not gona tell you'

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.