实例介绍
【实例简介】
【实例截图】
【核心代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | using System; using System.Collections; using System.Collections.Generic; using System.Drawing; using System.Linq; namespace Mine { /// <summary> /// 游戏对象 /// </summary> public sealed class Game { #region 只读字段 //游戏数据 private readonly Square[,] _gameData; //游戏等级 private readonly GameLevel _level; //雷区数 private readonly int _minesCount; //游戏区与窗口边界的偏移量 private readonly Size _gameFieldOffset; //窗口图面 private readonly Graphics _wndGraphics; //缓冲区 private readonly Bitmap _buffer; //缓冲区图面 private readonly Graphics _bufferGraphics; //随机生成器 private static readonly Random Rnd = new Random(); #endregion 只读字段 #region 可写字段 //游戏是否开始 private bool _gameStarted; //游戏是否结束 private bool _gameStoped; #endregion 可写字段 #region 常量 //边界常量 public const int MaxWidth = 24, //最大宽度 MaxHeight = 30, //最大高度 MinWidth = 9, //最小宽度 MinHeight = 9, //最小高度 MinMinesCount = 10; //最小雷数 #endregion 常量 #region 构造器 /// <summary> /// 从预设等级初始化游戏 /// </summary> /// <param name="level">预设等级</param> /// <param name="g">图面</param> /// <param name="gameFieldOffset">游戏区偏移</param> public Game(GameLevel level, Graphics g, Size gameFieldOffset) { _level = level; _gameFieldOffset = gameFieldOffset; _wndGraphics = g; //初始化游戏数据 switch (level) { case GameLevel.Beginner: _gameData = new Square[9, 9]; _minesCount = 10; break ; case GameLevel.Intermediate: _gameData = new Square[16, 16]; _minesCount = 40; break ; case GameLevel.Expert: _gameData = new Square[30, 16]; _minesCount = 99; break ; case GameLevel.Customize: break ; default : throw new ArgumentOutOfRangeException( "level" , level, null ); } //初始化缓冲区 _buffer = new Bitmap(_gameData.GetLength(0) * Square.SquareSize, _gameData.GetLength(1) * Square.SquareSize); //创建缓冲区图面 _bufferGraphics = Graphics.FromImage(_buffer); } /// <summary> /// 创建自定义游戏实例 /// </summary> /// <param name="gameSize">游戏宽高</param> /// <param name="minesCount">雷数</param> /// <param name="g">图面</param> /// <param name="gameFieldOffset">游戏区偏移</param> /// <exception cref="ArgumentOutOfRangeException">游戏尺寸或雷数非法</exception> public Game(Size gameSize, int minesCount, Graphics g, Size gameFieldOffset) { _level = GameLevel.Customize; _gameFieldOffset = gameFieldOffset; _wndGraphics = g; //边界值验证 if (gameSize.Width > MaxWidth) gameSize.Width = MaxWidth; else if (gameSize.Width < MinWidth) gameSize.Width = MinWidth; if (gameSize.Height > MaxHeight) gameSize.Height = MaxHeight; else if (gameSize.Height < MinHeight) gameSize.Height = MinHeight; if (minesCount < MinMinesCount) minesCount = MinMinesCount; else if (minesCount > (gameSize.Width - 1) * (gameSize.Height - 1)) minesCount = (gameSize.Width - 1) * (gameSize.Height - 1); //初始化游戏数据 _gameData = new Square[gameSize.Width, gameSize.Height]; _minesCount = minesCount; //初始化缓冲区 _buffer = new Bitmap(_gameData.GetLength(0) * Square.SquareSize, _gameData.GetLength(1) * Square.SquareSize); //创建缓冲区图面 _bufferGraphics = Graphics.FromImage(_buffer); } #endregion 构造器 #region 公共实例方法 /// <summary> /// 开始游戏 /// </summary> public void Start() { //假设所有方块区均非雷区 for ( int i = 0; i < _gameData.GetLength(0); i ) for ( int j = 0; j < _gameData.GetLength(1); j ) _gameData[i, j] = new Square( new Point(i, j), false , 0); } /// <summary> /// 获取游戏区像素尺寸 /// </summary> /// <returns></returns> public Size GetGamePixcelSize() { return new Size(_gameData.GetLength(0) * Square.SquareSize, _gameData.GetLength(1) * Square.SquareSize); } /// <summary> /// 左键单击 /// </summary> /// <param name="pixelPt">鼠标单击像素点</param> /// <returns> /// 0:空白处 /// 1:获胜 /// -1:游戏结束 /// </returns> public int LeftClick(Point pixelPt) { if (_gameStoped) return 0; //相对于游戏区的像素点 Point gameFieldpt = new Point(pixelPt.X - _gameFieldOffset.Width, pixelPt.Y - _gameFieldOffset.Height); //游戏逻辑点(坐标) Point logicalPt = new Point(gameFieldpt.X / Square.SquareSize, gameFieldpt.Y / Square.SquareSize); //首次单击时布雷 if (!_gameStarted) Mine(logicalPt); //没有踩中雷区 if (!_gameData[logicalPt.X, logicalPt.Y].LeftClick()) { //如果是空白区,则递归相邻的所有空白区 if (_gameData[logicalPt.X, logicalPt.Y].MinesAround == 0) AutoOpenAround(logicalPt); //获胜判定:所有空白处均已打开 for ( int i = 0; i < _gameData.GetLength(0); i ) { for ( int j = 0; j < _gameData.GetLength(1); j ) { if (_gameData[i, j].Mined) continue ; if (_gameData[i, j].Status == Square.SquareStatus.Opened) continue ; return 0; } } //获胜 return 1; } //游戏结束 for ( int i = 0; i < _gameData.GetLength(0); i ) for ( int j = 0; j < _gameData.GetLength(1); j ) if (_gameData[i, j].Mined) _gameData[i, j].GameOver(logicalPt); else if (_gameData[i, j].Status == Square.SquareStatus.Marked) _gameData[i, j].IndicateMarkMissed(); return -1; } /// <summary> /// 右键单击 /// </summary> /// <param name="pixelPt">鼠标单击像素点</param> public void RightClick(Point pixelPt) { if (_gameStoped) return ; Point pt = new Point(pixelPt.X - _gameFieldOffset.Width, pixelPt.Y - _gameFieldOffset.Height); _gameData[pt.X / Square.SquareSize, pt.Y / Square.SquareSize].RightClick(); } /// <summary> /// 结束游戏 /// </summary> public void Stop() { _gameStoped = true ; } /// <summary> /// 绘制一帧 /// </summary> public void Draw() { for ( int i = 0; i < _gameData.GetLength(0); i ) for ( int j = 0; j < _gameData.GetLength(1); j ) _gameData[i, j].Draw(_bufferGraphics); _wndGraphics.DrawImage(_buffer, new Point(_gameFieldOffset.Width, _gameFieldOffset.Height)); } #endregion 公共实例方法 #region 私有实例方法 /// <summary> /// 布雷 /// </summary> /// <param name="startPt">首次单击点</param> private void Mine(Point startPt) { Size area = new Size(_gameData.GetLength(0), _gameData.GetLength(1)); List<Point> excluded = new List<Point> { startPt }; //随机创建雷区 for ( int i = 0; i < _minesCount; i ) { Point pt = GetRandomPoint(area, excluded); _gameData[pt.X, pt.Y] = new Square(pt, true , 0); excluded.Add(pt); } //创建非雷区 for ( int i = 0; i < _gameData.GetLength(0); i ) for ( int j = 0; j < _gameData.GetLength(1); j ) if (!_gameData[i, j].Mined) //非雷区 { int minesAround = EnumSquaresAround( new Point(i, j)).Cast<Square>().Count(square => square.Mined); //周围雷数 _gameData[i, j] = new Square( new Point(i, j), false , minesAround); } _gameStarted = true ; } /// <summary> /// 自动打开周围非雷区方块(递归) /// </summary> /// <param name="squarePt">原方块逻辑坐标</param> private void AutoOpenAround(Point squarePt) { //遍历周围方块 foreach (Square square in EnumSquaresAround(squarePt)) { if (square.Mined || square.Status == Square.SquareStatus.Marked || square.Status == Square.SquareStatus.Opened) continue ; square.LeftClick(); //打开 //周围无雷区 if (square.MinesAround == 0) AutoOpenAround(square.Location); //递归打开 } } /// <summary> /// 枚举周围所有方块区 /// </summary> /// <param name="squarePt">原方块区</param> /// <returns>枚举数</returns> private IEnumerable EnumSquaresAround(Point squarePt) { int i = squarePt.X, j = squarePt.Y; //周围所有方块区 for ( int x = i - 1; x <= i 1; x) //横向 { if (x < 0 || x >= _gameData.GetLength(0)) //越界 continue ; for ( int y = j - 1; y <= j 1; y) //纵向 { if (y < 0 || y >= _gameData.GetLength(1)) //越界 continue ; if (x == squarePt.X && y == squarePt.Y) //排除自身 continue ; yield return _gameData[x, y]; } } } #endregion 私有实例方法 #region 私有静态方法 /// <summary> /// 获取指定区域内的一个不在排除项列表中的随机点 /// </summary> /// <param name="areaSize">区域大小,指定了随机点坐标的范围</param> /// <param name="excluded">排除项</param> /// <returns>随机点</returns> /// <exception cref="ArgumentNullException">排除项为空</exception> private static Point GetRandomPoint(Size areaSize, List<Point> excluded) { if (excluded == null ) throw new ArgumentNullException( "excluded" ); int x, y; do { x = Rnd.Next(0, areaSize.Width); y = Rnd.Next(0, areaSize.Height); } while (excluded.Any(pt => pt.X == x && pt.Y == y)); return new Point(x, y); } #endregion 私有静态方法 } } |
好例子网口号:伸出你的我的手 — 分享!
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论