//尝试在控制台下使用面向对象知识完成扫雷游戏,使用字符打印相关信息即可。
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 = 0; i < p; i++)
{
mineSet[i] = random.Next(0, mapLine.Length);
for (int j = 0; j < i; j++)
{
if (mineSet[j] == mineSet[i])
{
i--;
}
}
}
//一维数组初始化
for (int i = 0; i < mapLine.Length; i++)
{
mapLine[i] = 0;
}
//一维数组装雷
for (int i = 0; i < mineSet.Length; i++)
{
mapLine[mineSet[i]] = 9;
}
//将有雷的信息的一维数组撞填到二维数组里面
int conter = 0;
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < 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 = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
map[i, j] = SetNumber(i, j);
}
}
}
//展示地图
public void ShowMap()
{
for (int i = 0; i < map.GetLength(0); i++)
{
Console.Write(" ");
for (int j = 0; j < 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 = 0; i < gameMap.GetLength(0); i++)
{
for (int j = 0; j < gameMap.GetLength(1); j++)
{
gameMap[i, j] = "■";
}
}
}
//打印游戏界面
public void ShowGameMap()
{
for (int i = 0; i < gameMap.GetLength(0); i++)
{
Console.Write($"{i} ");
for (int j = 0; j < 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 = 0; i < gameMap.GetLength(0); i++)
{
for (int j = 0; j < 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();
}
}
}
}