用C#写的扫雷程序源代码_第1页
用C#写的扫雷程序源代码_第2页
用C#写的扫雷程序源代码_第3页
用C#写的扫雷程序源代码_第4页
用C#写的扫雷程序源代码_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、初学C#一周,老师布置了个用C#编写仿Windows扫雷游戏的作业,我用了三天才弄出来,分享出来自我满足一下。程序是用vs 2008 控制台应用程序编的。界面有点粗糙,基本的功能倒是实现了。using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace saolei class Program /说明 方向键控制方向,空格键显示,按R标记地雷 static int rl = 0; /rl横坐标 static int ud = 0; /ud纵坐标 static int leve

2、l = 0; /游戏难度 static int Win = 0; /根据难度赋值,用来判断是否完成游戏 static string, LendMine = new string9, 9;/二维数组表示游戏界面,横纵各9格 static string, LendMine2 = new string16, 16;/ 横纵各16格 static Random rnd = new Random(); static bool b = true; static void Main(string args) Console.Write("请选择难度(1简单,2中级):"); level

3、= int.Parse(Console.ReadLine(); switch (level) case 1: Level1(); Win = 71; break; case 2: Level2(); Win = 216; break; default: break; #region 绘制界面 switch (level) case 1: for (int i = 0; i < 9; i+) for (int j = 0; j < 9; j+) Console.Write("H"); Console.WriteLine(); break; case 2: for

4、(int i = 0; i < 16; i+) for (int j = 0; j < 16; j+) Console.Write("H"); Console.WriteLine(); break; default: break; #endregion #region 进行游戏 Console.SetCursorPosition(0, 0);/光标初始位置 do if (Console.KeyAvailable) ConsoleKey ck = Console.ReadKey(true).Key; if (ck = ConsoleKey.Spacebar) Sh

5、ow(); /显示光标位置的数字 else SetCursorPos(ck); /移动光标 while (b); #endregion public static void Level1() #region 产生地雷 int a = new int10; int c = new int10; for (int i = 0; i <= 9; i+) /设置地雷位置,总共10个,用*表示 int H = rnd.Next(0, 9); int S = rnd.Next(0, 9); ai = H; ci = S; for (int j = 0; j < i; j+) if (aj =

6、H && cj = S)/判断第i个地雷位置是否与之前的有重复 i-; /i-1,下一个循环重新产生第i个地雷坐标 break; LendMineH, S = "*" #endregion #region 编辑数字分布 for (int i = 0; i < 9; i+) /横纵9*9=81格 /用LendMinei,j表示格子,存储的元素为数字或地雷("*") for (int j = 0; j < 9; j+) int n = 0; if (LendMinei, j != "*") /判断此格周围的地雷

7、数,将数字存在此格LendMinei,j中 #region if (i = 0) if (j = 0) /位于左上顶点的格子 if (LendMinei + 1, j = "*") n+; if (LendMinei + 1, j + 1 = "*") n+; if (LendMinei, j + 1 = "*") n+; LendMinei, j = string.Format("0", n); if (j = 8) /右上顶点 if (LendMinei + 1, j = "*") n+;

8、if (LendMinei + 1, j - 1 = "*") n+; if (LendMinei, j - 1 = "*") n+; LendMinei, j = string.Format("0", n); if (j > 0 && j < 8) /上边界 if (LendMinei + 1, j = "*") n+; if (LendMinei + 1, j + 1 = "*") n+; if (LendMinei + 1, j - 1 = "*&qu

9、ot;) n+; if (LendMinei, j + 1 = "*") n+; if (LendMinei, j - 1 = "*") n+; LendMinei, j = string.Format("0", n); #endregion #region if (i = 8) if (j = 0) /左下顶点 if (LendMinei - 1, j = "*") n+; if (LendMinei - 1, j + 1 = "*") n+; if (LendMinei, j + 1 = &

10、quot;*") n+; LendMinei, j = string.Format("0", n); if (j = 8) /右下顶点 if (LendMinei - 1, j = "*") n+; if (LendMinei - 1, j - 1 = "*") n+; if (LendMinei, j - 1 = "*") n+; LendMinei, j = string.Format("0", n); if (j > 0 && j < 8) /下边界

11、if (LendMinei - 1, j = "*") n+; if (LendMinei - 1, j + 1 = "*") n+; if (LendMinei - 1, j - 1 = "*") n+; if (LendMinei, j + 1 = "*") n+; if (LendMinei, j - 1 = "*") n+; LendMinei, j = string.Format("0", n); #endregion #region if (j = 0 &

12、& i > 0 && i < 8) /左边界 if (LendMinei + 1, j = "*") n+; if (LendMinei + 1, j + 1 = "*") n+; if (LendMinei - 1, j = "*") n+; if (LendMinei - 1, j + 1 = "*") n+; if (LendMinei, j + 1 = "*") n+; LendMinei, j = string.Format("0"

13、;, n); if (j = 8 && i > 0 && i < 8) /右边界 if (LendMinei + 1, j = "*") n+; if (LendMinei + 1, j - 1 = "*") n+; if (LendMinei - 1, j = "*") n+; if (LendMinei - 1, j - 1 = "*") n+; if (LendMinei, j - 1 = "*") n+; LendMinei, j = stri

