2014/10/31

10/31第六週 補充圈叉遊戲玩家輸贏判斷

10/31第六週  補充圈叉遊戲玩家輸贏判斷

首先,一樣利用工具箱建立9個button、1個textbox

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

接者,在表單程式裡輸入button1.Text = "";
依此類推輸入另外8個

接下來和上次一樣在每個button內輸入相同宣告、條件來判斷玩家下的是O or X
但是這次不先輸入連線判斷,改用judge();來撰寫
註:judge()可隨意命名,最後宣告物件程式碼相同即可

在程式最後建立 private void judge()
輸入判斷輸贏的假設條件,即可完成程式

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int c = 0;

        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;
            textBox1.Text = Convert.ToString(r);
            if (r == 0)
            {
                button1.Text = "O";
            }
            else
            {
                button1.Text = "X";
            }
            button1.Enabled = false;
            judge();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
            textBox1.Text = Convert.ToString(r);
            if (r == 0)
            {
                button2.Text = "O";
            }
            else
            {
                button2.Text = "X";
            }
            button2.Enabled = false;

            judge();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
            textBox1.Text = Convert.ToString(r);
            if (r == 0)
            {
                button3.Text = "O";
            }
            else
            {
                button3.Text = "X";
            }
            button3.Enabled = false;

            judge();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
            textBox1.Text = Convert.ToString(r);
            if (r == 0)
            {
                button4.Text = "O";
            }
            else
            {
                button4.Text = "X";
            }
            button4.Enabled = false;

            judge();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
            textBox1.Text = Convert.ToString(r);
            if (r == 0)
            {
                button5.Text = "O";
            }
            else
            {
                button5.Text = "X";
            }
            button5.Enabled = false;

            judge();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
            textBox1.Text = Convert.ToString(r);
            if (r == 0)
            {
                button6.Text = "O";
            }
            else
            {
                button6.Text = "X";
            }
            button6.Enabled = false;

            judge();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
            textBox1.Text = Convert.ToString(r);
            if (r == 0)
            {
                button7.Text = "O";
            }
            else
            {
                button7.Text = "X";
            }
            button7.Enabled = false;

            judge();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
            textBox1.Text = Convert.ToString(r);
            if (r == 0)
            {
                button8.Text = "O";
            }
            else
            {
                button8.Text = "X";
            }
            button8.Enabled = false;

            judge();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
            textBox1.Text = Convert.ToString(r);
            if (r == 0)
            {
                button9.Text = "O";
            }
            else
            {
                button9.Text = "X";
            }
            button9.Enabled = false;

            judge();
        }

        private void judge()
        {
            if ((button1.Text == "O" && button2.Text == "O" && button3.Text == "O") ||
               (button4.Text == "O" && button5.Text == "O" && button6.Text == "O") ||
               (button7.Text == "O" && button8.Text == "O" && button9.Text == "O") ||
               (button1.Text == "O" && button4.Text == "O" && button7.Text == "O") ||
               (button2.Text == "O" && button5.Text == "O" && button8.Text == "O") ||
               (button3.Text == "O" && button6.Text == "O" && button9.Text == "O") ||
               (button1.Text == "O" && button5.Text == "O" && button9.Text == "O") ||
               (button3.Text == "O" && button5.Text == "O" && button7.Text == "O"))
                MessageBox.Show("O WIN!");
            else if ((button1.Text == "X" && button2.Text == "X" && button3.Text == "X") ||
                (button4.Text == "X" && button5.Text == "X" && button6.Text == "X") ||
                (button7.Text == "X" && button8.Text == "X" && button9.Text == "X") ||
                (button1.Text == "X" && button4.Text == "X" && button7.Text == "X") ||
                (button2.Text == "X" && button5.Text == "X" && button8.Text == "X") ||
                (button3.Text == "X" && button6.Text == "X" && button9.Text == "X") ||
                (button1.Text == "X" && button5.Text == "X" && button9.Text == "X") ||
                (button3.Text == "X" && button5.Text == "X" && button7.Text == "X"))
                MessageBox.Show("X WIN!!");
        }
    }
}









沒有留言:

張貼留言