Hello, I am having an issue where my Win conditions for my 9x9 grid tic tac toe game are not functioning properly.
Currently, I am dealing with two bugs, one where the game will automatically end on Player One’s turn on Move 6 and/or 7 regardless of the layout of the board or an outright failure of the win conditions to be detected.
As I stated in my title the game is essentially a 3x3 grid tic tac toe blown out to a 9x9 grid.
I did this because I wanted to be able to add additional features and random spawning into the grid.
Currently, two players can take turns playing the game and when either player wins, loses, or draws the correct methods are called just that these are called inconsistently and not in line with my rules.
This did work perfectly when I started with the standard 3x3 grid.
Any help at all would be appreciated.
I believe the error is restricted to this section of the code:
bool Won(Seed currPlayer) :
Here is the code: — Code Starts </>
bool Won(Seed currPlayer)
{
int[,] allConditions = new int[240, 3]; // There are 240 possible ways to win in a 9x9 grid with 3 in a row
int index = 0;
// Horizontal win conditions
for (int row = 0; row < 9; row++)
{
for (int col = 0; col < 7; col++) // Only go up to the 7th column for horizontal win
{
allConditions[index, 0] = row * 9 + col;
allConditions[index, 1] = row * 9 + col + 1;
allConditions[index, 2] = row * 9 + col + 2;
index++;
}
}
// Vertical win conditions
for (int col = 0; col < 9; col++)
{
for (int row = 0; row < 7; row++) // Only go up to the 7th row for vertical win
{
allConditions[index, 0] = row * 9 + col;
allConditions[index, 1] = (row + 1) * 9 + col;
allConditions[index, 2] = (row + 2) * 9 + col;
if (index < 240)
{
index++;
}
else
{
Debug.LogError("Index out of bounds detected in allConditions array.");//fix for that one error pookie-pie
}
}
}
// Diagonal win conditions (top-left to bottom-right)
for (int row = 0; row < 7; row++)
{
for (int col = 0; col < 7; col++)
{
allConditions[index, 0] = row * 9 + col;
allConditions[index, 1] = (row + 1) * 9 + col + 1;
allConditions[index, 2] = (row + 2) * 9 + col + 2;
index++;
}
}
// Diagonal win conditions (bottom-left to top-right)
for (int row = 2; row < 9; row++)
{
for (int col = 0; col < 7; col++)
{
allConditions[index, 0] = row * 9 + col;
allConditions[index, 1] = (row - 1) * 9 + col + 1;
allConditions[index, 2] = (row - 2) * 9 + col + 2;
index++;
}
}
// Check all win conditions
for (int i = 0; i < index; i++)
{
if (player[allConditions[i, 0]] == currPlayer &&
player[allConditions[i, 1]] == currPlayer &&
player[allConditions[i, 2]] == currPlayer)
{
return true; // Current player has won
}
}
return false; // No win found
}
bool IsDraw()
{
bool player1Won, player2Won, anyEmpty;
// Check if player-1 has won or not
player1Won = Won(Seed.CROSS);
// Check if player-2 has won or not
player2Won = Won(Seed.NOUGHT);
// Check if there is any empty cell or not
anyEmpty = IsAnyEmpty();
bool isDraw = false;
// Use logical AND (&&) instead of bitwise AND (&)
if (!player1Won && !player2Won && !anyEmpty)
isDraw = true;
return isDraw;
}
— Code Ends </>