Skip to content

Instantly share code, notes, and snippets.

@Qubik65536
Created May 28, 2024 00:16
Show Gist options
  • Save Qubik65536/d0ae9ef64cc82386f88342f5a27cbaab to your computer and use it in GitHub Desktop.
Save Qubik65536/d0ae9ef64cc82386f88342f5a27cbaab to your computer and use it in GitHub Desktop.
TTT-CSharp
public static bool checkForObviousMove(char[,] board) {
int rows = board.GetLength(0);
int cols = board.GetLength(1);
int x;
int y;
int obviousCount;
int blankCount;
for (int i = 0; i < rows; i++)
{
x = -1;
y = -1;
obviousCount = 0;
blankCount = 0;
for (int j = 0; j < cols; j++)
{
if (board[i, j] == 'o')
{
obviousCount++;
}
if (board[i, j] == ' ')
{
x = i;
y = j;
blankCount++;
}
}
if (obviousCount == cols - 1 && blankCount == 1)
{
writeOnBoard(board, 'o', x, y);
return true;
}
}
for (int j = 0; j < cols; j++)
{
x = -1;
y = -1;
obviousCount = 0;
blankCount = 0;
for (int i = 0; i < rows; i++)
{
if (board[i, j] == 'o')
{
obviousCount++;
}
if (board[i, j] == ' ')
{
x = i;
y = j;
blankCount++;
}
}
if (obviousCount == rows - 1 && blankCount == 1)
{
writeOnBoard(board, 'o', x, y);
return true;
}
}
x = -1;
y = -1;
obviousCount = 0;
blankCount = 0;
for (int i = 0, j = 0; i < rows && j < cols; i++, j++)
{
if (board[i, j] == 'o')
{
obviousCount++;
}
if (board[i, j] == ' ')
{
x = i;
y = j;
blankCount++;
}
}
if (obviousCount == rows - 1 && blankCount == 1)
{
writeOnBoard(board, 'o', x, y);
return true;
}
x = -1;
y = -1;
obviousCount = 0;
blankCount = 0;
for (int i = rows - 1, j = 0; i >= 0 && j < cols; i--, j++)
{
if (board[i, j] == 'o')
{
obviousCount++;
}
if (board[i, j] == ' ')
{
x = i;
y = j;
blankCount++;
}
}
if (obviousCount == rows - 1 && blankCount == 1)
{
writeOnBoard(board, 'o', x, y);
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment