标签:
扫雷步骤2:
首先第一步:随机埋雷, 定义一个随机埋雷的对象,循环,取得一个整数范围的数量(0- 格子的数)如果这个格子有地雷为真,那么循环+1
第二步:计算每个格子周边的格子的数量 用循环所有格子,其次在用一个cell类型的数组,并在周围格子的占内存中查找来存放当前方格周边所有的方格 并用int unm来记录 (传一个对象给我,然后统计当前的地雷)如果在周围格子中发现了地雷,记录下来并将值传给cell数组
第三步:定义一个周边格子的的数组类将当前格子传给它。 先计算左右、上下、四个角,并返回一个值,到第二步。
第四步:定义打开格子的三种情况:一是有雷,二是空的,三,将格子周边的地雷数显示到这个格子上。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public class MineField
{
private int width;
public int Width //宽度
{
get { return width; }
set { width = value; }
}
private int height;
public int Height //高度
{
get { return height; }
set { height = value; }
}
private Cell[] cells; //根据用户的选择来初始化多个少个格子
public Cell[] Cells
{
get { return cells; }
set { cells = value; }
}
private int minecout;
public int Minecout //地雷数
{
get { return minecout; }
set { minecout = value; }
}
private int linecout;
public int Linecout //一行的格子数
{
get { return linecout; }
set { linecout = value; }
}
public MineField() { }
public MineField(int width, int height, int linecout, int minecout) //构造雷区的方法
{
this.width = width;
this.height = height;
this.linecout = linecout;
this.minecout = minecout;
this.Init();
}
//初始化雷区的方法
public void Init()
{
cells = new Cell[linecout * linecout]; //按照每边的数量创建所有的格子
for (int i = 0; i < linecout * linecout; i++)
{
cells[i] = new Cell();
cells[i].Click += MineField_Click;
}
this.Layyout();
//随即埋雷
Random r=new Random(); //定义一个随机类型
for (int i = 0; i < minecout; )
{
int index = r.Next(0, linecout * linecout); //随机取得一个整数 范围是0-21
if (!cells[index].HasMine)
{
cells[index].HasMine = true;
i++;
}
}
//填写每个方格周边的数量
for (int i = 0; i < linecout * linecout; i++)
{
//分别计算当前格子的地雷数量
//定义一个cell类型的数组用来存放当前方格周边计算周边所有的方格
int num = 0;
Cell[] arroundCells = this.GetAroundCells(cells[i],ref num);
//给我一个格子 运行getaroumn方法中将左右、上下 四个角 的格子算出 并返回给这个方法运行
int count = 0;
for (int j = 0; j < num; j++)
{
if (arroundCells[j].HasMine) //统计所有方格的地雷
{
count++;
}
}
//将定义好的赋值到当前方格的aroundMineCount属性中
cells[i].AroundMineCount = count;
}
}
void MineField_Click(object sender, EventArgs e)
{
Cell cell = (Cell)sender;
if (cell.HasMine)
{
cell.Open();
this.OpenAllCells();
MessageBox.Show("你踩雷了");
}
else if (cell.AroundMineCount != 0)
{
cell.Open();
}
else
{
cell.Open();
int num = 0;
Cell[] aroundCells = this.GetAroundCells(cell,ref num);
for (int i = 0; i < num; i++)
{
aroundCells[i].Open();
}
}
}
//获取某个周边的方格
public Cell[] GetAroundCells(Cell currentCell, ref int num) //给我一个格子我给你所有的格子
{
int cellWidth = this.width / linecout;
int cellHeight = this.height / linecout;
Cell[] aroundCells = new Cell[8];
for (int i = 0; i < linecout * linecout; i++)
{
if (cells[i].Top == currentCell.Top && Math.Abs(cells[i].Left - currentCell.Left) == cellWidth)
{
aroundCells[num++] = cells[i]; //左右
}
if (cells[i].Left == currentCell.Left && Math.Abs(cells[i].Top - currentCell.Top) == cellHeight)
{
aroundCells[num++] = cells[i]; //上下
}
if (Math.Abs(cells[i].Top - currentCell.Top) == cellHeight && Math.Abs(cells[i].Left - currentCell.Left) == cellWidth)
{
aroundCells[num++] = cells[i]; //四个角
}
}
return aroundCells;
}
public void Layyout()
{
int cellWidth = this.width / Linecout;
int cellHeihth = this.height / Linecout;
int k = 0;
int left, top;
left = 0;
top = 0;
for (int i = 0; i < linecout; i++)
{
left = 0;
for (int j = 0; j < linecout; j++)
{
cells[k].Width = cellWidth;
cells[k].Height = cellHeihth;
cells[k].Left = left;
cells[k].Top = top;
left += cellWidth;
k++;
}
top += cellHeihth;
}
}
//翻开所有的方格
public void OpenAllCells()
{
for (int i = 0; i < linecout*linecout; i++)
{
cells[i].Open();
}
}
}
}
//格子的状态
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using WindowsFormsApplication1.Properties;
namespace WindowsFormsApplication1
{
public class Cell:Button
{
//是否存在地雷
private bool hasMine;
public bool HasMine
{
get { return hasMine; }
set { hasMine = value; }
}
//格子四周地雷数
private int aroundMineCount;
public int AroundMineCount
{
get { return aroundMineCount; }
set { aroundMineCount = value; }
}
private Cellstate state;
public Cellstate State
{ //格子的状态
get { return state; }
set { state = value; }
}
public enum Cellstate
{
//枚举类型 四种状态 翻开,关闭,标记、还原
Open,
Closed,
Mark,
Reset
}
public Cell()
{
this.state = Cellstate.Closed; //初始化翻开的时候是关闭的
this.BackgroundImageLayout = ImageLayout.Stretch;
}
public void Open() //打开时的状态
{
if (this.hasMine)
{
this.BackgroundImage=Resources.MarkedWrong;
}
else if (this.aroundMineCount == 0)
{
this.BackColor = Color.Green;
}
else
{
switch (aroundMineCount)
{
case 1: this.BackgroundImage = Resources._1; break;
case 2: this.BackgroundImage = Resources._2; break;
case 3: this.BackgroundImage = Resources._3; break;
case 4: this.BackgroundImage = Resources._4; break;
case 5: this.BackgroundImage = Resources._5; break;
case 6: this.BackgroundImage = Resources._6; break;
case 7: this.BackgroundImage = Resources._7; break;
case 8: this.BackgroundImage = Resources._8; break;
}
}
this.Enabled = false;
}
public void Mark()
{
//标记
}
public void Reset()
{
//重置恢复到关闭的状态
}
}
}
标签:
原文地址:http://www.cnblogs.com/liyiyong/p/5120350.html