java扫雷源代码思路_扫雷 思路 与源代码 二

扫雷步骤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()

{

//重置恢复到关闭的状态

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值