2014年12月5日 星期五

第十一周-亂數不重複

(1)

(2)
 (3)


//===========================================================
        static int n = 4; // 按鈕數量總共有 n*n 個
        Button[,] buttons = new Button[n,n];
        Button rnd_button = new Button();
        System.Random r = new System.Random();
        private void Form1_Load(object sender, EventArgs e)
        {
            //--Form------------
            this.Width = 1000;
            this.Height = 1000;
            //--Buttons---------
            int button_width = 50, button_height = 50,name=0;
            for (int i = 0; i < n; i++) for (int j = 0; j < n; j++)
                {
                    name++;
                    buttons[i, j] = new Button();
                    buttons[i, j].Location = new Point(j * 50, i * 50);
                    buttons[i, j].Size = new Size(button_width, button_height);
                    buttons[i, j].Text = name.ToString();
                    this.Controls.Add(buttons[i, j]);  //Show button
                }
            //--rnd_Button-----
            rnd_button.Location = new Point(300, 0);
            rnd_button.Text = "重新排列";
            rnd_button.Click += rnd_button_Click;
            this.Controls.Add(rnd_button);
        }
        void rnd_button_Click(object sender, EventArgs e)
        {
            //--Setting Random name--
            string[] Rnd_name = new string[n * n + 1];
            for (int i = 1; i <= n * n; i++) Rnd_name[i] = i.ToString();
            for (int i = 1; i <= n * n; i++)
            {
                string tmp = ""; int rnd = r.Next(1, n * n);
                tmp = Rnd_name[Rnd_name.Length - 1];
                Rnd_name[Rnd_name.Length - 1] = Rnd_name[rnd];
                Rnd_name[rnd] = tmp;
            }
            //--Rename--
            for (int i = 0, k = 1; i < n; i++)
                for (int j = 0; j < n; j++)
                {
                    buttons[i, j].Text = Rnd_name[k];
                    k++;
                }
            //------------
        }
        //===========================================================

2014年10月24日 星期五

第四周-圈圈叉叉

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 WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text != "O")
            {
                button1.Text = "O";
                if (defined()) MessageBox.Show("O win");
            }
            else
            {
                button1.Text = "X";
                if (defined()) MessageBox.Show("X win");
            }
            //----------------------------------------------------------------
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (button2.Text != "O")
            {
                button2.Text = "O";
                if (defined()) MessageBox.Show("O win");
            }
            else
            {
                button2.Text = "X";
                if (defined()) MessageBox.Show("X win");
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (button3.Text != "O")
            {
                button3.Text = "O";
                if (defined()) MessageBox.Show("O win");
            }
            else
            {
                button3.Text = "X";
                if (defined()) MessageBox.Show("X win");
            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            if (button4.Text != "O")
            {
                button4.Text = "O";
                if (defined()) MessageBox.Show("O win");
            }
            else
            {
                button4.Text = "X";
                if (defined()) MessageBox.Show("X win");
            }
        }
        private void button5_Click(object sender, EventArgs e)
        {
            if (button5.Text != "O")
            {
                button5.Text = "O";
                if (defined()) MessageBox.Show("O win");
            }
            else
            {
                button5.Text = "X";
                if (defined()) MessageBox.Show("X win");
            }
        }
        private void button6_Click(object sender, EventArgs e)
        {
            if (button6.Text != "O")
            {
                button6.Text = "O";
                if (defined()) MessageBox.Show("O win");
            }
            else
            {
                button6.Text = "X";
                if (defined()) MessageBox.Show("X win");
            }
        }
        private void button7_Click(object sender, EventArgs e)
        {
            if (button7.Text != "O")
            {
                button7.Text = "O";
                if (defined()) MessageBox.Show("O win");
            }
            else
            {
                button7.Text = "X";
                if (defined()) MessageBox.Show("X win");
            }
        }
        private void button8_Click(object sender, EventArgs e)
        {
            if (button8.Text != "O")
            {
                button8.Text = "O";
                if (defined()) MessageBox.Show("O win");
            }
            else
            {
                button8.Text = "X";
                if (defined()) MessageBox.Show("X win");
            }
        }
        private void button9_Click(object sender, EventArgs e)
        {
            if (button9.Text != "O")
            {
                button9.Text = "O";
                if (defined()) MessageBox.Show("O win");
            }
            else
            {
                button9.Text = "X";
                if (defined()) MessageBox.Show("X win");
            }
        }
        public bool defined()
        {
            bool A1 = (button1.Text == button2.Text && button2.Text == button3.Text && button3.Text == button1.Text);
            bool A2 = (button4.Text == button5.Text && button4.Text == button6.Text && button5.Text == button6.Text);
            bool A3 = (button7.Text == button8.Text && button7.Text == button9.Text && button7.Text == button8.Text);
            bool B1 = (button1.Text == button4.Text && button4.Text == button7.Text && button1.Text == button7.Text);
            bool B2 = (button2.Text == button5.Text && button5.Text == button8.Text && button7.Text == button8.Text);
            bool B3 = (button3.Text == button6.Text && button6.Text == button9.Text && button3.Text == button9.Text);
            bool C1 = (button1.Text == button5.Text && button5.Text == button9.Text && button1.Text == button9.Text);
            bool C2 = (button3.Text == button5.Text && button5.Text == button7.Text && button7.Text == button3.Text);
            return (A1 || A2 || A3 || B1 || B2 || B3 || C1 || C2);
        }
    }
}

第三週 丟骰子兩匹馬賽跑


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
    {
        public Form1()
        {
            InitializeComponent();
        }
        Label msg = new Label();
        Button play1 = new Button();
        Button play2 = new Button();
        Random rnd = new Random();
        private void Form1_Load(object sender, EventArgs e)
        {
            //=====================
            // Form
            this.Size = new Size(500, 500);
            // msg
            msg.Location = new Point(30, 30);
            msg.Text = "Position";
            msg.AutoSize = true;
            this.Controls.Add(msg);
            // button
            play1.Text = "play1";
            play1.Location = new Point(0, 100);
            play1.Click += new EventHandler(play1_Click);

            this.Controls.Add(play1);
            //
            play2.Text = "play2";
            play2.Location = new Point(0, 150);
            play2.Click += new EventHandler(play2_Click);
            this.Controls.Add(play2);
            //===================================
        }
        void play1_Click(object sender, EventArgs e)
        {
            int R1 = rnd.Next(10, 100);
            play1.Location = new Point(play1.Location.X + R1, play1.Location.Y);
            msg.Text = "Position";
            msg.Text += "\n Play1" + play1.Location.ToString();
            msg.Text += "\n Play2" + play2.Location.ToString();
            if (play1.Location.X >= this.Width - 150) MessageBox.Show("Play1 Win!");
        }
        void play2_Click(object sender, EventArgs e)
        {
            int R2 = rnd.Next(10, 100);
            play2.Location = new Point(play2.Location.X + R2, play2.Location.Y);
            msg.Text = "Position";
            msg.Text += "\n Play1" + play1.Location.ToString();
            msg.Text += "\n Play2" + play2.Location.ToString();
            if (play2.Location.X >= this.Width - 150) MessageBox.Show("Play2 Win!");
        }
    }
}

2014年9月19日 星期五

第一周:移動按鈕&Unity

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 第一周_移動按鈕
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button1.Text = "移動";
            button2.Text = "車子";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (button2.Location.ToString() == "{X=20,Y=20}")
                button2.Location = new Point(40, 40);
            else button2.Location = new Point(20, 20);
        }
    }
}