c#带你玩扫雷(附源码)

  

扫雷游戏,大家都应该玩过吧!其实规则也很简单,可是我们想自己实现一个扫雷,我们应该怎么做呢?

  

 C #带你玩扫雷(附源码)
  

  

<强>步骤1:知晓游戏原理
  

  

扫雷就是要把所有非地雷的格子揭开即胜利;踩到地雷格子就算失败。游戏主区域由很多个方格组成。使用鼠标左键随机点击一个方格,方格即被打开并显示出方格中的数字;方格中数字则表示其周围的8个方格隐藏了几颗雷,如果点开的格子为空白格,即其周围有0颗雷,则其周围格子自动打开;如果其周围还有空白格,则会引发连锁反应,在你认为有雷的格子上,点击右键即可标记雷,如果一个已打开格子周围所有的雷已经正确标出,则可以在此格上同时点击鼠标左右键以打开其周围剩余的无雷格。
  

  

1代表1的上下左右及斜角合计有一颗雷,依次轮推,2则有2颗,3则有3颗。
  

  

在确实是炸弹的方格上点了旗子,就安全了,不是炸弹的被点了旗子,后面会被炸死的. .问号就先不确定这里有没有炸弹,不会存在点错了被炸死的状况. .

  

<强>步骤2:由step1可知,游戏由格子组成,翻译成代码语言就叫做数组,也就是游戏地图就是一个二维数组。格子对象,格子的值即当前雷的数量,那么此时我们暂定雷的数字标识为1。除此之外,格子对象还有是否被显示,显示当前雷数量等属性,那么我们大概可以定义这样一个类:
  

        公开课CellBlockRole   {///& lt; summary>///位于游戏地图中的坐标X点///& lt;/summary>   公共int X{得到;设置;}///& lt; summary>///位于游戏地图中的坐标点Y///& lt;/summary>   公共int Y{得到;设置;}///& lt; summary>///是否展示最后格子所代表的结果///& lt;/summary>   公共bool IsShowResult{得到;设置;}=false;///& lt; summary>///是否计算数字结果///& lt;/summary>   公共bool IsComputeResult{得到;设置;}=false;///& lt; summary>///是否已经展示过计算结果了///& lt;/summary>   公共bool IsHasShowComputed{得到;设置;}=false;///& lt; summary>///当前的格子的角色数字,1:地雷,其他当前雷的数量///& lt;/summary>   公共int数{设置;得到;}=0;///& lt; summary>///是否被国旗标识///& lt;/summary>   公共bool IsFlag{得到;设置;}=false;///& lt; summary>///是否是雷///& lt;/summary>   公共bool IsBoom=比;数量==1;      }      之前      

绘制游戏UI画面,见代码:

        使用系统;   使用System.Collections.Generic;   使用System.ComponentModel;   使用System.Drawing;   使用System.Data;   使用来;   使用text;   使用System.Threading;   使用System.Threading.Tasks;   使用System.Windows.Forms;   使用SweeperLibrary.Properties;   使用定时器=System.Threading.Timer;      名称空间SweeperLibrary   {   公共委托void> X轴坐标& lt;/param>///& lt;参数name=" posiY祝辞Y轴坐标& lt;/param>   私人空间ShowNeiborhoodCellRolesByPosi (int posiX int posiY)   {   gameMap posiY (posiX)。IsShowResult=true;   gameMap posiY (posiX)。IsHasShowComputed=true;   int boomCount=GetBoomCountInNeiborhood (posiX posiY);   如果(boomCount==0)//如果周围没有雷,则翻开所有8个方向的相关数字   {   for (int i=0;我& lt;MoveDirectionPoints.Length;我+ +)   {   int [] itemPosi=MoveDirectionPoints[我];   int rx=posiX + itemPosi [0],   ry=posiY + itemPosi [1];   bool isNotOutIndexRange=rx祝辞=0,,rx & lt;GameCellCount,,目前在=0,,ry & lt;GameCellCount;   如果(isNotOutIndexRange)//防止坐标溢出   {   gameMap[一](rx)。IsShowResult=true;   如果(! gameMap(一)(rx)。IsHasShowComputed,,gameMap[一](rx)。数量==0)   ShowNeiborhoodCellRolesByPosi (rx, ry);   }   }   }   }///& lt; summary>///获取某点附近的雷数量///& lt;/summary>///& lt;参数name=" posiX祝辞X轴坐标点& lt;/param>///& lt;参数name=" posiY祝辞Y轴坐标点& lt;/param>///& lt; returns> & lt;/returns>   私人int GetBoomCountInNeiborhood (int, int posiX posiY)   {   int boomCount=0;   for (int i=0;我& lt;MoveDirectionPoints.Length;我+ +)   {   int [] itemPosi=MoveDirectionPoints[我];   int rx=posiX + itemPosi [0],   ry=posiY + itemPosi [1];   bool isNotOutIndexRange=rx祝辞=0,,rx & lt;GameCellCount,,目前在=0,,ry & lt;GameCellCount;   如果(isNotOutIndexRange,,gameMap[一](rx) .IsBoom)//防止坐标溢出   {   boomCount + +;   }   }   返回boomCount;   }///& lt; summary>///计算每个格子的数字标识///& lt;/summary>///& lt;参数name=" posiX祝辞X轴坐标& lt;/param>///& lt;参数name=" posiY祝辞Y轴坐标& lt;/param>   私人空间MakeAllNumberComputeInCellRole (int posiX int posiY)   {   int boomCount=GetBoomCountInNeiborhood (posiX posiY);   如果(boomCount !=0)//如果周围没有雷,则计算周围的8个方向的格子   {   gameMap posiY (posiX)。数量=boomCount;   其他}   {   如果(! gameMap posiY [posiX] .IsBoom)   gameMap posiY (posiX)。数量=0;   }   gameMap posiY (posiX)。IsComputeResult=true;   for (int i=0;我& lt;MoveDirectionPoints.Length;我+ +)   {   int [] itemPosi=MoveDirectionPoints[我];   int rx=posiX + itemPosi [0],   ry=posiY + itemPosi [1];   bool isNotOutIndexRange=rx祝辞=0,,rx & lt;GameCellCount,,目前在=0,,ry & lt;GameCellCount;   如果(isNotOutIndexRange,,! gameMap[一](rx)。IsComputeResult,,! gameMap[一](rx) .IsBoom)//防止坐标溢出   {   MakeAllNumberComputeInCellRole (rx, ry);   }   }   }      }      }      

c#带你玩扫雷(附源码)