Tic Tac Toe in C#
using System;
using System.Drawing;
using System.Windows.Forms;
class ttt :Form
{
Label ttt1;
TextBox tt1,tt2,tt3,tt4,tt5,tt6,tt7,tt8,tt9;
int count;
bool first,second;
ttt()
{
ClientSize = new Size(200,200);
Text="Reminder";
count=0;
/*tic tac toe*/
ttt1=new Label();
ttt1.Location=new Point(10,10);
ttt1.Text="TIC TAC TOE";
ttt1.ForeColor=Color.Cyan;
tt1=new TextBox();
tt1.Location=new Point(30,40);
tt1.Size=new Size(40,40);
tt1.Click += new EventHandler(tt1_Click);
tt2=new TextBox();
tt2.Location=new Point(70,40);
tt2.Size=new Size(40,40);
tt2.Click += new EventHandler(tt2_Click);
tt3=new TextBox();
tt3.Location=new Point(110,40);
tt3.Size=new Size(40,40);
tt3.Click += new EventHandler(tt3_Click);
tt4=new TextBox();
tt4.Location=new Point(30,60);
tt4.Size=new Size(40,40);
tt4.Click += new EventHandler(tt4_Click);
tt5=new TextBox();
tt5.Location=new Point(70,60);
tt5.Size=new Size(40,40);
tt5.Click += new EventHandler(tt5_Click);
tt6=new TextBox();
tt6.Location=new Point(110,60);
tt6.Size=new Size(40,40);
tt6.Click += new EventHandler(tt6_Click);
tt7=new TextBox();
tt7.Location=new Point(30,80);
tt7.Size=new Size(40,40);
tt7.Click += new EventHandler(tt7_Click);
tt8=new TextBox();
tt8.Location=new Point(70,80);
tt8.Size=new Size(40,40);
tt8.Click += new EventHandler(tt8_Click);
tt9=new TextBox();
tt9.Location=new Point(110,80);
tt9.Size=new Size(40,40);
tt9.Click += new EventHandler(tt9_Click);
first=false;
second=false;
Controls.AddRange(new Control[]{ttt1,tt1,tt2,tt3,tt4,tt5,tt6,tt7,tt8,tt9}
);
MessageBox.Show("Welcome to the tic tac toe GAME");
}
private void tt1_Click(
object sender, System.EventArgs e)
{
if(first==false)
{
tt1.Text="X";
first=true;
second=false;
tt1.ForeColor=Color.Brown;
}
else
{
tt1.Text="0";
first=false;
second=true;
tt1.ForeColor=Color.Cyan;
}
count++;
check();
}
private void tt2_Click(
object sender, System.EventArgs e)
{
if(first==false)
{
tt2.Text="X";
first=true;
second=false;
tt2.ForeColor=Color.Brown;
}
else
{
first=false;
second=true;
tt2.Text="0";
tt2.ForeColor=Color.Cyan;
}
count++;
check();
}
private void tt3_Click(
object sender, System.EventArgs e)
{
if(first==false)
{
tt3.Text="X";
first=true;
second=false;
tt3.ForeColor=Color.Brown;
}
else
{
first=false;
second=true;
tt3.Text="0";
tt3.ForeColor=Color.Cyan;
}
count++;
check();
}
private void tt4_Click(
object sender, System.EventArgs e)
{
if(first==false)
{
tt4.Text="X";
first=true;
second=false;
tt4.ForeColor=Color.Brown;
}
else
{
first=false;
second=true;
tt4.ForeColor=Color.Cyan;
tt4.Text="0";
}
count++;
check();
}
private void tt5_Click(
object sender, System.EventArgs e)
{
if(first==false)
{
tt5.Text="X";
first=true;
second=false;
tt5.ForeColor=Color.Brown;
}
else
{
first=false;
second=true;
tt5.Text="0";
tt5.ForeColor=Color.Cyan;
}
count++;
check();
}
private void tt6_Click(
object sender, System.EventArgs e)
{
if(first==false)
{
tt6.Text="X";
first=true;
second=false;
tt6.ForeColor=Color.Brown;
}
else
{
first=false;
second=true;
tt6.ForeColor=Color.Cyan;
tt6.Text="0";
}
count++;
check();
}
private void tt7_Click(
object sender, System.EventArgs e)
{
if(first==false)
{
tt7.Text="X";
first=true;
second=false;
tt7.ForeColor=Color.Brown;
}
else
{
first=false;
second=true;
tt7.Text="0";
tt7.ForeColor=Color.Cyan;
}
count++;
check();
}
private void tt8_Click(
object sender, System.EventArgs e)
{
if(first==false)
{
tt8.Text="X";
first=true;
second=false;
tt8.ForeColor=Color.Brown;
}
else
{
first=false;
second=true;
tt8.ForeColor=Color.Cyan;
tt8.Text="0";
}
count++;
check();
}
private void tt9_Click(
object sender, System.EventArgs e)
{
if(first==false)
{
tt9.Text="X";
first=true;
second=false;
tt9.ForeColor=Color.Brown;
}
else
{
first=false;
second=true;
tt9.ForeColor=Color.Cyan;
tt9.Text="0";
}
count++;
check();
}
void check()
{
if(tt1.Text=="X" && tt1.Text==tt2.Text && tt3.Text=="X" ||
tt1.Text=="X" && tt4.Text=="X" && tt7.Text=="X" ||
tt1.Text=="X" && tt5.Text=="X" && tt9.Text=="X" ||
tt2.Text=="X" && tt5.Text=="X" && tt8.Text=="X" ||
tt3.Text=="X" && tt6.Text=="X" && tt9.Text=="X" ||
tt4.Text=="X" && tt5.Text=="X" && tt6.Text=="X" ||
tt7.Text=="X" && tt8.Text=="X" && tt9.Text=="X" ||
tt3.Text=="X" && tt5.Text=="X" && tt7.Text=="X")
{
MessageBox.Show( "X is winner");
tt1.Text=" ";tt2.Text=" ";tt3.Text=" ";
tt4.Text=" ";tt5.Text=" ";tt6.Text=" ";
tt7.Text=" ";tt8.Text=" ";tt9.Text=" ";
//Application.Exit();
}
else if(tt1.Text=="0" && tt1.Text==tt2.Text && tt3.Text=="0" ||
tt1.Text=="0" && tt4.Text=="0" && tt7.Text=="0" ||
tt1.Text=="0" && tt5.Text=="0" && tt9.Text=="0" ||
tt2.Text=="0" && tt5.Text=="0" && tt8.Text=="0" ||
tt3.Text=="0" && tt6.Text=="0" && tt9.Text=="0" ||
tt4.Text=="0" && tt5.Text=="0" && tt6.Text=="0" ||
tt7.Text=="0" && tt8.Text=="0" && tt9.Text=="0" ||
tt3.Text=="0" && tt5.Text=="0" && tt7.Text=="0")
{
MessageBox.Show( "O is winner");
tt1.Text=" ";tt2.Text=" ";tt3.Text=" ";
tt4.Text=" ";tt5.Text="
";
tt6.Text=" ";
tt7.Text=" ";tt8.Text=" ";tt9.Text=" ";
}
else if(count==9)
{
MessageBox.Show("Match is draw");
tt1.Text=" ";tt2.Text=" ";tt3.Text=" ";
tt4.Text=" ";tt5.Text=" ";tt6.Text=" ";
tt7.Text=" ";tt8.Text=" ";tt9.Text=" ";
}
}
public static void Main()
{
Application.Run(new ttt());
}
}












No comments yet... Be the first to leave a reply!