using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace final_tic
{
public partial class Form1: Form
{
bool turn = true;
int turn_count = 0;
public Form1()
{
InitializeComponent();
}
private void button_Click(object sender, EventArgs e)
{
Button b = (Button) sender;
if (turn) b.Text = "X";
else b.Text = "O";
b.Enabled = false;
turn = !turn;
turn_count++;
check_winner();
}
private void check_winner()
{
bool winner = false;
//horizental check
if ((A1.Text == A2.Text) && (A2.Text == A3.Text) && (!A1.Enabled)) winner = true;
else if ((B1.Text == B2.Text) && (B2.Text == B3.Text) && (!B1.Enabled)) winner = true;
else if ((C1.Text == C2.Text) && (C2.Text == C3.Text) && (!C1.Enabled)) winner = true;
//Vertical check
else if ((A1.Text == B1.Text) && (B1.Text == C1.Text) && (!A1.Enabled)) winner = true;
else if ((A2.Text == B2.Text) && (B2.Text == C2.Text) && (!A2.Enabled)) winner = true;
else if ((A3.Text == B3.Text) && (B3.Text == C3.Text) && (!A3.Enabled)) winner = true;
//Digonal check
else if ((A1.Text == B2.Text) && (B2.Text == C3.Text) && (!A1.Enabled)) winner = true;
else if ((A3.Text == B2.Text) && (B2.Text == C1.Text) && (!C1.Enabled)) winner = true;
///////////////////////////////////////////////////////////////////////
if (winner)
{
disablebutton();
string w = "";
if (turn) w = "O";
else w = "X";
MessageBox.Show(w + " is winner", "Tic Tac Toe Result");
}
else if (turn_count == 9)
{
MessageBox.Show("Match is Draw", "Result");
}
}
private void disablebutton()
{
try
{
foreach(Control c in Controls)
{
Button b = (Button) c;
b.Enabled = false;
}
} catch {}
}
private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
{
turn = true;
turn_count = 0;
try
{
foreach(Control c in Controls)
{
Button b = (Button) c;
b.Enabled = true;
b.Text = "";
}
}
catch {}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("In tic Tac Toe ,when a sign match horizently,vertically or digonals,it wins.", "About");
}
}
}Tic Tac Toe is a Game that can be played by anybody.
