❔ ✅ 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
nah, thats not what i mean
u do the check inside the inner loop, for j = 0 to 2, but u dont even use j there, because u check the whole column
so u could basically do the check outside of the inner loop:
and it would still check for the
i
th columnOhh
For this part:
else if(board[0,j] == board[1,j] && board[1,j] == board[2,j]){
return true;
It would make sense for it to be underneath the inner loop right?
yeah, there u would only need the inner loop, as u do not care about
i
thereOhh I see
so for columns u dont need the inner, and for rows not the outer loop, so for this part u dont even need nesting:
Ohh I see
Ohh
the nested loop I created is good to access ALL the elements inside the 2-D arrays
yes, all one by one
but for this purpose it's just inefficient and won't work
Ohhh I see Omg
Thank you so much for the clarification and help!
well, it is inefficient yes, but u could make it work ^^
Got it, gonna try to redo this again
Thank you again!
glad i could help 🙂
good luck with ur new implementation, if u get stuck just ask here again (or if u r done and everything works fine u can then also $close this thread)
Use the
/close
command to mark a forum thread as answeredThanks! Will definitely ask here again if that's fine with you lol, but again Thank you!
ofc, thats what this thread is for!
Ok, I think I finished it, this is my new version:
Again I know I probably said this, but I seriously want to thank you for your help in helping me understand what I did wrong
looking good ;p
and now think about ur two
for
loops, basically they do the same thing, iterating from 0 to 2Oh you're right
Then there's no point of having another, since it's just used to check but not storing actual values
If it did stored values in a list then an additional loop would be necessary right?
combining the loops to one would change the order of checks. so in this use case it doesnt matter, while in others it might do
and the rest is more or less just syntactic differences to provided answer u couldnt understand at first ;p
and
⤴️ is basically the same
Ohhh got it thank you so much!
glad i could help 😄
dont forget to $close the thread
Use the
/close
command to mark a forum thread as answeredOh Yea thanks for the reminder
Hope you have a great day and again thank you!
!close
Closed!
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.