using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication9
{
public partial class Form1 : Form
{
Button[,] buttons = new Button[5, 5];
int i, j, sum, rn, rn1;
int[] a = new int[17], b = new int[17];
bool a1 = false, b1 = false;
string cho1, cho2;
int ch1i, ch1j, ch2i, ch2j,ch1,ch2;
bool onechoice = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
for (i = 1; i <= 16; i++)
{
a[i] = i;
}
for (i = 1; i <= 16; i++)
{
Random rn = new Random();
rn1 = rn.Next(1, 17 - i);
b[i] = a[rn1];
for (j = rn1; j <= 16 - i; j++)
{
a[j] = a[j + 1];
}
}
for (i = 1; i <= 4; i++)
{
for (j = 1; j <= 4; j++)
{
sum += 1;
buttons[i, j] = new Button();
buttons[i, j].TabIndex = sum;
buttons[i, j].Text = Convert.ToString(b[sum]);
if (b[sum] == 16)
{
buttons[i, j].Text = null;
}
buttons[i, j].Location = new Point(i * 50, j * 50);
buttons[i, j].Size = new Size(50, 50);
this.Controls.Add(buttons[i, j]);
buttons[i, j].Click += new EventHandler(buttons_Click);
}
}
}
private void buttons_Click(object sender, EventArgs e)
{
Button temp = (Button)sender;
label1.Text = temp.Text;
//MessageBox.Show(onechoice.ToString());
if (a1 == false)
{
cho1 = temp.Text;
a1 = true;
//MessageBox.Show(temp.Text);
}
else if (b1 == false && a1 == true)
{
//MessageBox.Show("2");
cho2 = temp.Text;
a1 = false;
}
for (int i = 1; i < 5; i++)
{
for (int j = 1; j <5 ; j++)
{
//MessageBox.Show(onechoice.ToString());
if (onechoice.ToString() == "False")
{
if (buttons[i,j].Text==cho1)
{
ch1i = i;
ch1j = j;
cho1 = temp.Text;
//MessageBox.Show(ch1i+","+ch1j);
onechoice = true;
}
}
else if(onechoice==true)
{
//onechoice = false;
if (temp.Text == buttons[i, j].Text)
{
//MessageBox.Show("1");
onechoice = false;
ch2i = i;
ch2j = j;
cho2 = temp.Text;
buttons[ch1i, ch1j].Text = cho2;
buttons[ch2i, ch2j].Text = cho1;
for (i = 1; i <= 4; i++)
{
for (j = 1; j <= 4; j++)
{
sum += 1;
buttons[i, j] = new Button();
buttons[i, j].TabIndex = sum;
//buttons[i, j].Text = Convert.ToString(b[sum]);
/*if (b[sum] == 16)
{
buttons[i, j].Text = null;
}*/
buttons[i, j].Location = new Point(i * 50, j * 50);
buttons[i, j].Size = new Size(50, 50);
this.Controls.Add(buttons[i, j]);
//buttons[i, j].Click += new EventHandler(buttons_Click);
}
}
}
}
}
}
}
}
}
沒有留言:
張貼留言