Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,943 questions

55,787 answers

573 users

How to write the Tic-Tac-Toe game in C#

1 Answer

0 votes
using System;

/*
    A simple Tic‑Tac‑Toe game written in C#.

    Highlights:
      - The board is represented as a char[] of length 9.
      - Functions handle printing, move validation, win checking,
        and turn progression.
      - The game loop is clear and easy to follow.
      - Comments explain the reasoning behind each part of the program.
*/

class TicTacToe
{
    // Print the board in a 3×3 layout
    static void PrintBoard(char[] board)
    {
        Console.WriteLine();
        for (int i = 0; i < 9; i++) {
            Console.Write(board[i]);
            if ((i + 1) % 3 == 0)
                Console.WriteLine();
            else
                Console.Write(" | ");
        }
        Console.WriteLine();
    }

    // Check if a player has won
    static bool CheckWin(char[] board, char player)
    {
        int[,] wins = {
            {0,1,2}, {3,4,5}, {6,7,8},   // rows
            {0,3,6}, {1,4,7}, {2,5,8},   // columns
            {0,4,8}, {2,4,6}             // diagonals
        };

        for (int i = 0; i < wins.GetLength(0); i++) {
            if (board[wins[i,0]] == player &&
                board[wins[i,1]] == player &&
                board[wins[i,2]] == player)
                return true;
        }
        return false;
    }

    // Check if the board is full (draw)
    static bool BoardFull(char[] board)
    {
        foreach (char c in board) {
            if (c == ' ')
                return false;
        }
        return true;
    }

    // Attempt to place a move; return true if successful
    static bool PlaceMove(char[] board, int pos, char player)
    {
        if (pos < 0 || pos >= 9)
            return false;

        if (board[pos] != ' ')
            return false;

        board[pos] = player;
        return true;
    }

    static void Main()
    {
        char[] board = new char[9];

        // Initialize board with spaces
        for (int i = 0; i < 9; i++)
            board[i] = ' ';

        char currentPlayer = 'X';

        Console.WriteLine("Tic-Tac-Toe");
        PrintBoard(board);

        while (true)
        {
            Console.Write($"Player {currentPlayer}, enter position (0-8): ");
            string input = Console.ReadLine();

            if (!int.TryParse(input, out int pos)) {
                Console.WriteLine("Invalid input. Try again.");
                continue;
            }

            // Try to place the move
            if (!PlaceMove(board, pos, currentPlayer)) {
                Console.WriteLine("Invalid move. Try again.");
                continue;
            }

            PrintBoard(board);

            // Check win
            if (CheckWin(board, currentPlayer)) {
                Console.WriteLine($"Player {currentPlayer} wins!");
                break;
            }

            // Check draw
            if (BoardFull(board))
            {
                Console.WriteLine("It's a draw!");
                break;
            }

            // Switch player
            currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
        }
    }
}


/*
run:

Tic-Tac-Toe

  |   |  
  |   |  
  |   |  

Player X, enter position (0-8): 4

  |   |  
  | X |  
  |   |  

Player O, enter position (0-8): 0

O |   |  
  | X |  
  |   |  

Player X, enter position (0-8): 5

O |   |  
  | X | X
  |   |  

Player O, enter position (0-8): 2

O |   | O
  | X | X
  |   |  

Player X, enter position (0-8): 

O |   | O
X | X | X
  |   |  

Player X wins!

*/

 



answered 5 days ago by avibootz
...