14、ng.Format("0", n); #endregion #region i>0&&i<8&&j>0&&j<8 if (i > 0 && i < 8 && j > 0 && j < 8) /中间 /n = 0; if (LendMinei - 1, j = "*") n+; if (LendMinei - 1, j - 1 = "*") n+; if (LendMinei - 1, j +

15、1 = "*") n+; if (LendMinei, j + 1 = "*") n+; if (LendMinei, j - 1 = "*") n+; if (LendMinei + 1, j = "*") n+; if (LendMinei + 1, j + 1 = "*") n+; if (LendMinei + 1, j - 1 = "*") n+; LendMinei, j = string.Format("0", n); #endregion

16、#endregion public static void Level2() #region 产生地雷 int a = new int40; int c = new int40; for (int i = 0; i < 40; i+) /设置地雷位置,总共40个,用*表示 int H = rnd.Next(0, 16); int S = rnd.Next(0, 16); ai = H; ci = S; for (int j = 0; j < i; j+) if (aj = H && cj = S)/判断第i个地雷位置是否与之前的有重复 i-; /i-1,下一个循环重

17、新产生第i个地雷坐标 break; LendMine2H, S = "*" #endregion #region 编辑数字分布 for (int i = 0; i < 16; i+) /横纵16*16=256格 /用LendMinei,j表示格子,存储的元素为数字或地雷("*") for (int j = 0; j < 16; j+) int n = 0; if (LendMine2i, j != "*") /判断此格周围的地雷数,将数字存在此格LendMinei,j中 #region if (i = 0) if (j =

18、 0) /位于左上顶点的格子 if (LendMine2i + 1, j = "*") n+; if (LendMine2i + 1, j + 1 = "*") n+; if (LendMine2i, j + 1 = "*") n+; LendMine2i, j = string.Format("0", n); if (j = 15) /右上顶点 if (LendMine2i + 1, j = "*") n+; if (LendMine2i + 1, j - 1 = "*")

19、 n+; if (LendMine2i, j - 1 = "*") n+; LendMine2i, j = string.Format("0", n); if (j > 0 && j < 15) /上边界 if (LendMine2i + 1, j = "*") n+; if (LendMine2i + 1, j + 1 = "*") n+; if (LendMine2i + 1, j - 1 = "*") n+; if (LendMine2i, j + 1 = &q

20、uot;*") n+; if (LendMine2i, j - 1 = "*") n+; LendMine2i, j = string.Format("0", n); #endregion #region if (i = 15) if (j = 0) /左下顶点 if (LendMine2i - 1, j = "*") n+; if (LendMine2i - 1, j + 1 = "*") n+; if (LendMine2i, j + 1 = "*") n+; LendMine2i

21、, j = string.Format("0", n); if (j = 15) /右下顶点 if (LendMine2i - 1, j = "*") n+; if (LendMine2i - 1, j - 1 = "*") n+; if (LendMine2i, j - 1 = "*") n+; LendMine2i, j = string.Format("0", n); if (j > 0 && j < 15) /下边界 if (LendMine2i - 1, j

22、 = "*") n+; if (LendMine2i - 1, j + 1 = "*") n+; if (LendMine2i - 1, j - 1 = "*") n+; if (LendMine2i, j + 1 = "*") n+; if (LendMine2i, j - 1 = "*") n+; LendMine2i, j = string.Format("0", n); #endregion #region if (j = 0 && i > 0

23、&& i < 15) /左边界 if (LendMine2i + 1, j = "*") n+; if (LendMine2i + 1, j + 1 = "*") n+; if (LendMine2i - 1, j = "*") n+; if (LendMine2i - 1, j + 1 = "*") n+; if (LendMine2i, j + 1 = "*") n+; LendMine2i, j = string.Format("0", n); i

24、f (j = 15 && i > 0 && i < 15) /右边界 if (LendMine2i + 1, j = "*") n+; if (LendMine2i + 1, j - 1 = "*") n+; if (LendMine2i - 1, j = "*") n+; if (LendMine2i - 1, j - 1 = "*") n+; if (LendMine2i, j - 1 = "*") n+; LendMine2i, j = stri

25、ng.Format("0", n); #endregion #region i>0&&i<15&&j>0&&j<15 if (i > 0 && i < 15 && j > 0 && j < 15) /中间 /n = 0; if (LendMine2i - 1, j = "*") n+; if (LendMine2i - 1, j - 1 = "*") n+; if (LendMine2i -

26、1, j + 1 = "*") n+; if (LendMine2i, j + 1 = "*") n+; if (LendMine2i, j - 1 = "*") n+; if (LendMine2i + 1, j = "*") n+; if (LendMine2i + 1, j + 1 = "*") n+; if (LendMine2i + 1, j - 1 = "*") n+; LendMine2i, j = string.Format("0", n); #endregion #endregion public st

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论