close
zxc

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int n = 4, a;
        Button[,] Buttons = new System.Windows.Forms.Button[10, 10];
        int[] randomize = new int[16];
        Random rnd = new Random();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    Buttons[i, j] = new Button();
                    Buttons[i, j].Location = new Point(i * 50, j * 50);
                    Buttons[i, j].Size = new Size(50, 50);

                    this.Controls.Add(Buttons[i, j]);
                }
            }

        }

        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]);

                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

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

                randomize[i] = i;

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

                for (int j = 0; j < 4; ++j)
                {
                    Buttons[i, j].Text = Convert.ToString(randomize[i  + j*4]);            //將一維陣列randomize依序放進二維陣列的按鈕上
                    this.Controls.Add(Buttons[i, j]);
                }

            }
        }
    }
}

arrow
arrow
    文章標籤
    randomize
    全站熱搜
    創作者介紹
    創作者 zxc1234567890666 的頭像
    zxc1234567890666

    zxc1234567890666的部落格

    zxc1234567890666 發表在 痞客邦 留言(0) 人氣()