Friday, 21 February 2014

TIC TAC TOE

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 WindowsFormsApplication1
{
    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) ? "O" : "X";
         
           // if (count % 2 == 0)
           //{
           //    //b.BackColor = Color.Red;
           //    b.ForeColor = Color.Red;
             
           //}
           //else
           //{
           //    b.BackColor = Color.Blue;
           //}
           b.Enabled = false;
           count++;
            check();
        }


        void check()
        {
            if (button1.Text == button2.Text && button2.Text == button3.Text && button1.Text != "")
            {
                result(button1.Text);
            }
             if (button1.Text == button4.Text && button4.Text == button7.Text && button1.Text != "")
            {
                result(button1.Text);
            }
             if (button4.Text == button5.Text && button5.Text == button6.Text && button4.Text != "")
            {
                result(button4.Text);
            }
             if (button7.Text == button8.Text && button8.Text == button9.Text && button7.Text != "")
            {
                result(button7.Text);
            }
             if (button2.Text == button5.Text && button5.Text == button8.Text && button2.Text != "")
            {
                result(button2.Text);
            }
             if (button3.Text == button6.Text && button6.Text == button9.Text && button3.Text != "")
            {
                result(button3.Text);
            }
             if (button1.Text == button5.Text && button5.Text == button9.Text && button1.Text != "")
            {
                result(button1.Text);
            }
             if (button3.Text == button5.Text && button5.Text == button7.Text && button3.Text != "")
            {
                result(button3.Text);
            }

        }
        void result( string txt)
        {
            if (txt=="O")
            {
                MessageBox.Show("Second Player Win");
            }
            else
            {
                MessageBox.Show("First Player Win");
            }
           
        }

        private void button10_Click(object sender, EventArgs e)
        {
             reset();
        }
     
        void reset()
        {
            foreach (Button b in this.Controls)
            {
                b.Text = "";
                b.Enabled = true;
                b.BackColor = Control.DefaultBackColor;
                button10.Text = "Reset";
                button11.Text = "Play Again";

            }
            count = 1;
        }

        private void button11_Click(object sender, EventArgs e)
        {
           
            if (MessageBox.Show("Are You Want To Play Again!"," "  , MessageBoxButtons.YesNo) == DialogResult.Yes)

           
            {

                reset();
            }
            else
            {
                Form1 frm = new Form1();
                frm.Focus();

            }
        }



        }
   
}


No comments:

Post a Comment