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.
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.
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)
returnfalse; // board is not empty
}
}
returntrue; // 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?
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.
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
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