ajax扫雷,布雷(扫雷游戏)

8d691a4a3b317d5456b6ed365bbc6b6e.gif扫雷啦!,这个游戏应该绝大数人都玩过,玩的时候很high,但是如果知道了游戏中的雷是怎么布置的,那么玩起来想必就更high啦。下面我们一起看看这么神奇的雷到底是怎么分布的?布雷的过程中有几个要点:

eb579bb8c775e9a2abbc3620966315ed.png取随机数作为雷的位置,让雷的位置可以随机分布;

eb579bb8c775e9a2abbc3620966315ed.png随机数不得重复,避免雷的数量减少;

eb579bb8c775e9a2abbc3620966315ed.png确定雷以外的地方应该显示的数字

namespace prj布雷

{

class Program

{

static void Main(string[] args)

{

//创建一个产生随机数的对象给r

Random r = new Random();

//设置雷区的边长

int length = 10;

//创建一个索引数组(数组中的元素作为索引),大小为雷区中元素的个数

int []ary=new int[length*length];

//设置雷的个数

int count = 9;

//为索引数组初始化,并要求每个元素的值不一样[0,99]

for (int i = 0; i < length*length; i++)

{

ary[i] = i;

}

//设置从索引数组中取值的范围(变化的)

int size=length*length;

//创建一个位置数组,其中元素都表示地雷的位置

int[] locations = new int[count];

for (int j = 0; j < count; j++)

{

//产生一个随机数index

int index = r.Next(size);

//以index为索引在索引数组中取出相应位置上的值

int val = ary[index];

//将在索引数组取出的值放入地雷位置数组中

locations[j] = val;

//已经将索引数组中的值作为一个地雷的位置,所以范围应该减小1

size--;

//将取过值的位置上的值用后面的值覆盖

for (int i = index; i < size-1; i++)

{

ary[i] = ary[i + 1];

}

}

//创建一个雷区数组将雷(9)放进去

int [,]mines=new int[length,length];

//将一维数组化成二维数组

for (int i = 0; i < count; i++)

{

int x = locations[i] / length;

int y = locations[i] % length;

mines[x, y] = 9;

}//给每颗雷的周围的格子中应该显示的数字

for (int i = 0; i < mines.GetLength(0); i++)

{

for (int j = 0; j < mines.GetLength(1); j++)

{

//如果格子中不是雷(9)就调用GetMineCount函数得到它应该显示的数字

if (mines[i, j] == 9)

{

continue;

}

mines[i, j] = GetMineCount(mines, i, j);

}

}

//将二维数组打印出来

for (int i = 0; i < mines.GetLength(0); i++)

{

for (int j = 0; j < mines.GetLength(1); j++)

{

Console.Write("{0}\t",mines[i,j]);

}

Console.WriteLine();

}

}

private static int GetMineCount(int[,] mines, int RowIndex, int ColumIndex)

{//

int count = 0;//假设这个格子显示的是0

int topRowIndex = RowIndex - 1;

int bottomRowIndex = RowIndex + 1;

int leftCoumIndex = ColumIndex - 1;

int rightColumIndex = ColumIndex + 1;

//左上

//格子存在的条件成立

if (topRowIndex >= 0 && leftCoumIndex>=0)

{

//判断这个方位的格子是不是雷,如果这个格子正左方的格子是雷那么数字加1

if (mines[topRowIndex,leftCoumIndex]==9)

{

count++;

}

}

//正上

//格子存在的条件成立

if (topRowIndex >= 0)

{

//判断这个方位的格子是不是雷,如果这个格子正左方的格子是雷那么数字加1

if (mines[topRowIndex,ColumIndex]==9)

{

count++;

}

}

//右上

//格子存在的条件成立

if (topRowIndex >= 0 && rightColumIndex < mines.GetLength(1))

{

//判断这个方位的格子是不是雷,如果这个格子正左方的格子是雷那么数字加1

if (mines[topRowIndex, rightColumIndex] == 9)

{

count++;

}

}

//左下

//格子存在的条件成立

if (bottomRowIndex < mines.GetLength(0) && leftCoumIndex >= 0)

{

//判断这个方位的格子是不是雷,如果这个格子正左方的格子是雷那么数字加1

if (mines[bottomRowIndex, leftCoumIndex] == 9)

{

count++;

}

}

//正下

//格子存在的条件成立

if (bottomRowIndex < mines.GetLength(0))

{

//判断这个方位的格子是不是雷,如果这个格子正左方的格子是雷那么数字加1

if (mines[bottomRowIndex, ColumIndex] == 9)

{

count++;

}

}

//右下

//格子存在的条件成立

if (bottomRowIndex < mines.GetLength(0) && rightColumIndex < mines.GetLength(1))

{

//判断这个方位的格子是不是雷,如果这个格子正左方的格子是雷那么数字加1

if (mines[bottomRowIndex, rightColumIndex] == 9)

{

count++;

}

}

//正左

//格子存在的条件成立

if (leftCoumIndex>=0)

{

//判断这个方位的格子是不是雷,如果这个格子正左方的格子是雷那么数字加1

if (mines[RowIndex,leftCoumIndex]==9)

{

count++;

}

}

//正右

//格子存在的条件成立

if (rightColumIndex < mines.GetLength(1))

{

//判断这个方位的格子是不是雷,如果这个格子正右方的格子是雷那么数字加1

if (mines[RowIndex, rightColumIndex] == 9)

{

count++;

}

}

//返回格子应该显示的数字

return count;

}

}

}

运行结果如下:1446408ca84b65d62f3b32cdeefc5cc4.gif

c22783ee03c9c7427f8cfbe5379fd301.png

b0af8b58ad799f79cee12a6965b89eb9.gifAjax的姑娘,加油!b0af8b58ad799f79cee12a6965b89eb9.gif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值