A simple game TIC TAC TOE
I just created a simple application a fun application we all familiar to this application which is known as TIC TAC TOE. Whenever we feel lonely and want to do something just open this and start playing.
In this application I just used ten buttons and one label nine buttons are for playing
game and remaining one is for restarting the game again and the label is used for displaying the status of game.
The functions used in this are winner() which shows the winner
of the game draw() which activates when game draws and start_again() which restarts the game again.
Code for the buttons.
For all buttons use the same code only change the name of the button inside the code.
if (count == 0)
{
button1.Text = "X";
count = count + 1;
}
else
{
button1.Text = "O";
count = 0;
}
winner();
button1.Enabled = false;
start_again();
draw();
Code for the winner function which shows the winner of the game if there is any winner and show the status on the label.
if ((button1.Text == "X" && button2.Text == "X" && button3.Text == "X") || (button1.Text == "O" && button2.Text == "O" && button3.Text == "O"))
{
label1.Text = button1.Text + " is Winner";
label1.Visible = true;
}
if ((button4.Text == "X" && button5.Text == "X" && button6.Text == "X") || (button4.Text == "O" && button5.Text == "O" && button6.Text == "O"))
{
label1.Text = button4.Text + " is Winner";
label1.Visible = true;
}
if ((button7.Text == "X" && button8.Text == "X" && button9.Text == "X") || (button7.Text == "O" && button8.Text == "O" && button9.Text == "O"))
{
label1.Text = button7.Text + " is Winner";
label1.Visible = true;
}
if ((button1.Text == "X" && button4.Text == "X" && button7.Text == "X") || (button1.Text == "O" && button4.Text == "O" && button7.Text == "O"))
{
label1.Text = button1.Text + " is Winner";
label1.Visible = true;
}
if ((button5.Text == "X" && button2.Text == "X" && button8.Text == "X") || (button5.Text == "O" && button2.Text == "O" && button8.Text == "O"))
{
label1.Text = button8.Text + " is Winner";
label1.Visible = true;
}
if ((button9.Text == "X" && button6.Text == "X" && button3.Text == "X") || (button1.Text == "9" && button6.Text == "O" && button3.Text == "O"))
{
label1.Text = button9.Text + " is Winner";
label1.Visible = true;
}
if ((button1.Text == "X" && button5.Text == "X" && button9.Text == "X") || (button1.Text == "O" && button5.Text == "O" && button9.Text == "O"))
{
label1.Text = button1.Text + " is Winner";
label1.Visible = true;
}
if ((button7.Text == "X" && button5.Text == "X" && button3.Text == "X") || (button7.Text == "O" && button5.Text == "O" && button3.Text == "O"))
{
label1.Text = button5.Text + " is Winner";
label1.Visible = true;
}
Code for the draw function which activates whenever game draws show the status on the label
if (button1.Enabled == false && button2.Enabled == false && button3.Enabled == false && button4.Enabled == false && button5.Enabled == false && button6.Enabled == false && button7.Enabled == false && button8.Enabled == false && button9.Enabled == false)
{
label1.Text = "DRAW";
button10.Visible = true;
}
Whenever the game ends and you want to play it again there is a start again button which helps to start the game again and the coding for
that button is as follows.
if (label1.Text != "TIC TAC TOE")
{
button10.Visible = true;
}