tic tac toe in csharp

Here's a basic implementation of Tic Tac Toe game in C#:

main.cs
using System;

namespace TicTacToe
{
    class Program
    {
        static char[] board = new char[9] { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
        static int player = 1;
        static int choice;
        static bool winFlag = false;

        static void Main(string[] args)
        {
            do
            {
                Console.Clear();
                Console.WriteLine("Player1: X and Player2: O");
                Console.WriteLine("\n");

                if(player % 2 == 0)
                {
                    Console.WriteLine("Player 2's turn");
                }
                else
                {
                    Console.WriteLine("Player 1's turn");
                }

                Console.WriteLine("\n");
                boardView();
                choice = int.Parse(Console.ReadLine());

                if(board[choice - 1] != 'X' && board[choice - 1] != 'O')
                {
                    if (player % 2 == 0)
                    {
                        board[choice - 1] = 'O';
                        player++;
                    }
                    else
                    {
                        board[choice - 1] = 'X';
                        player++;
                    }
                }
                else
                {
                    Console.WriteLine("Sorry the row {0} is already marked with {1}", choice, board[choice - 1]);
                    Console.WriteLine("\n");
                    Console.WriteLine("Please wait 2 seconds for board to load again...");
                    System.Threading.Thread.Sleep(2000);
                }

                if(board[0] == board[1] && board[1] == board[2] ||
                        board[3] == board[4] && board[4] == board[5] ||
                            board[6] == board[7] && board[7] == board[8] ||
                                board[0] == board[3] && board[3] == board[6] ||
                                    board[1] == board[4] && board[4] == board[7] ||
                                        board[2] == board[5] && board[5] == board[8] ||
                                            board[0] == board[4] && board[4] == board[8] ||
                                                board[2] == board[4] && board[4] == board[6])
                {
                    Console.Clear();
                    boardView();
                    winFlag = true;

                    if (player % 2 == 0)
                    {
                        Console.WriteLine("Player 1 wins!");
                    }
                    else
                    {
                        Console.WriteLine("Player 2 wins!");
                    }
                }
                else
                {
                    winFlag = false;
                }
            }
            while (winFlag != true);

            Console.ReadLine();
        }
        
        private static void boardView()
        {
            Console.WriteLine("     -------------------------");
            Console.WriteLine("     |   " + board[0] + "   |   " + board[1] + "   |   " + board[2] + "   |");
            Console.WriteLine("     -------------------------");
            Console.WriteLine("     |   " + board[3] + "   |   " + board[4] + "   |   " + board[5] + "   |");
            Console.WriteLine("     -------------------------");
            Console.WriteLine("     |   " + board[6] + "   |   " + board[7] + "   |   " + board[8] + "   |");
            Console.WriteLine("     -------------------------");
            Console.WriteLine("\n");
        }
    }
}
3597 chars
99 lines

The code uses a single-dimensional character array to represent the game board with 9 cells. The user input for cell selection is taken using integer values from 1 to 9, which are then mapped to the corresponding index value in the array. The game checks for win conditions by checking all the possible combinations of cells that can lead to a win.

The game will continue until a player wins or the board gets fully occupied, after which the game will end.

gistlibby LogSnag