Hello I need help with a Win Condition bug for a 9x9 grid tic tac toe game with standard 3 squares in a row to win mechanic

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 </>

2 Answers

2

Could there be a bug in how the player[] array is populated? It may help to print out the relevant player[] and allConditions[] entries when you hit the “Current player has won” line to see if some values are not what you expect.

This seems like a roundabout way to detect for 3-in-a-row. Couldn’t you start at the last played position and then look in all 8 directions to see if there are 2 more matching symbols? At the very least, it seems like you could populate the allConditions array just one time at startup rather than every time Won() is called because it is not going to change.

There’s actually only 224 winning possibilities, not 240.

I don’t understand why you need the allConditions array. Your solution seems overly complex and confusing. What you can do is just loop through the board to see if someone won:

public Seed CheckForWin ()
	{
		for (int col = 0; col < 9; col++) {
			for (int row = 0; row < 9; row++) {
				Seed seed = board [col, row];

				if (seed == Seed.EMPTY) {
					//no one played this position yet
					continue;
				}

				//Check horizontals
				if ((col < 7) &&
					board [col + 1, row] == seed &&
					board [col + 2, row] == seed) {

					//seed won
					return seed;
				}

				//Check verticals
				if ((row < 7) &&
					board [col, row + 1] == seed &&
					board [col, row + 2] == seed) {

					//seed won
					return seed;
				}

				//Check diagonal 1
				if ((col < 7) && (row < 7) &&
					board [col + 1, row + 1] == seed &&
					board [col + 2, row + 2] == seed) {

					//seed won
					return seed;
				}

				//Check diagonal 2
				if ((col < 7) && (row > 1) &&
					board [col + 1, row - 1] == seed &&
					board [col + 2, row - 2] == seed) {

					//seed won
					return seed;
				}
			}
		}
		return Seed.EMPTY; //no body won yet
	}