You are on page 1of 3

How to make Simple Tic Tac Toe Game in C# windows application

This is simple Tic Tac Toe game in C# windows application in which we


use 10 buttons, 9 buttons for game the text of these buttons by default null,
and 1 for Reset.

The code of reset function is:

 public void reset()
               {
                     button1.Text = button2.Text =
                     button3.Text = button4.Text =
                     button5.Text = button6.Text =
                     button7.Text = button8.Text =
                     button9.Text = "";
                     i = 0;
                }

When we click on any button program first check the 2 conditions first the
text of button is null and the how many times buttons are clicked.

  if (i < 9 && button1.Text == "")

Then check the value of (i) if the value of (i) is even then button text
change into (X) else (O) and increase the value of (i).

  if (i % 2 == 0)
                    button1.Text = "X";
                else
                    button1.Text = "O";
                i++;
Then check the text of buttons in row, column or diagonal if the text of row,
column or diagonal match then the text of team won the game and call the
reset function.

  if (button2.Text == text && button3.Text == text)


                {
                    MessageBox.Show("Team " + text + " Won
the Game");
                    reset();
                }
                else if (button4.Text == text &&
button7.Text == text)
                {
                    MessageBox.Show("Team " + text + " Won
the Game");
                    reset();
                }

If the value of i=9 and text of any row, column or diagonal is not same then
program display the message no one won the game.

  else if (i == 9)
                {
                    MessageBox.Show("No one Won the Game");
                    reset();
                }

Like this we code for all 9 buttons and enjoy the game.
Screen Shot

You might also like