程式設計工藝大師
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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
Button[,]Buttons = new System.Windows.Forms.Button[5,5];
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 1; i < 5; i++)
{
for (int j = 1; j < 5; j++)
{
Buttons[i, j] = new Button();
Buttons[i, j].Size = new Size(50, 50);
Buttons[i, j].Location = new Point(i * 50, j * 50);
this.Controls.Add(Buttons[i, j]);//出現在畫面中
}
}
}
private void button1_Click(object sender, EventArgs e)
{
int o=0,c,f;
Random rnd = new Random();
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0};
for (int i = 0; i < 16; ++i)
{
int j = rnd.Next(15);
int t = a[j];
a[j] = a[i];
a[i] = t;
}
for (int h = 1; h < 5; h++)
{
for (int v = 1; v < 5; v++)
{
Buttons[h, v].Text = a[o].ToString();
o = o + 1;
}
}
for (int k = 0; k < 16; ++k)
{
if (a[k] == 0)
{
c = k % 4 +1;
f = k / 4 +1;
Buttons[f, c].Text = " ";
}
}
}
private void button2_Click(object sender, EventArgs e)
{
int o = 0;
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0 };
for (int h = 1; h < 5; h++)
{
for (int v = 1; v < 5; v++)
{
Buttons[v, h].Text = a[o].ToString();
o = o + 1;
}
}
Buttons[4, 4].Text = " ";
}
private void button3_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
}
}
}
沒有留言:
張貼留言