Tic Tac Toe Game

I'm doing the exercises here: http://www.cplusplus.com/forum/articles/12974/
I'm doing the Tic Tac Toe one and for now without the other points.

This is the code right now, but I'm stuck in the condition of the main while loop.
I don't know how to make him check when the Tic Tac Toe is full so that he stops the loop and exit.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>

using namespace std;

int main(int argc, char** argv) {
	const int N=3;
	int pos[N][N];
	
	//TicTacToe Map initialization.
	pos[0][0]=0; pos[0][1]=0; pos[0][2]=0;
	pos[1][0]=0; pos[1][1]=0; pos[1][2]=0;
	pos[2][0]=0; pos[2][1]=0; pos[2][2]=0;
	//Print Map
	for(int i=0;i<N;i++){
		for(int j=0;j<N;j++){
			cout<<pos[i][j];
			if(j==2){
				cout<<endl;
			}
		}
	}
	
	int c=0, l=0;
	while(){
			//Player 1
			cin>>c;
			cin>>l;
			while(pos[c][l]==1 || pos[c][l]==2){
				cout<<"This place is already marked! Try another place."<<endl;
				cin>>c;
				cin>>l;
			}
			pos[c][l]=1;
			for(int i1=0;i1<N;i1++){
				for(int j1=0;j1<N;j1++){
					cout<<pos[i1][j1];
					if(j1==2){
						cout<<endl;
					}
				}
			}
			
			//Player 2
			cin>>c;
			cin>>l;
			while(pos[c][l]==1 || pos[c][l]==2){
				cout<<"This place is already marked! Try another place."<<endl;
				cin>>c;
				cin>>l;
			}
			pos[c][l]=2;
			for(int i2=0;i2<N;i2++){
				for(int j2=0;j2<N;j2++){
					cout<<pos[i2][j2];
					if(j2==2){
						cout<<endl;
					}
				}
			}
		}
	
	return 0;
}
I don't know how to make him check when the Tic Tac Toe is full

Simply count the number of cells that are zero. If any are zero, the board is not yet full (tie).

You also need to check to see if either place has won after any move.

You should move your code to print the board into a function. You're repeating that code 3 times. Anytime you have repeated code, that's a good indication the code needs to be put into a function.

Also compare lines 26-33 and lines 44-51. The only difference is the player number on lines 33 and 51. Another good indication of code that belongs in a function.

Last edited on
Simply count the number of cells that are zero. If any are zero, the board is not yet full (tie).

What do you mean? Could you do an example?

You also need to check to see if either place has won after any move.

That was in the first point of the exercise. I was going to do it after I see it's possible to play, etc.

You should move your code to print the board into a function.

I put it in a function like this:
1
2
3
4
5
6
7
8
9
10
void PrintBoard(int pos[N][]){
	for(int i=0;i<N;i++){
		for(int j=0;j<N;j++){
			cout<<pos[i][j];
			if(j==2){
				cout<<endl;
			}
		}
	}
}


Also compare lines 26-33 and lines 44-51. The only difference is the player number on lines 33 and 51.

I put it in a function but how can I know who's the player?
1
2
3
4
5
6
7
8
9
10
11
12
void Player(){
	cout<<"Column: ";
	cin>>c;
	cout<<"Line: ";
	cin>>l;
	while(pos[c][l]==1 || pos[c][l]==2){
		cout<<"This place is already marked! Try another place."<<endl;
		cin>>c;
		cin>>l;
	}
	pos[c][l]=;
}
What do you mean? Could you do an example?

Really? You can't count the number of cells that are zero?

1
2
3
4
5
6
7
8
9
bool CheckTie (int pos[N][N])
{ for (int i=0;i<N;i++)
  { for(int j=0;j<N;j++)
    {  if (pos[i][j] == 0)
          return false;  // board is not empty
    }
  }
  return true;  // All cells are non-zero 
}


Note on your PrintBoard function, at line 5 you assume the number of columsn per row is 3. Since you have defined the dimension of the board as N, you should use N-1 here.

I put it in a function but how can I know who's the player?

Pass the player as an argument.
1
2
3
void Player (int player)
...
  pos[c][l] = player;




Oh sorry about that! I asked without thinking...

Note on your PrintBoard function, at line 5 you assume the number of columsn per row is 3. Since you have defined the dimension of the board as N, you should use N-1 here.

Why? I start from 0 so there's no problem with that because it stops at i<N(for example it stops at 2).
My point was that if you change N to be 5, your if statement at line 5 isn't going to work properly testing a hard coded value of 2 to determine when to output the endl.

Oh right! I adjusted it.

Anyway I have a problem now.
This is the code now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>

using namespace std;

const int N=3;

//Prototype Functions
bool CheckTie(int pos[][N]);
void PrintBoard(int pos[][N]);
void Player(int player, int pos[][N]);

int main(int argc, char** argv) {
	int pos[N][N];
	int player1=1, player2=2;
	
	//TicTacToe Board initialization.
	pos[0][0]=0; pos[0][1]=0; pos[0][2]=0;
	pos[1][0]=0; pos[1][1]=0; pos[1][2]=0;
	pos[2][0]=0; pos[2][1]=0; pos[2][2]=0;
	
	while(CheckTie(pos)!=true)
	{
		Player(player1, pos);
		PrintBoard(pos);
		Player(player2, pos);
		PrintBoard(pos);
	}
	return 0;
}

bool CheckTie(int pos[][N]){
	for(int i=0;i<N;i++){
		for(int j=0;j<N;j++){
			if(pos[i][j]==0)
          	return false;
    	    	}
  	}
  return true;
}

void PrintBoard(int pos[][N]){
	for(int i=0;i<N;i++){
		for(int j=0;j<N;j++){
			cout<<pos[i][j];
			if(j==N-1){
				cout<<endl;
			}
		}
	}
}

void Player(int player, int pos[][N]){
	int c=0, l=0;
	cout<<"Column: ";
	cin>>c;
	cout<<"Line: ";
	cin>>l;
	while(pos[c][l]==1 || pos[c][l]==2){
		cout<<"This place is already marked! Try another place."<<endl;
		cin>>c;
		cin>>l;
	}
	pos[c][l]=player;
}


While running the program, after the entire board is full, it still asks for a column and a line. I checked the "CheckTie" function and it works fine so I think the while loops work wrong
Lines 21-27: A tie can occur after either player moves. You don't check this after player 1 moves.

Actually with an odd number of cells, the first player will always make the last move.
Last edited on
Do I have to do a "do ... while" loop? Like this:
1
2
3
4
5
6
do{
	Player(player1, pos);
	PrintBoard(pos);
	Player(player2, pos);
	PrintBoard(pos);
}while(CheckTie(pos)!=true);

Anyway it doesn't check it after player 1 moves but after both players move.

Actually with an odd number of cells, the first player will always make the last move.


So that's why it keeps asking for a column and a line even though the board is full. Do I have to put the CheckTie function between Player 1 and Player 2?
So that's why it keeps asking for a column and a line even though the board is full. Do I have to put the CheckTie function between Player 1 and Player 2?

Yes.

Your do/while loop doesn;t change anything. You're still not checking after player one moves.


Okay! I did it! Thanks!
Now I'll try the other points of the exercise.
I'm sorry if bothered you with all stupid things. I started programming recently so I'm not so good at the moment. XD
Please mark this thread as solved if you have solved your issue.
:)
Topic archived. No new replies allowed.