❔ ✅ Basic Tic Tac Toe Checker that uses a 2-D Array
The question says this:
This time, you have to write only a checker for the game. It will be a method that takes a 2D array and returns a boolean. If there is a winner, it returns true otherwise false.
We use "X" and "O" as signs of our players. The empty places will be filled with numbers from 1 to 9.
You have to check 3 types of cases:
horizontal;
vertical;
diagonal;
The answer to the code is:
My answer to the code is this:
38 Replies
I was wondering if anyone could help me identify how my code is wrong or why the answer code only used for (int i = 0; i < 3; i++) and not a nested for loop to access the elements inside the 2-D array
1)
else if(i == j)
:
if noone won using the coordinate 0, 0, this is check results in true
, basically on the first iteration
2) ur diagonal check checks only only one diagonal, but there are 2 diagonals in a square, u are missing the 0,0 -> 1,1 -> 2,2 diagonal
3) else { return false; }
lets assume 1) didnt exist and u would reach this point
at this point u have checked the horizontal line, the vertical line and (lets assume 2) is fixed) the diagonal lines for 0,0
if anyone would have won in a line not involving 0,0 u then here return a false negative and breaking the loops and return from the method
4) this one is not an error resulting in a wrong result, but a logical one: with these 2 nested loops u iterate over each cell of the board, and check each line.
basically at 0,0 u test for 0,0 -> 0,1 -> 0,2
for 0,1 u test for 0,0 -> 0,1 -> 0,2
for 0,2 u test for 0,0 -> 0,1 -> 0,2
as well, so its ending up in redundant checks (this happens vertical, horizontal and for one diagonal right now)
on how about to reach the answer's code ill get to in ~5min need a quick smoke
back, so can u follow so far, or do u have any questions?Oh thank you so much for the feedback, I get everything just expect for the last part, by for 0,2 u test for 0,0 -> 0,1 -> 0,2
My thought process was that that for loops would function like this, so it first does [0, j] and j gets looped 3 times, resulting in [0,0], [0,1], [0,2]
Then it goes back to the outer loop
and goes back to the inner loop again, resulting in [1,0], [1,1], [1,2]
yeah, so with just the loop iterations (outer and inner combined) u basically visit every cell right?
Yea
so for every cell u have this check:
if(board[i,0] == board[i,1] && board[i,1] == board[i,2])
for exampleYes
OH
because if its true
does the for outer loop reset?
wait
Nvm i think Im wrong
basically with that if u check if the column
i
is a win column, right?Yea
Cause the i variable should be the same throughout
So if its the same, it'll create a line of the same value making it considered a win i think
but u check it for all cells, thus u check it for all cells of that column as well,
Ohh So im just constantly checking even if it does return something
lets strip ur loops a bit down:
Wait then my diagonal checks should be outside the for loops
cause looking back that doesn't make sense
note that
i
is only 0 in that stripped down codeOhh
So it never goes to 1 or 2