using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace 连连看
{
public partial class Link : Form
{
private const int COUNT_BUTTONS = 80;//按纽总数
private const int COLUMNS = 8;//烈数目
private int count = 0;//鼠标单击按纽次数
private int firstIndex = 0, secondIndex = 0;//第一次、第二次单击按纽的下标
Button[] b = new Button[80];
ArrayList list1 = new ArrayList(40);
ArrayList list2 = new ArrayList(40);
public Link()
{
InitializeComponent();
}
private void Link_Load(object sender, EventArgs e)
{
generateRandomNums(list1);
generateRandomNums(list2);
for (int i = 0; i < COUNT_BUTTONS; i++)
{
b[i] = new Button();
b[i].FlatStyle = FlatStyle.Flat;
if (i < 40)
{
b[i].Image = this.imageList1.Images[Int32.Parse(list1[i].ToString())];
b[i].Tag = Int32.Parse(list1[i].ToString());
}
else
{
b[i].Image = this.imageList1.Images[Int32.Parse(list2[i - 40].ToString())];
b[i].Tag = Int32.Parse(list2[i - 40].ToString());
}
b[i].Width = 30;
b[i].Height = 30;
b[i].Top = b[i].Height * (i / COLUMNS);
b[i].Left = b[i].Width * (i % COLUMNS);
b[i].Click += new EventHandler(process_click);
this.Controls.Add(b[i]);
}
}
private void process_click(object sender, EventArgs e)
{
count++;
if (count == 1)
{
this.firstIndex = GetFocusedButtonIndex();
}
if (count == 2)
{
this.secondIndex = GetFocusedButtonIndex();
if (b[firstIndex].Tag.Equals(b[secondIndex].Tag) && firstIndex != secondIndex)
{
b[firstIndex].Dispose();
b[secondIndex].Dispose();
}
count = 0;
}
}
/// <summary>
/// 判断哪个按纽被单击
///
/// </summary>
/// <returns></returns>
private int GetFocusedButtonIndex()
{
for (int i = 0; i < COUNT_BUTTONS; i++)
if (b[i].Focused == true)
return i;
return -1;
}
/// <summary>
/// 产生随机数,存放到数组中
/// </summary>
/// <param name="list"></param>
private void generateRandomNums(ArrayList list)
{
int randomNum;
while (true)
{
Random r = new Random();
randomNum = r.Next(40);
if (!list.Contains(randomNum))
{
list.Add(randomNum);
}
if (list.Count == 40)
{
break;
}
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace 连连看
{
public partial class Link : Form
{
private const int COUNT_BUTTONS = 80;//按纽总数
private const int COLUMNS = 8;//烈数目
private int count = 0;//鼠标单击按纽次数
private int firstIndex = 0, secondIndex = 0;//第一次、第二次单击按纽的下标
Button[] b = new Button[80];
ArrayList list1 = new ArrayList(40);
ArrayList list2 = new ArrayList(40);
public Link()
{
InitializeComponent();
}
private void Link_Load(object sender, EventArgs e)
{
generateRandomNums(list1);
generateRandomNums(list2);
for (int i = 0; i < COUNT_BUTTONS; i++)
{
b[i] = new Button();
b[i].FlatStyle = FlatStyle.Flat;
if (i < 40)
{
b[i].Image = this.imageList1.Images[Int32.Parse(list1[i].ToString())];
b[i].Tag = Int32.Parse(list1[i].ToString());
}
else
{
b[i].Image = this.imageList1.Images[Int32.Parse(list2[i - 40].ToString())];
b[i].Tag = Int32.Parse(list2[i - 40].ToString());
}
b[i].Width = 30;
b[i].Height = 30;
b[i].Top = b[i].Height * (i / COLUMNS);
b[i].Left = b[i].Width * (i % COLUMNS);
b[i].Click += new EventHandler(process_click);
this.Controls.Add(b[i]);
}
}
private void process_click(object sender, EventArgs e)
{
count++;
if (count == 1)
{
this.firstIndex = GetFocusedButtonIndex();
}
if (count == 2)
{
this.secondIndex = GetFocusedButtonIndex();
if (b[firstIndex].Tag.Equals(b[secondIndex].Tag) && firstIndex != secondIndex)
{
b[firstIndex].Dispose();
b[secondIndex].Dispose();
}
count = 0;
}
}
/// <summary>
/// 判断哪个按纽被单击
///
/// </summary>
/// <returns></returns>
private int GetFocusedButtonIndex()
{
for (int i = 0; i < COUNT_BUTTONS; i++)
if (b[i].Focused == true)
return i;
return -1;
}
/// <summary>
/// 产生随机数,存放到数组中
/// </summary>
/// <param name="list"></param>
private void generateRandomNums(ArrayList list)
{
int randomNum;
while (true)
{
Random r = new Random();
randomNum = r.Next(40);
if (!list.Contains(randomNum))
{
list.Add(randomNum);
}
if (list.Count == 40)
{
break;
}
}
}
}
}
下载地址: