2014年12月18日 星期四

2014/12/19

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 _123
{
    public partial class Form1 : Form
    {
        public System.Windows.Forms.Button Button1;
        Random rand = new Random();
        Button[,] buttons = new Button[10, 10];
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Button1 = new System.Windows.Forms.Button();
            Button1.Location = new Point(400, 400);
            Button1.Size = new Size(100, 30);
            Button1.Click += new EventHandler(Button1_Click);
            Button1.Text = "Rand";
            this.Controls.Add(Button1);

            for (int j = 1; j < 5; j++)
            {
                for (int i = 1; i < 5; i++)
                {
                    buttons[i, j] = new Button();
                    buttons[i, j].Location = new Point(80 + i * 50, 80 + j * 50);
                    buttons[i, j].Size = new Size(50, 50);
                    buttons[i, j].Text = Convert.ToString((i - 1) + 4 * (j - 1));
                    this.Controls.Add(buttons[i, j]);
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int x, y, a;
            int[] myarray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
            for (int i = 15; i > 1; i--)
            {

                x = rand.Next(0, i);
                y = myarray[x];
                myarray[x] = myarray[i];
                myarray[i] = y;

            }
            for (int j = 1; j < 5; j++)
            {
                for (int i = 1; i < 5; i++)
                {
                    a = myarray[(j - 1) * 4 + (i - 1)];
                    buttons[i, j].Text = Convert.ToString(a);
                    buttons[i, j].Click += new EventHandler(a_Click);
                    this.Controls.Add(buttons[i, j]);
                }
            }

        }
        //int m, n;
        protected void a_Click(object sender, EventArgs e)
        {
           
            for (int i = 1; i < 5; i++)
            {
                for (int j = 1; j < 5; j++)
                {
                    if (Convert.ToDouble(buttons[i, j].Text) == 0)
                    {
                        buttons[i - 1, j].Click += new EventHandler(b_Click);
                        buttons[i + 1, j].Click += new EventHandler(b_Click);
                        buttons[i, j - 1].Click += new EventHandler(b_Click);
                        buttons[i, j + 1].Click += new EventHandler(b_Click);
                        //i = m;
                        //j = n;
                        //this.Controls.Add(buttons[i, j]);
                        //if (sender == buttons[i, j])
                        //MessageBox.Show("(" + Convert.ToString(i) + "," + Convert.ToString(j) + ")");

                    }
                }
            }
        }
        protected void b_Click(object sender, EventArgs e)
        {
            //buttons[m, n].Text = buttons[m - 1, n].Text;
            //buttons[m-1, n].Text = buttons[m , n].Text;
            MessageBox.Show("  ");
        }

    }
}
       
   

沒有留言:

張貼留言