Header Ads

Header ADS

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

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 can make easily a  tic tac toe game c#


You can make easily a  tic tac toe game c#

                                                                                               Assad Raza 
Visit :http://bel4solutions.com/

8 comments:

  1. i need sourse code , do u have it ?

    ReplyDelete
  2. want the source code,me too

    ReplyDelete
  3. I have made my game any one need code =) ??? only one issue is not resolving my button text is not washing out as mention in above coding i have follow the same pattern :/

    ReplyDelete
  4. here what is the text ?????????
    that will show the error........

    ReplyDelete
  5. Want to learn C# from basics & in most professional Code development style. I'll have you writing real working applications in no time flat-before you reach the end of course.
    Hire C# Teacher.

    ReplyDelete
  6. why their is said object reference not set to an instance of an object???
    is there any solution for me?

    ReplyDelete
  7. Hello, I simply hopped over to your site via StumbleUpon. No longer something I might normally learn, but I favored your emotions none the less. Thanks for making one thing value reading. mega888 game

    ReplyDelete

Powered by Blogger.