
using System
using System.Threading
namespace 扫雷
{
//地图类,用来生成地图
class Cmap
{
public Cmap(int x,int y) { CreatMap(x, y, 20)CreatGameMap(x, y)}
含拍 int x//地图长
int y//地图宽
public int p//雷的数量---数量要小于x*y待实现
消清 public int[] mapLine//地图的一维数组
public int[,] map//地图的二维数组
public string[,] gameMap//游戏地图
public int[] mineSet//记录雷的位置
public int gameState=0//记录游戏情况:0游戏中,1游戏失败,2游戏结束
public int n = 0
//创建地图
public void CreatMap(int x, int y, int p)
{
Random random = new Random()
this.x = x
this.y = y
this.p = p
map = new int[x, y]
mapLine = new int[x*y]
//生成雷的位置
mineSet = new int[p]
for (int i = 0i <pi++)
{
mineSet[i] = random.Next(0, mapLine.Length)
for (int j = 0j <ij++)
{
if (mineSet[j] == mineSet[i])
{
i--
}
}
}
//一维数组初始化
for (int i = 0i <mapLine.Lengthi++)
{
mapLine[i] = 0
}
//一维数组装雷
for (int i = 0i <mineSet.Lengthi++)
{
mapLine[mineSet[i]] = 9
}
//将有雷的信息的一维数组撞填到二维数组里面
int conter = 0
for (int i = 0i <map.GetLength(0)i++)
{
for (int j = 0j <map.GetLength(1)j++)
{
map[i, j] = mapLine[conter++]
}
拿老前 }
//数字块算法
int SetNumber(int x,int y)
{ int conter=0
if (map[x,y]==9)
{
return 9
}
else
{
if (x - 1 >= 0 &&y - 1 >= 0) { if (map[x - 1, y - 1] == 9) { conter++} }
if (y - 1 >= 0) { if (map[x, y - 1] == 9) { conter++} }
if (x - 1 >= 0) { if (map[x - 1, y] == 9) { conter++} }
if (x - 1 >= 0 &&y + 1 <= this.y-1) { if (map[x - 1, y + 1] == 9) { conter++} }
if (x + 1 <= this.x - 1 &&y - 1 >= 0) { if (map[x + 1, y - 1] == 9) { conter++} }
if (y + 1 <= this.y-1) { if (map[x , y + 1] == 9) { conter++} }
if (x + 1 <= this.x-1 ) { if (map[x + 1, y] == 9) { conter++} }
if (x + 1 <= this.x-1 &&y + 1 <= this.y - 1) { if (map[x + 1, y + 1] == 9) { conter++} }
}
return conter
}
//遍历地图,生成数字块
for (int i = 0i <map.GetLength(0)i++)
{
for (int j = 0j <map.GetLength(1)j++)
{
map[i, j] = SetNumber(i, j)
}
}
}
//展示地图
public void ShowMap()
{
for (int i = 0i <map.GetLength(0)i++)
{
Console.Write(" ")
for (int j = 0j <map.GetLength(1)j++)
{
Console.Write($"{map[i, j]} ")
}
Console.WriteLine()
//Console.WriteLine()
}
}
//创建游戏列表
public void CreatGameMap(int x, int y)
{
gameMap = new string[x, y]
//填充游戏界面:
for (int i = 0i <gameMap.GetLength(0)i++)
{
for (int j = 0j <gameMap.GetLength(1)j++)
{
gameMap[i, j] = "■"
}
}
}
//打印游戏界面
public void ShowGameMap()
{
for (int i = 0i <gameMap.GetLength(0)i++)
{
Console.Write($"{i} ")
for (int j = 0j <gameMap.GetLength(1)j++)
{
Console.Write($"{gameMap[i, j]} ")
}
Console.WriteLine()
}
}
//界面转换
public string MapChange(int x,int y)
{
if (map[x,y]==9)
{
return "☆"
}
else if (map[x,y]==0)
{
return "□"
}
else
{
return map[x, y].ToString()+" "
}
}
//交互事件
public void Click(int x, int y)
{
if (map[x,y]==9)
{
gameState = 2
Console.WriteLine( $"游戏结束!")
}
else if (map[x,y]==0)
{
//递归
AutoClick(x, y )
}
else
{
//继续游戏
}
gameMap[x, y] = MapChange(x, y)
}
public void AutoClick(int x, int y)
{
if (gameMap[x,y]== "■")
{
if (map[x, y] == 9)
{
//Console.WriteLine($"{x} {y}检测到地雷")
return
}
else if (map[x, y] == 0)
{
//Console.WriteLine($"{x} {y}开始递归")
gameMap[x, y] = MapChange(x, y)
//递归
if (x - 1 >= 0 &&y - 1 >= 0) { AutoClick(x - 1, y - 1)}//左上
if (y - 1 >= 0 ) { AutoClick(x, y - 1)}//左
if (x + 1 <= this.x - 1 &&y - 1 >= 0) { AutoClick(x + 1, y - 1)}//左下
if (x - 1 >= 0 ) { AutoClick(x - 1, y)}//左下
if (x - 1 >= 0 &&y + 1 <= this.y - 1) { AutoClick(x - 1, y + 1)}//
if (y + 1 <= this.y - 1 ) { AutoClick(x, y + 1)}
if (x + 1 <= this.x - 1 ) { AutoClick(x + 1, y)}
if (x + 1 <= this.x - 1 &&y + 1 <= this.y - 1) { AutoClick(x + 1, y + 1)}
}
else
{
//Console.WriteLine($"{x} {y}检测到数字")
//继续游戏
gameMap[x, y] = MapChange(x, y)
return
}
}
}
//白块递归
//判断游戏输赢
public void iswin()
{
n = 0
for (int i = 0i <gameMap.GetLength(0)i++)
{
for (int j = 0j <gameMap.GetLength(1)j++)
{
if (gameMap[i, j]== "■")
{
n++
}
}
//Console.WriteLine()
}
if (n<=p)
{
gameState = 1
}
}
}
class Program
{
static void Main(string[] args)
{
Cmap map = new Cmap(16, 30)
//map.ShowMap()
int x = 0
int y = 0
while (map.gameState == 0)
{
map.ShowGameMap()
Console.WriteLine($"请输入您要扫描的坐标:")
x = int.Parse(Console.ReadLine())
y = int.Parse(Console.ReadLine())
map.Click(x, y)
Console.ReadLine()
map.iswin()
if (map.gameState == 1)
{
Console.WriteLine("恭喜你游戏胜利")
}
if (map.gameState == 2)
{
Console.WriteLine("很抱歉游戏失败")
}
if (map.gameState == 0)
{
Console.WriteLine("游戏继续")
}
Console.ReadLine()
Console.Clear()
}
}
}
}
1.首先我们需要知道扫雷游戏程序设计的本质,用函数实现游戏的过程,先分一个大的模块,接着去分成小的模块,比如先去设置九宫格棋盘,再去初始化二维数消谈物组,设置地雷的数量,为避免重复要去扫雷,根据每个侍蔽坐标位置的数字显示判断。
2.与布雷的设计流程一样,也要将雷阵设计成(A+2)×(A+2);因拿液为这个地雷阵为玩家雷阵,为了增加神秘性,可以将它其初始化为字符*,或是其他字符也可以。
3.游戏设计中,#代表地雷,而0代表没有地雷,每个坐标位置的数字代表周围8个格子的地雷数量。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)