First we create a simple form and drag&drop 9 button controls on form.
(ctrl+button click) select all button and generate click event. you can choose
any name of click event
Now design phase is complete, We are going to coding phase.are you ready for it?
Step by step we are go ahead
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int flag = 0;
public void clean()
{
btn1.Text = "";
btn2.Text = "";
btn3.Text = "";
btn4.Text = "";
btn5.Text = "";
btn6.Text = "";
btn7.Text = "";
btn8.Text = "";
btn9.Text = "";
}
private void abc(object sender, System.EventArgs e)
{
Button btn = (Button)sender // create button object
if (btn.Text == "") // check condition object is empty or not
{
if (flag == 0) // check flag 0 or 1
{
btn.Text = "x"; // if flag is 0 thn display X othewise 0
flag = 1;
}
else
{
btn.Text = "0";
flag = 0;
}
}
if (btn1.Text == btn2.Text && btn1.Text == btn3.Text && btn1.Text != "")
{
MessageBox.Show(btn1.Text = "is win");
clean();
}
if (btn4.Text == btn5.Text && btn4.Text == btn6.Text && btn4.Text != "")
{
MessageBox.Show(btn4.Text = "is win");
clean();
}
if (btn7.Text == btn8.Text && btn7.Text == btn9.Text && btn7.Text != "")
{
MessageBox.Show(btn7.Text = "is win");
clean();
}
if (btn1.Text == btn4.Text && btn1.Text == btn7.Text && btn1.Text != "")
{
MessageBox.Show(btn1.Text = "is win");
clean();
}
if (btn2.Text == btn5.Text && btn2.Text == btn8.Text && btn2.Text != "")
{
MessageBox.Show(btn2.Text = "is win");
clean();
}
if (btn3.Text == btn6.Text && btn3.Text == btn9.Text && btn3.Text != "")
{
MessageBox.Show(btn3.Text = "is win");
clean();
}
if (btn1.Text == btn5.Text && btn1.Text == btn9.Text && btn1.Text != "")
{
MessageBox.Show(btn1.Text = "is win");
clean();
}
if (btn3.Text == btn5.Text && btn3.Text == btn7.Text && btn3.Text != "")
{
MessageBox.Show(btn3.Text = "is win");
clean();
}
}
}
}
All if condition check some condition and logic of tic-tac-toe like same
row same column not all are 0 and x
first check first column and first row all condition and cross connection check
Thats over
Enjoy

Comments
Post a Comment