2014年12月3日 星期三

C# 圈叉遊戲

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;
using System.Threading;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        int c = 0;
        int r;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button1.Text = "";
            button2.Text = "";
            button3.Text = "";
            button4.Text = "";
            button5.Text = "";
            button6.Text = "";
            button7.Text = "";
            button8.Text = "";
            button9.Text = "";
            //按鈕1~9字串是空白

        }
        private void button1_Click_1(object sender, EventArgs e)
        {
            r = c % 2;
            c = c + 1;
            if (r == 0)
            {
                button1.Text = "O";

            }
            else
            {
                button1.Text = "X";
            }
            textBox1.Text = Convert.ToString(r);
            button1.Enabled = false;
            if (button1.Text == button2.Text & button2.Text == button3.Text & button3.Text == button1.Text)
            {
                if(r==0)
                MessageBox.Show("O win");
                else
                    MessageBox.Show("X win");
            }
            if (button1.Text == button4.Text & button4.Text == button7.Text & button7.Text == button1.Text)
            {
                if (r == 0)
                    MessageBox.Show("O win");
                else if (r == 1)
                    MessageBox.Show("X win");
            }
            if (button1.Text == button5.Text & button5.Text == button9.Text & button9.Text == button1.Text)
            {
                if (r == 0)
                    MessageBox.Show("O win");
                else if(r == 1)
                    MessageBox.Show("X win");
            }
        }
        private void button2_Click_1(object sender, EventArgs e)
        {
            r = c % 2;
            c = c + 1;
            if (r == 0)
            {
                button2.Text = "O";
            }
            else
            {
                button2.Text = "X";
            }
            textBox1.Text = Convert.ToString(r);
            button2.Enabled = false;
            if (button1.Text == button2.Text & button2.Text == button3.Text & button3.Text == button1.Text)
            {
                if (r == 0)
                    MessageBox.Show("O win");
                else if(r == 1)
                    MessageBox.Show("X win");
            }
            if (button2.Text == button5.Text & button5.Text == button8.Text & button8.Text == button2.Text)
            {
                if (r == 0)
                    MessageBox.Show("O win");
                else if (r == 1)
                    MessageBox.Show("X win");
            }
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            r = c % 2;
            c = c + 1;
            if (r == 0)
            {
                button3.Text = "O";
            }
            else
            {
                button3.Text = "X";
            }
            textBox1.Text = Convert.ToString(r);
            button3.Enabled = false;
            if (button1.Text == button2.Text & button2.Text == button3.Text & button3.Text == button1.Text)
            {
                if (r == 0)
                    MessageBox.Show("O win");
                else if (r == 1)
                    MessageBox.Show("X win");
            }
            if (button3.Text == button6.Text & button6.Text == button9.Text & button9.Text == button3.Text)
            {
                if (r == 0)
                    MessageBox.Show("O win");
                else if (r == 1)
                    MessageBox.Show("X win");
            }
            if (button3.Text == button5.Text & button5.Text == button7.Text & button7.Text == button3.Text)
            {
                if (r == 0)
                    MessageBox.Show("O win");
                else if (r == 1)
                    MessageBox.Show("X win");
            }
        }

        private void button4_Click_1(object sender, EventArgs e)
        {
            r = c % 2;
            c = c + 1;
            if (r == 0)
            {
                button4.Text = "O";
            }
            else
            {
                button4.Text = "X";
            }
            textBox1.Text = Convert.ToString(r);
            button4.Enabled = false;
            if (button1.Text == button4.Text & button4.Text == button7.Text & button7.Text == button1.Text)
            {
                if (r == 0)
                    MessageBox.Show("O win");
                else if (r == 1)
                    MessageBox.Show("X win");
            }
            if (button4.Text == button5.Text & button5.Text == button6.Text & button6.Text == button4.Text)
            {
                if (r == 0)
                    MessageBox.Show("O win");
                else if (r == 1)
                    MessageBox.Show("X win");
            }
        }

        private void button5_Click_1(object sender, EventArgs e)
        {
            r = c % 2;
            c = c + 1;
            if (r == 0)
            {
                button5.Text = "O";
            }
            else
            {
                button5.Text = "X";
            }
            textBox1.Text = Convert.ToString(r);
            button5.Enabled = false;
            if (button1.Text == button5.Text & button5.Text == button9.Text & button9.Text == button1.Text)
            {
                if (r == 0)
                    MessageBox.Show("O win");
                else if (r == 1)
                    MessageBox.Show("X win");
            }
            if (button2.Text == button5.Text & button5.Text == button8.Text & button8.Text == button2.Text)
            {
                if (r == 0)
                    MessageBox.Show("O win");
                else if (r == 1)
                    MessageBox.Show("X win");
            }
            if (button3.Text == button5.Text & button5.Text == button7.Text & button7.Text == button3.Text)
            {
                if (r == 0)
                    MessageBox.Show("O win");
                else if (r == 1)
                    MessageBox.Show("X win");
            }
            if (button4.Text == button5.Text & button5.Text == button6.Text & button6.Text == button4.Text)
            {
                if (r == 0)
                    MessageBox.Show("O win");
                else if (r == 1)
                    MessageBox.Show("X win");
            }
        }

        private void button6_Click_1(object sender, EventArgs e)
        {
            r = c % 2;
            c = c + 1;
            if (r == 0)
            {
                button6.Text = "O";
            }
            else
            {
                button6.Text = "X";
            }
            textBox1.Text = Convert.ToString(r);
            button6.Enabled = false;
            if (button4.Text == button5.Text & button5.Text == button6.Text & button6.Text == button4.Text)
            {
                if (r == 0)
                    MessageBox.Show("O win");
                else if (r == 1)
                    MessageBox.Show("X win");
            }
            if (button3.Text == button6.Text & button6.Text == button9.Text & button9.Text == button3.Text)
            {
                if (r == 0)
                    MessageBox.Show("O win");
                else if (r == 1)
                    MessageBox.Show("X win");
            }
        }

        private void button7_Click_1(object sender, EventArgs e)
        {
            r = c % 2;
            c = c + 1;
            if (r == 0)
            {
                button7.Text = "O";
            }
            else
            {
                button7.Text = "X";
            }
            textBox1.Text = Convert.ToString(r);
            button7.Enabled = false;
            if (button1.Text == button4.Text & button4.Text == button7.Text & button7.Text == button1.Text)
            {
                if (r == 0)
                    MessageBox.Show("O win");
                else if (r == 1)
                    MessageBox.Show("X win");
            }
            
            if (button3.Text == button5.Text & button5.Text == button7.Text & button7.Text == button3.Text)
            {
                if (r == 0)
                    MessageBox.Show("O win");
                else if (r == 1)
                    MessageBox.Show("X win");
            }
            if (button7.Text == button8.Text & button8.Text == button9.Text & button9.Text == button7.Text)
            {
                if (r == 0)
                    MessageBox.Show("O win");
                else if (r == 1)
                    MessageBox.Show("X win");
            }

        }

        private void button8_Click_1(object sender, EventArgs e)
        {
            r = c % 2;
            c = c + 1;
            if (r == 0)
            {
                button8.Text = "O";
            }
            else
            {
                button8.Text = "X";
            }
            textBox1.Text = Convert.ToString(r);
            button8.Enabled = false;
            if (button2.Text == button5.Text & button5.Text == button8.Text & button8.Text == button2.Text)
            {
                if (r == 0)
                    MessageBox.Show("O win");
                else if (r == 1)
                    MessageBox.Show("X win");
            }
            if (button7.Text == button8.Text & button8.Text == button9.Text & button9.Text == button7.Text)
            {
                if (r == 0)
                    MessageBox.Show("O win");
                else if (r == 1)
                    MessageBox.Show("X win");
            }

        }

        private void button9_Click_1(object sender, EventArgs e)
        {
            r = c % 2;
            c = c + 1;
            if (r == 0)
            {
                button9.Text = "O";
            }
            else
            {
                button9.Text = "X";
            }
            textBox1.Text = Convert.ToString(r);
            button9.Enabled = false;
            if (button1.Text == button5.Text & button5.Text == button9.Text & button9.Text == button1.Text)
            {
                if (r == 0)
                    MessageBox.Show("O win");
                else if (r == 1)
                    MessageBox.Show("X win");
            }
            if (button3.Text == button6.Text & button6.Text == button9.Text & button9.Text == button3.Text)
            {
                if (r == 0)
                    MessageBox.Show("O win");
                else if (r == 1)
                    MessageBox.Show("X win");
            }
            if (button7.Text == button8.Text & button8.Text == button9.Text & button9.Text == button7.Text)
            {
                if (r == 0)
                    MessageBox.Show("O win");
                else if (r == 1)
                    MessageBox.Show("X win");
            }
        }
    }

}

沒有留言:

張貼留言