Find winner on a tic tac toe game problem in c#

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:

  1. Three of the same markers (either ‘X’ or ‘O’) in a row (horizontal).
  2. Three of the same markers in a column (vertical).
  3. 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

  1. 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.
  2. FindWinner Method:

    • This method checks for a winner in the provided board.
  3. 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.
  4. 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.
  5. Return Value:

    • If a winning condition is met, the method returns the winning character ('X' or 'O').
    • If no winner is found, it returns '.'.
  6. Main Method:

    • In the Main method, a sample Tic Tac Toe board is defined, and the FindWinner method is called.
    • The result is printed, indicating whether there is a winner or not.

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.