'How does this CheckDraw method work for TicTacToe- C++
I have to determine how exactly the CheckDraw method works but after reviewing the code for hours, I still haven't figured out anything.
bool CheckDraw(bool GameOver)
{
int n = 1;
int i = 0;
int j = 0;
int counter = 0;
for( i = 0; i < 3; i++ ) {
for( j = 0; j < 3; j++ )
if(table[i][j] == '0'+n)
counter++;
n++;
}
if( counter < 1 )
{
cout << "It's a draw!\n\n";
GameOver = true;
}
return GameOver;
}
Solution 1:[1]
I think I've found out how it works...So there's this method in the code where if the player enters a number, that number is replaced with the character assigned to a player (X or O). So this part of the code:
for( i = 0; i < 3; i++ ) {
for( j = 0; j < 3; j++ )
if(table[i][j] == '0'+n)
scans through the board for numbers that have not been changed to X or O.
The code below the nested loops starting from "counter++" verifies the number of times the nested loop looped. The nested loop should continue to loop for 3 times, so the counter variable should also increment 2 times and if it looped less than 1 then the game is considered a draw.
This is how I think it should work, if I omitted any mistake, feel free to correct me
Huge thanks to @AlanBirtles for his comment, I suddenly got an idea.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tony |
