C
C#15mo 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
krixsick15mo 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
cap5lut15mo 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
krixsick15mo 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
cap5lut15mo ago
yeah, so with just the loop iterations (outer and inner combined) u basically visit every cell right?
krixsick
krixsick15mo ago
Yea
cap5lut
cap5lut15mo 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
krixsick15mo ago
Yes OH because if its true does the for outer loop reset? wait Nvm i think Im wrong
cap5lut
cap5lut15mo ago
basically with that if u check if the column i is a win column, right?
krixsick
krixsick15mo 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
cap5lut15mo ago
but u check it for all cells, thus u check it for all cells of that column as well,
krixsick
krixsick15mo ago
Ohh So im just constantly checking even if it does return something
cap5lut
cap5lut15mo 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
krixsick15mo ago
Wait then my diagonal checks should be outside the for loops cause looking back that doesn't make sense
cap5lut
cap5lut15mo ago
note that i is only 0 in that stripped down code
krixsick
krixsick15mo ago
Ohh So it never goes to 1 or 2
Want results from more Discord servers?
Add your server