Introduction
This Tic-Tac-Toe Window Game usually played with paper and pen by peoples.
How To Design
On New Form Window drag and drop 9 button controls from Toolbox and select all controls go to property window, choose Click Events Property and on Click Property give name it BtnAll .
and after doing this double click on Click Property Now the BtnAll_Click Event Opens on Code Window. Then Write down the following code on Code Window.
namespace TicTokGameByJatin
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int Count = 1;
private void BtnAll(object sender, EventArgs
e)
{
Button b = (Button)sender;
b.Text = (Count % 2 == 0) ? "X"
: "0";
b.Enabled = false;
check();
Count++;
}
//condition for 8 logic with row
column diagonal.................
void check()
{
if (button1.Text == button2.Text &&
button2.Text == button3.Text && button1.Text != "")
{
result(button1.Text);
}
else if (button4.Text
== button5.Text && button5.Text == button6.Text && button4.Text
!= "")
{
result(button4.Text);
}
else if (button7.Text
== button8.Text && button8.Text == button9.Text && button7.Text
!= "")
{
result(button7.Text);
}
else if (button1.Text
== button4.Text && button4.Text == button7.Text && button1.Text
!= "")
{
result(button1.Text);
}
else if (button2.Text
== button5.Text && button5.Text == button8.Text && button2.Text
!= "")
{
result(button2.Text);
}
else if (button3.Text
== button6.Text && button6.Text == button9.Text && button3.Text
!= "")
{
result(button3.Text);
}
else if (button1.Text
== button5.Text && button5.Text == button9.Text && button1.Text
!= "")
{
result(button1.Text);
}
else if (button3.Text
== button5.Text && button5.Text == button7.Text && button3.Text
!= "")
{
result(button3.Text);
}
else if (Count == 10)
{
MessageBox.Show("Game Draw!!!!!");
}
}
//code for result.....................
void result(string text)
{
if (text == "X")
{
MessageBox.Show("Second
Player has won the GAME!!!!!!!!");
}
else if (text == "0")
{
MessageBox.Show("First
Player has won the GAME!!!!!!!!");
}
else
{
MessageBox.Show("");
}
if (MessageBox.Show("Are You Want To Play Again!", "Confirmation Message", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
reset();
}
else
{
this.Close();
}
}
//code for reset
button.....................
void reset()
{
foreach (Button b in this.Controls)
{
b.Text = "";
b.Enabled = true;
}
Count = 1;
}
}
}
0 comments:
Post a Comment