Wednesday, March 14, 2012

Бидний тоглодог XO тоглоом C# дээр (source)





using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Media;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApplication10
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            for (int i = 0; i < 9; i++)
            {
                isClicked[i] = false;
            }

            textBox3.Text="X Winner";
             textBox1.Text=scrX.ToString() ;
            textBox2.Text=scrO.ToString();

        }

        int scrX = 0;
        int scrO = 0;

        bool X = true;
        bool O = false;

        bool signal = false;
        bool[] isClicked = new bool[9];

        int counter = 0;
        string[] array={"0","0","0","0","0","0","0","0","0"};


        private void winnerX()
        {
            MessageBox.Show("ялагч бол X", "ялагч", MessageBoxButtons.OK, MessageBoxIcon.Information);
            textBox3.Text = "ялагч X. Дахин тоглоно уу  !";
            scrX++;
            textBox1.Text = scrX.ToString();

        }
        private void winner0()
        {
            MessageBox.Show("ялагч O", "ялагч", MessageBoxButtons.OK, MessageBoxIcon.Information);
            textBox3.Text = "ялагч бол O. Дахин тоглоно уу !";
            scrO++;
            textBox2.Text = scrO.ToString();

        }
     

        private void isDrawBattle()
        {

            if ((counter == 9) && !(CheckForWin()))
            {
                MessageBox.Show("Тоглолт тэнцлээ", "ялагч", MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBox3.Text = "Тоглолт тэнцлээ";
            }

        }

     

       private void button1_Click(object sender, EventArgs e)
        {
            int button = 0;

            if (signal)
            {
                return;
            }

            if (isClicked[0])
            {
                return;
            }
         
            if (X && (counter < 9))
            {
                textBox3.Text="Одоо X дарна !";
                button1.Text="X";
                array[button] = "x";
                X = false;
                O = true;
                if (!signal)
                {
                    textBox3.Text = "Одоо 0 дарна";
                }
            }
            else if (O && (counter < 9))
            {
                textBox3.Text = "Одоо 0 дарна";
                button1.Text = "O";
                array[button] = "o";
                O = false;
                X = true;
                if (!signal)
                {
                    textBox3.Text = "Одоо X дарна";
                }
            }
            else return;
            counter++;

            isClicked[0] = true;

            if (CheckForWin())
            {
                return;
            }

            isDrawBattle();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int button = 1;

            if (signal)
            {
                return;
            }
            if (isClicked[1])
            {
                return;
            }

            if (X && (counter < 9))
            {
                textBox3.Text = "Одоо X дарна";
                button2.Text= "X";
                array[button] = "x";
                X = false;
                O = true;
                if (!signal)
                {
                    textBox3.Text = "Одоо 0 дарна";
                }
            }
            else if (O && (counter < 9))
            {
                textBox3.Text = "Одоо 0 дарна!";
                button2.Text = "O";
                array[button] = "o";
                O = false;
                X = true;
                if (!signal)
                {
                    textBox3.Text = "Одоо X дарна";
                }
            }
            else return;
            counter++;
            isClicked[1] = true;

            if (CheckForWin())
            {
                return;
            }

            isDrawBattle();
        }

        private void button3_Click(object sender, EventArgs e)
        {
             int button = 2;

            if (signal)
            {
                return;
            }
            if (isClicked[2])
            {
                return;
            }

            if (X && (counter < 9))
            {
                textBox3.Text = "Одоо X дарна";
                button3.Text = "X";
                array[button] = "x";
                X = false;
                O = true;
                if (!signal)
                {
                    textBox3.Text = "Одоо 0 дарна";
                }
            }
            else if (O && (counter < 9))
            {
                textBox3.Text = "Одоо 0 дарна";
                button3.Text = "O";
                array[button] = "o";
                O = false;
                X = true;
                if (!signal)
                {
                    textBox3.Text = "Одоо X дарна";
                }
            }
            else return;
            counter++;
            isClicked[2] = true;

            if (CheckForWin())
            {
                return;
            }

            isDrawBattle();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int button = 3;

            if (signal)
            {
                return;
            }
            if (isClicked[3])
            {
                return;
            }

            if (X && (counter < 9))
            {
                textBox3.Text = "Одоо X дарна";
                button4.Text = "X";
                array[button] = "x";
                X = false;
                O = true;
                if (!signal)
                {
                    textBox3.Text = "Одоо 0 дарна";
                }
            }
            else if (O && (counter < 9))
            {
                textBox3.Text = "Одоо 0 дарна";
                button4.Text = "O";
                array[button] = "o";
                O = false;
                X = true;
                if (!signal)
                {
                    textBox3.Text = "Одоо X дарна";
                }
            }
            else return;
            counter++;
            isClicked[3] = true;

            if (CheckForWin())
            {
                return;
            }
            isDrawBattle();
        }

        private void button5_Click(object sender, EventArgs e)
        {
             int button = 4;

            if (signal)
            {
                return;
            }
            if (isClicked[4])
            {
                return;
            }

            if (X   &&  (counter < 9))
            {
                textBox3.Text = "Одоо X дарна";
                button5.Text = "X";
                array[button] = "x";
                X = false;
                O = true;
                if (!signal)
                {
                    textBox3.Text = "Одоо 0 дарна!";
                }
            }
            else if (O && (counter < 9))
            {
                textBox3.Text = "Одоо 0 дарна";
                button5.Text = "O";
                array[button] = "o";
                O = false;
                X = true;
                if (!signal)
                {
                    textBox3.Text = "Одоо X дарна";
                }
            }
            else return;
            counter++;
            isClicked[4] = true;

            if (CheckForWin())
            {
                return;
            }

            isDrawBattle();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            int button = 5;

            if (signal)
            {
                return;
            }
            if (isClicked[5])
            {
                return;
            }

            if (X && (counter<9))
            {
                textBox3.Text = "Одоо X дарна !";
                button6.Text = "X";
                array[button] = "x";
                X = false;
                O = true;
                if (!signal)
                {
                    textBox3.Text = "Одоо 0 дарна!";
                }
            }
            else if (O && (counter < 9))
            {
                textBox3.Text = "Одоо 0 дарна!";
                button6.Text = "O";
                array[button] = "o";
                O = false;
                X = true;
                if (!signal)
                {
                    textBox3.Text = "Одоо X дарна!";
                }
            }

            else return;
            counter++;
            isClicked[5] = true;

            if (CheckForWin())
            {
                return;
            }

            isDrawBattle();
        }

        private void button7_Click(object sender, EventArgs e)
        {
             int button = 6;

            if (signal)
            {
                return;
            }
            if (isClicked[6])
            {
                return;
            }

            if (X && (counter < 9))
            {
                textBox3.Text = "Одоо X дарна!";
                button7.Text = "X";
                array[button] = "x";
                X = false;
                O = true;
                if (!signal)
                {
                    textBox3.Text = "Одоо 0 дарна !";
                }
            }
            else if (O && (counter < 9))
            {
                textBox3.Text = "Одоо 0 дарна!";
                button7.Text = "O";
                array[button] = "o";
                O = false;
                X = true;
                if (!signal)
                {
                    textBox3.Text = "Одоо X дарна!";
                }
            }
            else return;
            counter++;
            isClicked[6] = true;

            if (CheckForWin())
            {
                return;
            }
            isDrawBattle();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            int button = 7;

            if (signal)
            {
                return;
            }
            if (isClicked[7])
            {
                return;
            }

            if (X && (counter < 9))
            {
                textBox3.Text = "Одоо X дарна !";
                button8.Text = "X";
                array[button] = "x";
                X = false;
                O = true;
                if (!signal)
                {
                    textBox3.Text = "Одоо 0 дарна !";
                }
            }
            else if (O && (counter < 9))
            {
                textBox3.Text = "Одоо 0 дарна !";
                button8.Text = "O";
                array[button] = "o";
                O = false;
                X = true;
                if (!signal)
                {
                    textBox3.Text = "Одоо X дарна !";
                 
                }
            }
            else return;
            counter++;
            isClicked[7] = true;

            if (CheckForWin())
            {
                return;
            }
            isDrawBattle();
        }

        private void button9_Click(object sender, EventArgs e)
        {
        int button = 8;

            if (signal)
            {
                return;
            }
            if (isClicked[8])
            {
                return;
            }

            if (X && (counter < 9))
            {
                textBox3.Text = "Одоо X дарна !";
                button9.Text = "X";
                array[button] = "x";
                X = false;
                O = true;
                if (!signal)
                {
                    textBox3.Text = "Одоо 0 дарна !";
                }
            }
            else if (O && (counter < 9))
            {
                textBox3.Text = "NОдоо 0 дарна!";
                button9.Text = "O";
                array[button] = "o";
                O = false;
                X = true;
                if (!signal)
                {
                    textBox3.Text = "Одоо X дарна !";
                }
            }
            else return;
            counter++;
            isClicked[8] = true;

            if (CheckForWin())
            {
                return;
            }
            isDrawBattle();
        }

        private void button10_Click(object sender, EventArgs e)
        {
       button1.Text=button2.Text=button3.Text=button4.Text=button5.Text=button6.Text=button7.Text=button8.Text=button9.Text= "";        
            for (int i = 0; i < 9; i++)
            {
                isClicked[i] = false;
            }

            textBox3.Text = "X эхлүүлнэ!";
            counter = 0;
            signal = false;

            X = true;
            O = false;

            for (int i = 0; i < 9; i++)
            {
                array[i] = "0";
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            scrX = scrO = 0;
            textBox1.Text = scrX.ToString();
            textBox2.Text = scrO.ToString();
            textBox3.Text = "Хожсон тоог арилгалаа!";
        }
            private bool CheckForWin()
        {
            bool flag = false;

            if ((array[0] == array[1]) && (array[0] == array[2]) && (array[1] == array[2]) && (array[0] == "x"))
            {
                winnerX();
                flag = true;
            }
            else if ((array[0] == array[1]) && (array[0] == array[2]) && (array[1] == array[2]) && (array[0] == "o"))
            {
                winner0();
                flag = true;
            }
            else if ((array[3] == array[4]) && (array[3] == array[5]) && (array[4] == array[5]) && (array[3] == "x"))
            {
                winnerX();
                flag = true;
            }
            else if ((array[3] == array[4]) && (array[3] == array[5]) && (array[4] == array[5]) && (array[3] == "o"))
            {
                winner0();
                flag = true;
            }
            else if ((array[6] == array[7]) && (array[6] == array[8]) && (array[7] == array[8]) && (array[6] == "x"))
            {
                winnerX();
                flag = true;
            }
            else if ((array[6] == array[7]) && (array[6] == array[8]) && (array[7] == array[8]) && (array[6] == "o"))
            {
                winner0();
                flag = true;
            }
            else if ((array[0] == array[3]) && (array[0] == array[6]) && (array[3] == array[6]) && (array[0] == "x"))
            {
                winnerX();
                flag = true;
            }
            else if ((array[0] == array[3]) && (array[0] == array[6]) && (array[3] == array[6]) && (array[0] == "o"))
            {
                winner0();
                flag = true;
            }
            else if ((array[1] == array[4]) && (array[1] == array[7]) && (array[4] == array[7]) && (array[1] == "x"))
            {
                winnerX();
                flag = true;
            }
            else if ((array[1] == array[4]) && (array[1] == array[7]) && (array[4] == array[7]) && (array[1] == "o"))
            {
                winner0();
                flag = true;
            }
            else if ((array[2] == array[5]) && (array[2] == array[8]) && (array[5] == array[8]) && (array[2] == "x"))
            {
                winnerX();
                flag = true;
            }
            else if ((array[2] == array[5]) && (array[2] == array[8]) && (array[5] == array[8]) && (array[2] == "o"))
            {
                winner0();
                flag = true;
            }
            else if ((array[0] == array[4]) && (array[0] == array[8]) && (array[4] == array[8]) && (array[0] == "x"))
            {
                winnerX();
                flag = true;
            }
            else if ((array[0] == array[4]) && (array[0] == array[8]) && (array[4] == array[8]) && (array[0] == "o"))
            {
                winner0();
                flag = true;
            }
            else if ((array[2] == array[4]) && (array[2] == array[6]) && (array[4] == array[6]) && (array[2] == "x"))
            {
                winnerX();
                flag = true;
            }
            else if ((array[2] == array[4]) && (array[2] == array[6]) && (array[4] == array[6]) && (array[2] == "o"))
            {
                winner0();
                flag = true;
            }

            signal = flag;
            return flag;
        }

            private void зохиогчToolStripMenuItem1_Click(object sender, EventArgs e)
            {
                AboutBox1 f2 = new AboutBox1();
                f2.Show();
            }
       }
  }

     
     
     

     
     

     


No comments:

Post a Comment