C
C#2y ago
krixsick

❔ ✅ 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:
namespace Coding.Exercise
{
public class TicTacToe
{
public static bool Checker(string[,] board)
{
// here we perform horizontal and vertical checks
for (int i = 0; i < 3; i++)
{
if (board[i,0] == board[i,1] && board[i,1] == board[i,2])
return true;
if (board[0,i] == board[1,i] && board[1,i] == board[2,i])
return true;
}
// here diagonal checks
if (board[0,0] == board[1,1] && board[1,1] == board[2,2])
return true;
if (board[0,2] == board[1,1] && board[1,1] == board[2,0])
return true;
return false;
}
}
}
namespace Coding.Exercise
{
public class TicTacToe
{
public static bool Checker(string[,] board)
{
// here we perform horizontal and vertical checks
for (int i = 0; i < 3; i++)
{
if (board[i,0] == board[i,1] && board[i,1] == board[i,2])
return true;
if (board[0,i] == board[1,i] && board[1,i] == board[2,i])
return true;
}
// here diagonal checks
if (board[0,0] == board[1,1] && board[1,1] == board[2,2])
return true;
if (board[0,2] == board[1,1] && board[1,1] == board[2,0])
return true;
return false;
}
}
}
My answer to the code is this:
38 Replies
krixsick
krixsickOP2y ago
namespace Coding.Exercise
{
public class TicTacToe
{
public static bool Checker(string[,] board)
{

for (int i = 0; i < board.GetLength(0); i++)
{
for (int j = 0; j < board.GetLength(1); j++)
{
if(board[i,0] == board[i,1] && board[i,1] == board[i,2])
{
return true;
}
else if(board[0,j] == board[1,j] && board[1,j] == board[2,j]){
return true;
}
else if(i == j){
return true;
}
//diagonal
else if(board[0,2] == board[1,1] && board[1,1] == board[2,0]){
return true;
}
else{
return false;
}


}
}

}
}
}
namespace Coding.Exercise
{
public class TicTacToe
{
public static bool Checker(string[,] board)
{

for (int i = 0; i < board.GetLength(0); i++)
{
for (int j = 0; j < board.GetLength(1); j++)
{
if(board[i,0] == board[i,1] && board[i,1] == board[i,2])
{
return true;
}
else if(board[0,j] == board[1,j] && board[1,j] == board[2,j]){
return true;
}
else if(i == j){
return true;
}
//diagonal
else if(board[0,2] == board[1,1] && board[1,1] == board[2,0]){
return true;
}
else{
return false;
}


}
}

}
}
}
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
cap5lut
cap5lut2y ago
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?
krixsick
krixsickOP2y ago
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]
cap5lut
cap5lut2y ago
yeah, so with just the loop iterations (outer and inner combined) u basically visit every cell right?
krixsick
krixsickOP2y ago
Yea
cap5lut
cap5lut2y ago
so for every cell u have this check: if(board[i,0] == board[i,1] && board[i,1] == board[i,2]) for example
krixsick
krixsickOP2y ago
Yes OH because if its true does the for outer loop reset? wait Nvm i think Im wrong
cap5lut
cap5lut2y ago
basically with that if u check if the column i is a win column, right?
krixsick
krixsickOP2y ago
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
cap5lut
cap5lut2y ago
but u check it for all cells, thus u check it for all cells of that column as well,
krixsick
krixsickOP2y ago
Ohh So im just constantly checking even if it does return something
cap5lut
cap5lut2y ago
lets strip ur loops a bit down:
for (int i = 0; i < 1; i++)
{
for (int j = 0; j < 3; j++)
{
if(board[i,0] == board[i,1] && board[i,1] == board[i,2])
{
return true;
}
}
}
for (int i = 0; i < 1; i++)
{
for (int j = 0; j < 3; j++)
{
if(board[i,0] == board[i,1] && board[i,1] == board[i,2])
{
return true;
}
}
}
krixsick
krixsickOP2y ago
Wait then my diagonal checks should be outside the for loops cause looking back that doesn't make sense
cap5lut
cap5lut2y ago
note that i is only 0 in that stripped down code
krixsick
krixsickOP2y ago
Ohh So it never goes to 1 or 2
cap5lut
cap5lut2y ago
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:
for (int i = 0; i < 3; i++)
{
if(board[i,0] == board[i,1] && board[i,1] == board[i,2])
{
return true;
}
}
for (int i = 0; i < 3; i++)
{
if(board[i,0] == board[i,1] && board[i,1] == board[i,2])
{
return true;
}
}
and it would still check for the ith column
krixsick
krixsickOP2y ago
Ohh 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?
cap5lut
cap5lut2y ago
yeah, there u would only need the inner loop, as u do not care about i there
krixsick
krixsickOP2y ago
Ohh I see
cap5lut
cap5lut2y ago
so for columns u dont need the inner, and for rows not the outer loop, so for this part u dont even need nesting:
// check columns
for (int i = 0; i < 3; i++)
{
if(board[i,0] == board[i,1] && board[i,1] == board[i,2])
{
return true;
}
}
// check rows
for (int j = 0; j < 3; j++)
{
if(board[0,j] == board[1,j] && board[1,j] == board[2,j])
{
return true;
}
}
// check columns
for (int i = 0; i < 3; i++)
{
if(board[i,0] == board[i,1] && board[i,1] == board[i,2])
{
return true;
}
}
// check rows
for (int j = 0; j < 3; j++)
{
if(board[0,j] == board[1,j] && board[1,j] == board[2,j])
{
return true;
}
}
krixsick
krixsickOP2y ago
Ohh I see Ohh the nested loop I created is good to access ALL the elements inside the 2-D arrays
cap5lut
cap5lut2y ago
yes, all one by one
krixsick
krixsickOP2y ago
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!
cap5lut
cap5lut2y ago
well, it is inefficient yes, but u could make it work ^^
krixsick
krixsickOP2y ago
Got it, gonna try to redo this again Thank you again!
cap5lut
cap5lut2y ago
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)
MODiX
MODiX2y ago
Use the /close command to mark a forum thread as answered
krixsick
krixsickOP2y ago
Thanks! Will definitely ask here again if that's fine with you lol, but again Thank you!
cap5lut
cap5lut2y ago
ofc, thats what this thread is for!
krixsick
krixsickOP2y ago
Ok, I think I finished it, this is my new version:
using System;

