To determine the winner of a Tic Tac Toe game, we can check for winning conditions after each player’s turn. The winning conditions are:
- Three of the same markers (either ‘X’ or ‘O’) in a row (horizontal).
- Three of the same markers in a column (vertical).
- Three of the same markers in a diagonal.
Problem Statement
Given a 3x3 Tic Tac Toe board represented as a 2D array of characters ('X'
, 'O'
, and '.'
for empty), we need to find the winner. If there is a winner, return the character representing the winner; if there’s no winner yet, return '.'
.
C# Implementation
Here’s a C# solution to check for the winner:
using System;
public class TicTacToe
{
public static char FindWinner(char[][] board)
{
// Check rows and columns
for (int i = 0; i < 3; i++)
{
// Check rows
if (board[i][0] != '.' && board[i][0] == board[i][1] && board[i][1] == board[i][2])
{
return board[i][0]; // Return the winner ('X' or 'O')
}
// Check columns
if (board[0][i] != '.' && board[0][i] == board[1][i] && board[1][i] == board[2][i])
{
return board[0][i]; // Return the winner ('X' or 'O')
}
}
// Check diagonals
if (board[0][0] != '.' && board[0][0] == board[1][1] && board[1][1] == board[2][2])
{
return board[0][0]; // Return the winner ('X' or 'O')
}
if (board[0][2] != '.' && board[0][2] == board[1][1] && board[1][1] == board[2][0])
{
return board[0][2]; // Return the winner ('X' or 'O')
}
return '.'; // No winner
}
public static void Main(string[] args)
{
char[][] board = new char[][]
{
new char[] { 'X', 'O', 'X' },
new char[] { 'O', 'X', 'O' },
new char[] { 'O', 'X', 'X' }
};
char result = FindWinner(board);
if (result != '.')
{
Console.WriteLine($"The winner is: {result}");
}
else
{
Console.WriteLine("No winner yet.");
}
}
}
Explanation
-
Data Structure:
- The board is represented as a jagged array (
char[][]
), where each inner array corresponds to a row in the Tic Tac Toe game.
- The board is represented as a jagged array (
-
FindWinner Method:
- This method checks for a winner in the provided board.
-
Row and Column Check:
- A loop iterates over each row and checks if all three cells in that row contain the same marker (
'X'
or'O'
). - It also checks each column in a similar manner.
- A loop iterates over each row and checks if all three cells in that row contain the same marker (
-
Diagonal Check:
- The first diagonal is checked from the top-left to the bottom-right.
- The second diagonal is checked from the top-right to the bottom-left.
-
Return Value:
- If a winning condition is met, the method returns the winning character (
'X'
or'O'
). - If no winner is found, it returns
'.'
.
- If a winning condition is met, the method returns the winning character (
-
Main Method:
- In the
Main
method, a sample Tic Tac Toe board is defined, and theFindWinner
method is called. - The result is printed, indicating whether there is a winner or not.
- In the
Usage
You can modify the board
variable in the Main
method to test different game states. This implementation efficiently checks for a winner by examining rows, columns, and diagonals in a straightforward manner.