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 WindowsFormsApplication8
{
public partial class Form1 : Form
{
public System.Windows.Forms.Button Button1;
public System.Windows.Forms.Button Button2;
public System.Windows.Forms.Button Button3;
public System.Windows.Forms.Button Button4;
public System.Windows.Forms.Button Button5;

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

Button2 = new System.Windows.Forms.Button();
Button2.Location = new Point(400, 100);
Button2.Size = new Size(50, 60);
Button2.Text = "Up";
Button2.Click += new EventHandler(Button2_Click);
this.Controls.Add(Button2);

Button3 = new System.Windows.Forms.Button();
Button3.Location = new Point(400, 220);
Button3.Size = new Size(50, 60);
Button3.Text = "Down";
Button3.Click += new EventHandler(Button3_Click);
this.Controls.Add(Button3);

Button4 = new System.Windows.Forms.Button();
Button4.Location = new Point(350, 160);
Button4.Size = new Size(50, 60);
Button4.Text = "Left";
Button4.Click += new EventHandler(Button4_Click);
this.Controls.Add(Button4);

Button5 = new System.Windows.Forms.Button();
Button5.Location = new Point(450, 160);
Button5.Size = new Size(50, 60);
Button5.Text = "Right";
Button5.Click += new EventHandler(Button5_Click);
this.Controls.Add(Button5);

for (int i = 1; i < 4; i++)
{
for (int j = 1; j < 4; j++)
{

buttons[i, j] = new Button();
buttons[i, j].Location = new Point(40 + i * 70, 40 + j * 70);
buttons[i, j].Size = new Size(70, 70);
this.Controls.Add(buttons[i, j]);
}
}
buttons[1, 1].Text = "1";
buttons[2, 1].Text = "2";
buttons[3, 1].Text = "3";
buttons[1, 2].Text = "4";
buttons[2, 2].Text = "5";
buttons[3, 2].Text = "6";
buttons[1, 3].Text = "7";
buttons[2, 3].Text = "8";
buttons[3, 3].Text = "0";

}
protected void Button1_Click(object sender, EventArgs e)
{

int x, y, a;
int[] myarray = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
for (int i = 8; i > 1; i--)
{
x = rand.Next(0, i);
y = myarray[x];
myarray[x] = myarray[i];
myarray[i] = y;
}
for (int i = 1; i < 4; i++)
{
for (int j = 1; j < 4; j++)
{
a = myarray[(j - 1) * 3 + (i - 1)];
buttons[i, j].Text = Convert.ToString(a);
this.Controls.Add(buttons[i, j]);
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
string zero;
if (buttons[1, 2].Text == "0")
{
zero = buttons[1, 2].Text;
buttons[1, 2].Text = buttons[1,1].Text;
buttons[1,1].Text = zero;
}
if (buttons[2, 2].Text == "0")
{
zero = buttons[2, 2].Text;
buttons[2, 2].Text = buttons[2,1].Text;
buttons[2,1].Text = zero;
}
if (buttons[3, 2].Text == "0")
{
zero = buttons[3, 2].Text;
buttons[3, 2].Text = buttons[3,1].Text;
buttons[3,1].Text = zero;
}
if (buttons[1, 3].Text == "0")
{
zero = buttons[1, 3].Text;
buttons[1, 3].Text = buttons[1,2].Text;
buttons[1,2].Text = zero;
}
if (buttons[2, 3].Text == "0")
{
zero = buttons[2, 3].Text;
buttons[2, 3].Text = buttons[2,2].Text;
buttons[2,2].Text = zero;
}
if (buttons[3, 3].Text == "0")
{
zero = buttons[3, 3].Text;
buttons[3, 3].Text = buttons[3,2].Text;
buttons[3,2].Text = zero;
}
win();


}

protected void Button3_Click(object sender, EventArgs e)
{
string zero;

if (buttons[1, 1].Text == "0")
{
zero = buttons[1, 1].Text;
buttons[1, 1].Text = buttons[1, 2].Text;
buttons[1, 2].Text = zero;
}
if (buttons[2, 1].Text == "0")
{
zero = buttons[2, 1].Text;
buttons[2, 1].Text = buttons[2, 2].Text;
buttons[2, 2].Text = zero;
}
if (buttons[3, 1].Text == "0")
{
zero = buttons[3, 1].Text;
buttons[3, 1].Text = buttons[3, 2].Text;
buttons[3, 2].Text = zero;
}
if (buttons[1, 2].Text == "0")
{
zero = buttons[1, 2].Text;
buttons[1, 2].Text = buttons[1, 3].Text;
buttons[1, 3].Text = zero;
}
if (buttons[2, 2].Text == "0")
{
zero = buttons[2, 2].Text;
buttons[2, 2].Text = buttons[2, 3].Text;
buttons[2, 3].Text = zero;
}
if (buttons[3, 2].Text == "0")
{
zero = buttons[3, 2].Text;
buttons[3, 2].Text = buttons[3, 3].Text;
buttons[3, 3].Text = zero;
}
win();
}
protected void Button4_Click(object sender, EventArgs e)
{
string zero;

if (buttons[2,1].Text == "0")
{
zero = buttons[2, 1].Text;
buttons[2, 1].Text = buttons[1, 1].Text;
buttons[1, 1].Text = zero;
}
if (buttons[2, 2].Text == "0")
{
zero = buttons[2, 2].Text;
buttons[2, 2].Text = buttons[1, 2].Text;
buttons[1, 2].Text = zero;
}
if (buttons[2, 3].Text == "0")
{
zero = buttons[2, 3].Text;
buttons[2, 3].Text = buttons[1, 3].Text;
buttons[1, 3].Text = zero;
}
if (buttons[3, 1].Text == "0")
{
zero = buttons[3, 1].Text;
buttons[3, 1].Text = buttons[2, 1].Text;
buttons[2, 1].Text = zero;
}
if (buttons[3, 2].Text == "0")
{
zero = buttons[3, 2].Text;
buttons[3, 2].Text = buttons[2, 2].Text;
buttons[2, 2].Text = zero;
}
if (buttons[3, 3].Text == "0")
{
zero = buttons[3, 3].Text;
buttons[3, 3].Text = buttons[2, 3].Text;
buttons[2, 3].Text = zero;
}
win();

}
protected void Button5_Click(object sender, EventArgs e)
{
string zero;

if (buttons[1, 1].Text == "0")
{
zero = buttons[1, 1].Text;
buttons[1, 1].Text = buttons[2, 1].Text;
buttons[2, 1].Text = zero;
}
if (buttons[1, 2].Text == "0")
{
zero = buttons[1, 2].Text;
buttons[1, 2].Text = buttons[2, 2].Text;
buttons[2, 2].Text = zero;
}
if (buttons[1, 3].Text == "0")
{
zero = buttons[1, 3].Text;
buttons[1, 3].Text = buttons[2, 3].Text;
buttons[2, 3].Text = zero;
}
if (buttons[2, 1].Text == "0")
{
zero = buttons[2, 1].Text;
buttons[2, 1].Text = buttons[3, 1].Text;
buttons[3, 1].Text = zero;
}
if (buttons[2, 2].Text == "0")
{
zero = buttons[2, 2].Text;
buttons[2, 2].Text = buttons[3, 2].Text;
buttons[3, 2].Text = zero;
}
if (buttons[2, 3].Text == "0")
{
zero = buttons[2, 3].Text;
buttons[2, 3].Text = buttons[3, 3].Text;
buttons[3, 3].Text = zero;
}
win();

}
public void win()
{
if (buttons[1, 1].Text == "1" && buttons[2, 1].Text == "2" && buttons[3, 1].Text == "3" && buttons[1, 2].Text == "4" && buttons[2, 2].Text == "5" && buttons[3, 2].Text == "6" && buttons[1, 3].Text == "7" && buttons[2, 3].Text == "8" && buttons[3, 3].Text == "0")
{
MessageBox.Show("Congratulations!!! You Won!");
}
}
}
}

arrow
arrow
    全站熱搜

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