2014/10/24

10/24第五週 圈叉遊戲

10/24第五週  圈叉遊戲

首先,利用工具箱的button功能建立9個按鈕以及建立一個label

然後在public partial class Form1 : Form裏頭輸入int c;

再來進入表單程式輸入 button1.Text = "";
依樣畫葫蘆地把另外8個也輸入進去

最後,在每個按鈕的程式裡宣告變數以及假設條件
即可完成圈叉遊戲

Ex:
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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        int c;

        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 = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
            label1.Text = Convert.ToString(r);

            if (r == 1)
                button1.Text = "O";
            else
                button1.Text = "X";

            button1.Enabled = false;

            if(button1.Text==button2.Text&button2.Text==button3.Text&button1.Text==button3.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button1.Text == button4.Text & button4.Text == button7.Text & button1.Text == button7.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button1.Text == button5.Text & button5.Text == button9.Text & button1.Text == button9.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
            label1.Text = Convert.ToString(r);

            if (r == 1)
                button2.Text = "O";
            else
                button2.Text = "X";

            button2.Enabled = false;

            if (button1.Text == button2.Text & button2.Text == button3.Text & button1.Text == button3.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button2.Text == button5.Text & button5.Text == button8.Text & button2.Text == button8.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
            label1.Text = Convert.ToString(r);

            if (r == 1)
                button3.Text = "O";
            else
                button3.Text = "X";

            button3.Enabled = false;

            if (button1.Text == button2.Text & button2.Text == button3.Text & button1.Text == button3.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button3.Text == button6.Text & button6.Text == button9.Text & button3.Text == button9.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button3.Text == button5.Text & button5.Text == button7.Text & button3.Text == button7.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
            label1.Text = Convert.ToString(r);

            if (r == 1)
                button4.Text = "O";
            else
                button4.Text = "X";

            button4.Enabled = false;

            if (button4.Text == button5.Text & button5.Text == button6.Text & button4.Text == button6.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button1.Text == button4.Text & button4.Text == button7.Text & button1.Text == button7.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }

        }

        private void button5_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
            label1.Text = Convert.ToString(r);

            if (r == 1)
                button5.Text = "O";
            else
                button5.Text = "X";

            button5.Enabled = false;

            if (button4.Text == button5.Text & button5.Text == button6.Text & button4.Text == button6.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button2.Text == button5.Text & button5.Text == button8.Text & button2.Text == button8.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button1.Text == button5.Text & button5.Text == button9.Text & button1.Text == button9.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button3.Text == button5.Text & button5.Text == button7.Text & button3.Text == button7.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }

        }

        private void button6_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
            label1.Text = Convert.ToString(r);

            if (r == 1)
                button6.Text = "O";
            else
                button6.Text = "X";

            button6.Enabled = false;

            if (button4.Text == button5.Text & button5.Text == button6.Text & button4.Text == button6.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button3.Text == button6.Text & button6.Text == button9.Text & button3.Text == button9.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
            label1.Text = Convert.ToString(r);

            if (r == 1)
                button7.Text = "O";
            else
                button7.Text = "X";

            button7.Enabled = false;

            if (button7.Text == button8.Text & button8.Text == button9.Text & button7.Text == button9.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button1.Text == button4.Text & button4.Text == button7.Text & button1.Text == button7.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button3.Text == button5.Text & button5.Text == button7.Text & button3.Text == button7.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }

        }

        private void button8_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
            label1.Text = Convert.ToString(r);

            if (r == 1)
                button8.Text = "O";
            else
                button8.Text = "X";

            button8.Enabled = false;

            if (button7.Text == button8.Text & button8.Text == button9.Text & button7.Text == button9.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button2.Text == button5.Text & button5.Text == button8.Text & button2.Text == button8.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }

        }

        private void button9_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
            label1.Text = Convert.ToString(r);

            if (r == 1)
                button9.Text = "O";
            else
                button9.Text = "X";

            button9.Enabled = false;

            if (button7.Text == button8.Text & button8.Text == button9.Text & button7.Text == button9.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button3.Text == button6.Text & button6.Text == button9.Text & button3.Text == button9.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button1.Text == button5.Text & button5.Text == button9.Text & button1.Text == button9.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }

        }

    }
}










沒有留言:

張貼留言