2014年12月12日 星期五

C# 陣列隨機亂數

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 width, height,x;
        Button[,] buttons = new Button[10, 10];
        int[] array = new int[17];

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {



            // Instantiating all the buttons in the array



            for (int i = 1; i < 17; i++)
            {
                array[i] = i;
            }



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


            /*
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    buttons[i, j] = new Button();
                    buttons[i, j].Location = new Point(i * 50, j * 50);
                    buttons[i,j].Text = j.ToString();
                    this.Controls.Add(buttons[i, j]);
                }
            }

            */

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            int d1, tmp, i;

            Random irand = new Random(Guid.NewGuid().GetHashCode());
            d1 = irand.Next(1, 16);
            label1.Text = d1.ToString();


            for (int j = 1; j < 16; j++)
            {
                i = 15 - j + 1;
                tmp = array[d1];
                array[d1] = array[i];
                array[i] =tmp ;
            }

            buttons[1, 1].Text = array[1].ToString();


            for (int j = 1; j < 5; j++)
            {
                for (int a = 1; a < 5; a++)
                {
                    width = this.Size.Width;
                    height = this.Size.Height;
                    width = 50;
                    height = 50;
                    x = array[(j - 1) * 4 + a - 1];
                    //buttons[j, a] = new Button();
                    buttons[j, a].Location = new Point(a * 50, j * 50);
                    buttons[j, a].Size = new Size(width, height);
                    x = array[(j - 1) * 4 + a - 1];
                    buttons[j, a].Text = array[x].ToString();
                    this.Controls.Add(buttons[j, a]);
                }


                label2.Text = array[d1].ToString();
            }
        }
    }
}

沒有留言:

張貼留言