Tic Tac Toe (Game)

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. namespace final_tic  
  10. {  
  11.     public partial class Form1: Form   
  12.     {  
  13.         bool turn = true;  
  14.         int turn_count = 0;  
  15.         public Form1()   
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.         private void button_Click(object sender, EventArgs e)   
  20.         {  
  21.             Button b = (Button) sender;  
  22.             if (turn) b.Text = "X";  
  23.             else b.Text = "O";  
  24.             b.Enabled = false;  
  25.             turn = !turn;  
  26.             turn_count++;  
  27.             check_winner();  
  28.         }  
  29.         private void check_winner()   
  30.         {  
  31.             bool winner = false;  
  32.             //horizental check  
  33.             if ((A1.Text == A2.Text) && (A2.Text == A3.Text) && (!A1.Enabled)) winner = true;  
  34.             else if ((B1.Text == B2.Text) && (B2.Text == B3.Text) && (!B1.Enabled)) winner = true;  
  35.             else if ((C1.Text == C2.Text) && (C2.Text == C3.Text) && (!C1.Enabled)) winner = true;  
  36.             //Vertical check  
  37.             else if ((A1.Text == B1.Text) && (B1.Text == C1.Text) && (!A1.Enabled)) winner = true;  
  38.             else if ((A2.Text == B2.Text) && (B2.Text == C2.Text) && (!A2.Enabled)) winner = true;  
  39.             else if ((A3.Text == B3.Text) && (B3.Text == C3.Text) && (!A3.Enabled)) winner = true;  
  40.             //Digonal check  
  41.             else if ((A1.Text == B2.Text) && (B2.Text == C3.Text) && (!A1.Enabled)) winner = true;  
  42.             else if ((A3.Text == B2.Text) && (B2.Text == C1.Text) && (!C1.Enabled)) winner = true;  
  43.             ///////////////////////////////////////////////////////////////////////  
  44.             if (winner)   
  45.             {  
  46.                 disablebutton();  
  47.                 string w = "";  
  48.                 if (turn) w = "O";  
  49.                 else w = "X";  
  50.                 MessageBox.Show(w + " is winner""Tic Tac Toe Result");  
  51.             }   
  52.             else if (turn_count == 9)   
  53.             {  
  54.                 MessageBox.Show("Match is Draw""Result");  
  55.             }  
  56.         }  
  57.         private void disablebutton()   
  58.         {  
  59.             try   
  60.             {  
  61.                 foreach(Control c in Controls)   
  62.                 {  
  63.                     Button b = (Button) c;  
  64.                     b.Enabled = false;  
  65.                 }  
  66.             } catch {}  
  67.         }  
  68.         private void newGameToolStripMenuItem_Click(object sender, EventArgs e)   
  69.         {  
  70.             turn = true;  
  71.             turn_count = 0;  
  72.             try   
  73.             {  
  74.                 foreach(Control c in Controls)   
  75.                 {  
  76.                     Button b = (Button) c;  
  77.                     b.Enabled = true;  
  78.                     b.Text = "";  
  79.                 }  
  80.             }   
  81.             catch {}  
  82.         }  
  83.         private void exitToolStripMenuItem_Click(object sender, EventArgs e)   
  84.         {  
  85.             Application.Exit();  
  86.         }  
  87.         private void aboutToolStripMenuItem_Click(object sender, EventArgs e)   
  88.         {  
  89.             MessageBox.Show("In tic Tac Toe ,when a sign match horizently,vertically or digonals,it wins.""About");  
  90.         }  
  91.     }  
  92. }