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 saolei
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<Label> lblList = new List<Label>(81);
List<Button> btnList = new List<Button>(81);
List<Point> pointList = new List<Point>();//控件位置集合
Dictionary<int, Point> pointByIndex = new Dictionary<int, Point>();//将位置和索引绑定
List<int> alreadyIndex = new List<int>();//已经校验过的索引
List<int> leiPoint = new List<int>();//记录雷所在位置
Label l2 = new Label();//雷数标签
Button b2 = new Button();//重新开始按钮
Label l3 = new Label();//倒计时标签
Timer t1 = new Timer();//计时器
private int jishi = 0;//计时
string btn1Name = "";
bool isWin = true;
int al_count = 0;
string startTime = "";
string endTime = "";
private void Form1_Load(object sender, EventArgs e)
{
initGame();
}
private void btnLei_MouseClick(object sender, MouseEventArgs e)
{
Button b1 = (Button)sender;
if (e.Button==MouseButtons.Left)
{
if (b1.Text == "☻")
{
return;
}
for (int i = 0; i < btnList.Count; i++)
{
if (btnList[i].Name == b1.Name)
{
//如果有雷
if (lblList[i].Text == "*")
{
//b2.BackgroundImage = Image.FromFile(@"D:\学习资料\C#\QQ2013最新素材\Data\Face\15fix.bmp");
for (int j = 0; j < lblList.Count; j++)
{
if (lblList[j].Text == "*")
{
lblList[j].Visible = true;
}
}
}
else
{
//找到周围8个位置
startTime = DateTime.Now.ToLongTimeString();
isCunzai(i);
endTime = DateTime.Now.ToLongTimeString();
//MessageBox.Show("开始时间:"+startTime+"\n结束时间:"+endTime);
}
}
}
Win();
}
else if(e.Button==MouseButtons.Right)
{
//MessageBox.Show("点击了右键");
if (b1.Text == "☻")
{
b1.Text = "";
int leiNum = int.Parse(l2.Text.Substring(5));
l2.Text = "剩余雷数:" + (leiNum +1);
}
else
{
int leiNum = int.Parse(l2.Text.Substring(5));
if (leiNum - 1<0)
{
return;
}
else
{
l2.Text = "剩余雷数:" + (leiNum - 1);
b1.Text = "☻";
Win();
}
}
}
}
//获胜判断
private void Win()
{
isWin = true;
//校验是否扫出所有雷
for (int i = 0; i < leiPoint.Count; i++)
{
if (btnList[leiPoint[i]].Text != "☻")
{
isWin = false;
}
}
if (isWin)
{
initGame();
MessageBox.Show("恭喜!您赢了!");
}
}
//初始化
private void initGame()
{
jishi = 0;
t1.Stop();
lblList.Clear();
btnList.Clear();
pointList.Clear();
pointByIndex.Clear();
alreadyIndex.Clear();
leiPoint.Clear();
isWin = true;
al_count = 0;
Controls.Clear();
int count2 = 0;//索引
int count = 0;//剩余雷数
//初始化
//生成标签和按钮并添加到集合
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
Point p1 = new Point(20 * i, 45+(20 * j));
Label l1 = new Label();
l1.Size = new Size(20, 20);
l1.Name = "lbl" + i + j;
l1.Text = "";
l1.Location = p1;
l1.BackColor = Color.Green;
l1.Visible = false;
lblList.Add(l1);
Button b1 = new Button();
b1.Size = new Size(20, 20);
b1.Name = "btn" + i + j;
b1.Location = p1;
//b1.BackgroundImage=Image.FromFile(@"D:\学习资料\C#\QQ2013最新素材\Default\AppFramework\Button\btn_down.png");
b1.MouseDown += new MouseEventHandler(btnLei_MouseClick);
btnList.Add(b1);
Controls.Add(l1);
Controls.Add(b1);
pointList.Add(new Point(i, j));
pointByIndex.Add(count2, new Point(i, j));
count2++;
}
}
//将10个雷随机放入雷区
Random rd = new Random();
int k1 = rd.Next(0, 80);
int k2 = rd.Next(0, 80);
int k3 = rd.Next(0, 80);
int k4 = rd.Next(0, 80);
int k5 = rd.Next(0, 80);
int k6 = rd.Next(0, 80);
int k7 = rd.Next(0, 80);
int k8 = rd.Next(0, 80);
int k9 = rd.Next(0, 80);
int k10 = rd.Next(0, 80);
lblList[k1].Text = "*";
lblList[k1].BackColor = Color.Red;
lblList[k2].Text = "*";
lblList[k2].BackColor = Color.Red;
lblList[k3].Text = "*";
lblList[k3].BackColor = Color.Red;
lblList[k4].Text = "*";
lblList[k4].BackColor = Color.Red;
lblList[k5].Text = "*";
lblList[k5].BackColor = Color.Red;
lblList[k6].Text = "*";
lblList[k6].BackColor = Color.Red;
lblList[k7].Text = "*";
lblList[k7].BackColor = Color.Red;
lblList[k8].Text = "*";
lblList[k8].BackColor = Color.Red;
lblList[k9].Text = "*";
lblList[k9].BackColor = Color.Red;
lblList[k10].Text = "*";
lblList[k10].BackColor = Color.Red;
for (int i = 0; i < lblList.Count; i++)
{
if (lblList[i].Text == "*")
{
count++;
leiPoint.Add(i);
}
}
//生成雷数提示标签和再来一次按钮
btn1Name = "剩余雷数:" + count.ToString();
l2.Name = "lable1";
l2.Text = btn1Name;
l2.Size = new Size(65, 12);
l2.Location = new Point(12, 14);
Controls.Add(l2);
b2.Name = "button1";
b2.Text = "";
b2.Size = new Size(28,28);
//b2.BackgroundImage = Image.FromFile(@"D:\学习资料\C#\QQ2013最新素材\Data\Face\14fix.bmp");
b2.BackgroundImageLayout = ImageLayout.Center;
b2.Location = new Point(80, 6);
b2.MouseDown += new MouseEventHandler(btn1_Down);
Controls.Add(b2);
//生成计时器
l3.Name = "lable2";
l3.Text = "";
l3.BackColor = Color.Black;
l3.ForeColor = Color.Red;
l3.Size = new Size(54, 15);
l3.Location = new Point(130, 14);
Controls.Add(l3);
t1.Interval = 1000;
t1.Tick+=new EventHandler(t1_Tick);
t1.Start();
}
//递归扫雷
public void isCunzai(int curIndex)
{
//如果校验过该位置,则返回
if (alreadyIndex.Contains(curIndex))
{
return;
}
else
{
alreadyIndex.Add(curIndex);
}
List<int> disIndex = new List<int>();//要显示的控件集合
List<Point> indexList = new List<Point>();//当前点击按钮周围控件位置集合
List<int> zwIndex = new List<int>();//周围控件索引集合
int lei_count = 0;
int count1 = 0;
//翻开被点击的按钮
lblList[curIndex].Visible = true;
Point curPoint = pointByIndex[curIndex];
//计算周围8个位置
Point leftUpIndex = new Point(curPoint.X - 1, curPoint.Y - 1);
Point leftIndex = new Point(curPoint.X - 1, curPoint.Y);
Point leftDownIndex = new Point(curPoint.X - 1, curPoint.Y + 1);
Point UpIndex = new Point(curPoint.X, curPoint.Y - 1);
Point DownIndex = new Point(curPoint.X, curPoint.Y + 1);
Point rigthUpIndex = new Point(curPoint.X + 1, curPoint.Y - 1);
Point rightIndex = new Point(curPoint.X + 1, curPoint.Y);
Point rightDownIndex = new Point(curPoint.X + 1, curPoint.Y + 1);
indexList.Add(leftUpIndex);
indexList.Add(leftIndex);
indexList.Add(leftDownIndex);
indexList.Add(UpIndex);
indexList.Add(DownIndex);
indexList.Add(rigthUpIndex);
indexList.Add(rightIndex);
indexList.Add(rightDownIndex);
//找出周围位置的索引
for (int i = 0; i < indexList.Count; i++)
{
if (pointByIndex.ContainsValue(indexList[i]))
{
//用linq获取key的值,并添加到周围索引的集合
zwIndex.Add(pointByIndex.FirstOrDefault(x => x.Value == indexList[i]).Key);
}
}
//判断周围8个位置是否有雷,当且仅当8个位置都没有雷的情况下,全部翻开
for (int i = 0; i < zwIndex.Count; i++)
{
if (lblList[zwIndex[i]].Text=="*")
{
lei_count++;
}
else
{
count1++;
disIndex.Add(zwIndex[i]);
}
}
if (count1 == zwIndex.Count)
{
for (int j = 0; j < disIndex.Count; j++)
{
lblList[disIndex[j]].Visible = true;
}
for (int i = 0; i < zwIndex.Count; i++)
{
isCunzai(zwIndex[i]);
}
}
else
{
lblList[curIndex].Text = lei_count.ToString();
}
}
private void btn1_Down(object sender, MouseEventArgs e)
{
initGame();
}
//计时功能,未完善
private void t1_Tick(object sender, EventArgs e)
{
jishi++;
string l3Name = "";
if (jishi<10)
{
l3Name = "00:00:0"+jishi.ToString();
}
else if(jishi<100)
{
l3Name = "00:00:" + jishi.ToString();
}
l3.Text = l3Name;
}
}
}
C#制作的扫雷小程序
最新推荐文章于 2021-12-12 11:59:31 发布