namespace Coding.Exercise
{
public class TicTacToe
{
public static bool Checker(string[,] board)
{
for(int i = 0; i < board.GetLength(0); i++)
{
if(board[i,0] == board[i, 1] && board[i,1] == board[i,2]){
return true;
}
}
for(int j = 0; j < board.GetLength(1); j++){
if(board[0,j] == board[1, j] && board[1,j] == board[2,j]){
return true;
}
}
if(board[0,0] == board[1,1] && board[1,1] == board[2,2]){
return true;
}
else if(board[0,2] == board[1,1] && board[1,1] == board[2,0]){
return true;
}
else{
return false;
}
}
}
}
using System;

namespace Coding.Exercise
{
public class TicTacToe
{
public static bool Checker(string[,] board)
{
for(int i = 0; i < board.GetLength(0); i++)
{
if(board[i,0] == board[i, 1] && board[i,1] == board[i,2]){
return true;
}
}
for(int j = 0; j < board.GetLength(1); j++){
if(board[0,j] == board[1, j] && board[1,j] == board[2,j]){
return true;
}
}
if(board[0,0] == board[1,1] && board[1,1] == board[2,2]){
return true;
}
else if(board[0,2] == board[1,1] && board[1,1] == board[2,0]){
return true;
}
else{
return false;
}
}
}
}
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
cap5lut
cap5lut2y ago
looking good ;p and now think about ur two for loops, basically they do the same thing, iterating from 0 to 2
krixsick
krixsickOP2y ago
Oh 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?
cap5lut
cap5lut2y ago
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
if (a) {
return true;
}
else {
return false;
}
if (a) {
return true;
}
else {
return false;
}
and
if (a) {
return true;
}
return false;
if (a) {
return true;
}
return false;
⤴️ is basically the same
krixsick
krixsickOP2y ago
Ohhh got it thank you so much!
cap5lut
cap5lut2y ago
glad i could help 😄 dont forget to $close the thread
MODiX
MODiX2y ago
Use the /close command to mark a forum thread as answered
krixsick
krixsickOP2y ago
Oh Yea thanks for the reminder Hope you have a great day and again thank you! !close
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server