2015年10月30日 星期五

推盤

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        Button[,] buttons = new Button[9, 9];     //宣告一個二維陣列  (用來製造4x4按鈕)
        int[] randomize = new int[16];                //宣告一個一維陣列  (用來存取變數的陣列)
        Random rnd = new Random();                //宣告產生亂數
        int x, y;
        public Form1()
        {

            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    buttons[i, j] = new Button();                  //利用二維陣列產生4x4按鈕
                    buttons[i, j].Location = new Point(i * 100, j * 100);     //改變其按鈕位置
                    buttons[i, j].Size = new Size(100, 100);            //改變按鈕大小    
                    this.Controls.Add(buttons[i, j]);     //要產生按鈕一定要有這行
                    buttons[i, j].Click += new EventHandler(Button1_Click);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 16; i++)
            {

                randomize[i] = rnd.Next(0, 16);        //將0~15個亂數依序放進一維陣列randomize中

                for (int j = 0; j < i; j++)
                {
                    while (randomize[j] == randomize[i])         // 檢查是否有重複的亂數 如果有就重新產生
                    {
                        j = 0;
                        randomize[i] = rnd.Next(0, 16);
                    }
                }
            }

            for (int i = 0; i < 4; ++i)
            {

                for (int j = 0; j < 4; ++j)
                {
                    buttons[i, j].Text = Convert.ToString(randomize[i * 4 + j]);            //將一維陣列randomize依序放進二維陣列的按鈕上
                    this.Controls.Add(buttons[i, j]);
                    if (buttons[i, j].Text == "0")
                    {
                        buttons[i, j].Text = "";
                        x = i;
                        y = j;
                    }
                }
            }
        }
            private void Button1_Click(object sender, EventArgs e)
            {
            string a;
            if (x > 0)
            {
                if (sender == buttons[x - 1, y])
                {
                    a = buttons[x - 1, y].Text;
                    buttons[x - 1, y].Text = buttons[x, y].Text;
                    buttons[x, y].Text = a;
                    x = x - 1;
                }
            }
            if (x < 3)
            {
                if (sender == buttons[x + 1, y])
                {
                    a = buttons[x + 1, y].Text;
                    buttons[x + 1, y].Text = buttons[x, y].Text;
                    buttons[x, y].Text = a;
                    x = x + 1;
                }
            }
            if (y > 0)
            {
                if (sender == buttons[x, y - 1])
                {
                    a = buttons[x, y - 1].Text;
                    buttons[x, y - 1].Text = buttons[x, y].Text;
                    buttons[x, y].Text = a;
                    y = y - 1;
                }
            }
            if (y < 3)
            {
                if (sender == buttons[x, y + 1])
                {
                    a = buttons[x, y + 1].Text;
                    buttons[x, y + 1].Text = buttons[x, y].Text;
                    buttons[x, y].Text = a;
                    y = y + 1;
                }
        }
    }
}
}

沒有留言:

張貼留言