Tic Tac Toe Game using C#
Posted by Samath
Last Updated: January 17, 2024

This is a simple two player tic tac toe game written using C#. When the game start, player 1 start by clicking on a empty box. Each player takes turn until someone win or the game draw. Player 1 is automatically assign X and player 2 is assign O.  As i said before this is a simple game, the game has two button. New game button which resets the game and Exit button which quits the game.

Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TicTacToe
{
    public partial class Form1 : Form
    {
        bool isTurn = true; 
        int Turn_Count = 0;

        public Form1()
        {
            InitializeComponent();
        }

  
  

        private void button_click(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            if (isTurn)
            {
                b.Text = "X";
                b.BackColor = Color.DarkGoldenrod;
            }
            else
            {
                b.Text = "O";
                b.BackColor = Color.Moccasin;
            }
            isTurn = !isTurn;
            b.Enabled = false;
            Turn_Count++;

            Winner();
        }

        private void Winner()
        {
            bool isWinner = false;


            if ((A1.Text == A2.Text) && (A2.Text == A3.Text) && (!A1.Enabled))
                isWinner = true;
            else if ((B1.Text == B2.Text) && (B2.Text == B3.Text) && (!B1.Enabled))
                isWinner = true;
            else if ((C1.Text == C2.Text) && (C2.Text == C3.Text) && (!C1.Enabled))
                isWinner = true;


            else if ((A1.Text == B1.Text) && (B1.Text == C1.Text) && (!A1.Enabled))
                isWinner = true;
            else if ((A2.Text == B2.Text) && (B2.Text == C2.Text) && (!A2.Enabled))
                isWinner = true;
            else if ((A3.Text == B3.Text) && (B3.Text == C3.Text) && (!A3.Enabled))
                isWinner = true;


            else if ((A1.Text == B2.Text) && (B2.Text == C3.Text) && (!A1.Enabled))
                isWinner = true;
            else if ((A3.Text == B2.Text) && (B2.Text == C1.Text) && (!C2.Enabled))
                isWinner = true;


            if (isWinner)
            {
                Disable_Button();

                string winner = "";
                if (isTurn)
                    winner = "O";
                else
                    winner = "X";

                MessageBox.Show(winner + " Wins!", "Tic Tac Toe");
            }
            else
            {
                if (Turn_Count == 9)
                    MessageBox.Show("Draw!", "Tic Tac Toe");
            }
        }
        private void Disable_Button()
        {
            
                foreach (Control c in Controls)
                {
                    Button b = (Button)c;
                    if (b.Text != "New Game" && b.Text != "Exit")
                    {
                        
                        b.Enabled = false;
                    }
                }
           
        }

   

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnnewGame_Click(object sender, EventArgs e)
        {
            isTurn = true;
            Turn_Count = 0;

                foreach (Control c in Controls)
                {

                    Button b = (Button)c;
                    if(b.Text!= "New Game" && b.Text != "Exit")
                    {
                        b.Enabled = true;
                        b.Text = "";
                    b.BackColor = System.Drawing.Color.Wheat;
                    b.Font = new System.Drawing.Font("Microsoft Sans Serif", 27.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    b.ForeColor = System.Drawing.Color.Cyan;
                    b.UseVisualStyleBackColor = false;
                }

                }
       
        }

       
    }
}

Related Content
Tic Tac Toe Game using Python
Tic Tac Toe Game using Python
Samath | Jan 17, 2024
2 Player Tic Tac Toe Game in C++
2 Player Tic Tac Toe Game in C++
Samath | Jan 06, 2021
The Matching Game in Java
The Matching Game in Java
Samath | Mar 20, 2015
Hangman Game in C++
Hangman Game in C++
Samath | Jan 17, 2024
Hangman Game using C#
Hangman Game using C#
Samath | Jan 17, 2024
Animal Game in Java
Animal Game in Java
gideonna | Apr 13, 2016
Number Guessing Game using C++
Number Guessing Game using C++
Samath | Jan 04, 2017
Blackjack Game in C++
Blackjack Game in C++
Samath | Jan 05, 2017
Checkers Game using C++
Checkers Game using C++
Samath | Jan 17, 2024
Minesweeper Game using C++
Minesweeper Game using C++
Samath | Jan 05, 2017
Craps Dice game in C++
Craps Dice game in C++
Samath | Jan 06, 2021
Paper Strip Game using Python
Paper Strip Game using Python
Samath | Jan 17, 2024
John Conway's Game of Life using C++
John Conway's Game of Life using C++
Samath | Jan 04, 2017
C++ Rock Paper Scissors Game
C++ Rock Paper Scissors Game
Samath | Jan 05, 2017