版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 船务测试题及答案
- 2026年达州市中考化学试卷(含答案及解析)
- 护理基本手法培训资料
- 护理专业预防医学中的慢性病预防社区干预
- 妇产科护理中的产科感染护理
- 护理妆容技巧与礼仪规范
- 护理基础知识概览
- 高标准农田建设项目检查实施细则
- 护理职业道德与伦理决策
- 安蔻妃:眼部问题的快速缓解技巧
- 2026四川甘孜州交通运输综合行政执法支队招聘行政执法辅助人员8人笔试题库及完整答案详解【名校卷】
- 2026云南昆明空港投资开发集团有限公司第二次招聘3人笔试模拟试题及答案详解
- 2026年环境保护知识竞赛试题库(附答案)
- 2026年二级造价师《土建工程实务》真题(附解析)
- 个人防护装备穿脱操作规范
- 2025年全国青少年信息素养大赛Scratch图形化编程挑战赛(小高组-复赛)真题(含答案)
- 销售谈判技巧指南与话术模板
- (2025年)高空作业考试习题及答案
- 2026年国开电大机械CAD-CAM形考试卷含完整答案详解【夺冠系列】
- 2025版《预防导尿管相关尿路感染(CAUTI)指南》解读课件
- 排涝站工作制度
评论
0/150
提交评论