DEV Community

Kerimova_Manzura
Kerimova_Manzura

Posted on • Edited on

"Tic Tac Toe" in C#.

๐ŸŽฎ Console Tic Tac Toe Game in Csharp

using System;

class TicTacToe
{
    static char[] board = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    static int player = 1;
    static int choice;
    static int flag = 0; // 0: playing, 1: win, -1: draw

    static void Main()
    {
        do
        {
            Console.Clear();
            Console.WriteLine("๐ŸŽฎ TIC TAC TOE GAME");
            Console.WriteLine("Player 1: X    Player 2: O");
            Console.WriteLine();
            PrintBoard();

            player = (player % 2 == 0) ? 2 : 1;
            Console.Write($"Player {player}, enter a number (1-9): ");

            bool validInput = int.TryParse(Console.ReadLine(), out choice);
            if (!validInput || choice < 1 || choice > 9 || board[choice - 1] == 'X' || board[choice - 1] == 'O')
            {
                Console.WriteLine("โŒ Invalid move! Press Enter to try again.");
                Console.ReadLine();
                continue;
            }

            board[choice - 1] = (player == 1) ? 'X' : 'O';
            flag = CheckWin();
            player++;
        }
        while (flag == 0);

        Console.Clear();
        PrintBoard();

        if (flag == 1)
            Console.WriteLine($"\n๐Ÿ† Player {(player % 2) + 1} wins!");
        else
            Console.WriteLine("\n๐Ÿค It's a draw!");

        Console.ReadLine();
    }

    static void PrintBoard()
    {
        Console.WriteLine("     |     |     ");
        Console.WriteLine($"  {board[0]}  |  {board[1]}  |  {board[2]}");
        Console.WriteLine("_____|_____|_____");
        Console.WriteLine("     |     |     ");
        Console.WriteLine($"  {board[3]}  |  {board[4]}  |  {board[5]}");
        Console.WriteLine("_____|_____|_____");
        Console.WriteLine("     |     |     ");
        Console.WriteLine($"  {board[6]}  |  {board[7]}  |  {board[8]}");
        Console.WriteLine("     |     |     ");
    }

    static int CheckWin()
    {
        // Horizontal wins
        if (board[0] == board[1] && board[1] == board[2]) return 1;
        if (board[3] == board[4] && board[4] == board[5]) return 1;
        if (board[6] == board[7] && board[7] == board[8]) return 1;

        // Vertical wins
        if (board[0] == board[3] && board[3] == board[6]) return 1;
        if (board[1] == board[4] && board[4] == board[7]) return 1;
        if (board[2] == board[5] && board[5] == board[8]) return 1;

        // Diagonal wins
        if (board[0] == board[4] && board[4] == board[8]) return 1;
        if (board[2] == board[4] && board[4] == board[6]) return 1;

        // Check for draw
        bool allFilled = true;
        foreach (char c in board)
        {
            if (c != 'X' && c != 'O')
            {
                allFilled = false;
                break;
            }
        }

        return allFilled ? -1 : 0;
    }
}
Enter fullscreen mode Exit fullscreen mode

โœ… Features:

  • 2-player mode
  • Input validation
  • Detects wins and draws
  • Clean console layout

๐Ÿ’ก How to Run:

  1. Copy the code into a .cs file (e.g., TicTacToe.cs).
  2. Compile using a C# compiler:
   csc TicTacToe.cs
Enter fullscreen mode Exit fullscreen mode
  1. Run:
   TicTacToe.exe
Enter fullscreen mode Exit fullscreen mode

Do you want:

  • a GUI version using Windows Forms or WPF?
  • or maybe a version with an AI opponent?

Top comments (0)