布雷(扫雷游戏)

布雷(扫雷游戏)

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

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

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

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

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;

}

}

}

运行结果如下:

Ajax的姑娘,加油!

相关文章

365一直提款维护中 英雄登场,共赴花开之夜!英雄联盟14周年生日会开启
365bet官网欧洲 翡翠绳子怎么编_翡翠绳子编法?