初学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 level = 0; //游戏难度static int Win = 0; //根据难度赋值,用来判断是否完成游戏static string[,] LendMine = new string[9, 9];//二维数组表示游戏界面,横纵各9格static string[,] LendMine2 = new string[16, 16];//横纵各16格static Random rnd = new Random();static bool b = true;static void Main(string[] args){Console.Write("请选择难度(1简单,2中级):");level = 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 (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){Show(); //显示光标位置的数字}else{SetCursorPos(ck); //移动光标}}} while (b);#endregion}public static void Level1(){#region产生地雷int[] a = new int[10];int[] c = new int[10];for (int i = 0; i <= 9; i++) //设置地雷位置,总共10个,用*表示{int H = rnd.Next(0, 9);int S = rnd.Next(0, 9);a[i] = H;c[i] = S;for (int j = 0; j < i; j++){if (a[j] == H && c[j] == S)//判断第i个地雷位置是否与之前的有重复 {i--; //i-1,下一个循环重新产生第i个地雷坐标break;}}LendMine[H, S] = "*";}#endregion#region编辑数字分布for (int i = 0; i < 9; i++) //横纵9*9=81格{ //用LendMine[i,j]表示格子,存储的元素为数字或地雷("*") for (int j = 0; j < 9; j++){int n = 0;if(LendMine[i, j] != "*") //判断此格周围的地雷数,将数字存在此格LendMine[i,j]中{#regionif (i == 0){if (j == 0) //位于左上顶点的格子{if (LendMine[i + 1, j] == "*"){ n++; }if (LendMine[i + 1, j + 1] == "*"){ n++; }if (LendMine[i, j + 1] == "*"){ n++; }LendMine[i, j] = string.Format("{0}", n);}if (j == 8) //右上顶点{if (LendMine[i + 1, j] == "*"){ n++; }if (LendMine[i + 1, j - 1] == "*"){ n++; }if (LendMine[i, j - 1] == "*"){ n++; }LendMine[i, j] = string.Format("{0}", n);}if (j > 0 && j < 8) //上边界{if (LendMine[i + 1, j] == "*"){ n++; }if (LendMine[i + 1, j + 1] == "*"){ n++; }if (LendMine[i + 1, j - 1] == "*"){ n++; }if (LendMine[i, j + 1] == "*"){ n++; }if (LendMine[i, j - 1] == "*"){ n++; }LendMine[i, j] = string.Format("{0}", n);}}#endregion#regionif (i == 8){if (j == 0) //左下顶点{if (LendMine[i - 1, j] == "*"){ n++; }if (LendMine[i - 1, j + 1] == "*"){ n++; }if (LendMine[i, j + 1] == "*"){ n++; }LendMine[i, j] = string.Format("{0}", n); }if (j == 8) //右下顶点{if (LendMine[i - 1, j] == "*"){ n++; }if (LendMine[i - 1, j - 1] == "*"){ n++; }if (LendMine[i, j - 1] == "*"){ n++; }LendMine[i, j] = string.Format("{0}", n); }if (j > 0 && j < 8) //下边界{if (LendMine[i - 1, j] == "*"){ n++; }if (LendMine[i - 1, j + 1] == "*"){ n++; }if (LendMine[i - 1, j - 1] == "*"){ n++; }if (LendMine[i, j + 1] == "*"){ n++; }if (LendMine[i, j - 1] == "*"){ n++; }LendMine[i, j] = string.Format("{0}", n); }}#endregion#regionif (j == 0 && i > 0 && i < 8) //左边界{if (LendMine[i + 1, j] == "*"){ n++; }if (LendMine[i + 1, j + 1] == "*"){ n++; }if (LendMine[i - 1, j] == "*"){ n++; }if (LendMine[i - 1, j + 1] == "*"){ n++; }if (LendMine[i, j + 1] == "*"){ n++; }LendMine[i, j] = string.Format("{0}", n);}if (j == 8 && i > 0 && i < 8) //右边界{if (LendMine[i + 1, j] == "*"){ n++; }if (LendMine[i + 1, j - 1] == "*"){ n++; }if (LendMine[i - 1, j] == "*"){ n++; }if (LendMine[i - 1, j - 1] == "*"){ n++; }if (LendMine[i, j - 1] == "*"){ n++; }LendMine[i, j] = string.Format("{0}", n);}#endregion#region i>0&&i<8&&j>0&&j<8if (i > 0 && i < 8 && j > 0 && j < 8) //中间{//n = 0;if (LendMine[i - 1, j] == "*"){ n++; }if (LendMine[i - 1, j - 1] == "*"){ n++; }if (LendMine[i - 1, j + 1] == "*"){ n++; }if (LendMine[i, j + 1] == "*"){ n++; }if (LendMine[i, j - 1] == "*"){ n++; }if (LendMine[i + 1, j] == "*"){ n++; }if (LendMine[i + 1, j + 1] == "*"){ n++; }if (LendMine[i + 1, j - 1] == "*"){ n++; }LendMine[i, j] = string.Format("{0}", n);}#endregion}}}#endregion}public static void Level2(){#region产生地雷int[] a = new int[40];int[] c = new int[40];for (int i = 0; i < 40; i++) //设置地雷位置,总共40个,用*表示{int H = rnd.Next(0, 16);int S = rnd.Next(0, 16);a[i] = H;c[i] = S;for (int j = 0; j < i; j++){if (a[j] == H && c[j] == S)//判断第i个地雷位置是否与之前的有重复 {i--; //i-1,下一个循环重新产生第i个地雷坐标break;}}LendMine2[H, S] = "*";}#endregion#region编辑数字分布for (int i = 0; i < 16; i++) //横纵16*16=256格{ //用LendMine[i,j]表示格子,存储的元素为数字或地雷("*")for (int j = 0; j < 16; j++){int n = 0;if (LendMine2[i, j] != "*") //判断此格周围的地雷数,将数字存在此格LendMine[i,j]中{#regionif (i == 0){if (j == 0) //位于左上顶点的格子{if (LendMine2[i + 1, j] == "*"){ n++; }if (LendMine2[i + 1, j + 1] == "*"){ n++; }if (LendMine2[i, j + 1] == "*"){ n++; }LendMine2[i, j] = string.Format("{0}", n);}if (j == 15) //右上顶点{if (LendMine2[i + 1, j] == "*"){ n++; }if (LendMine2[i + 1, j - 1] == "*"){ n++; }if (LendMine2[i, j - 1] == "*"){ n++; }LendMine2[i, j] = string.Format("{0}", n);}if (j > 0 && j < 15) //上边界{if (LendMine2[i + 1, j] == "*"){ n++; }if (LendMine2[i + 1, j + 1] == "*"){ n++; }if (LendMine2[i + 1, j - 1] == "*"){ n++; }if (LendMine2[i, j + 1] == "*"){ n++; }if (LendMine2[i, j - 1] == "*"){ n++; }LendMine2[i, j] = string.Format("{0}", n);}}#endregion#regionif (i == 15){if (j == 0) //左下顶点{if (LendMine2[i - 1, j] == "*"){ n++; }if (LendMine2[i - 1, j + 1] == "*"){ n++; }if (LendMine2[i, j + 1] == "*"){ n++; }LendMine2[i, j] = string.Format("{0}", n); }if (j == 15) //右下顶点{if (LendMine2[i - 1, j] == "*"){ n++; }if (LendMine2[i - 1, j - 1] == "*"){ n++; }if (LendMine2[i, j - 1] == "*"){ n++; }LendMine2[i, j] = string.Format("{0}", n); }if (j > 0 && j < 15) //下边界{if (LendMine2[i - 1, j] == "*"){ n++; }if (LendMine2[i - 1, j + 1] == "*"){ n++; }if (LendMine2[i - 1, j - 1] == "*"){ n++; }if (LendMine2[i, j + 1] == "*"){ n++; }if (LendMine2[i, j - 1] == "*"){ n++; }LendMine2[i, j] = string.Format("{0}", n); }}#endregion#regionif (j == 0 && i > 0 && i < 15) //左边界{if (LendMine2[i + 1, j] == "*"){ n++; }if (LendMine2[i + 1, j + 1] == "*"){ n++; }if (LendMine2[i - 1, j] == "*"){ n++; }if (LendMine2[i - 1, j + 1] == "*"){ n++; }if (LendMine2[i, j + 1] == "*"){ n++; }LendMine2[i, j] = string.Format("{0}", n);}if (j == 15 && i > 0 && i < 15) //右边界{if (LendMine2[i + 1, j] == "*"){ n++; }if (LendMine2[i + 1, j - 1] == "*"){ n++; }if (LendMine2[i - 1, j] == "*"){ n++; }if (LendMine2[i - 1, j - 1] == "*"){ n++; }if (LendMine2[i, j - 1] == "*"){ n++; }LendMine2[i, j] = string.Format("{0}", n); }#endregion#region i>0&&i<15&&j>0&&j<15if (i > 0 && i < 15 && j > 0 && j < 15) //中间 {//n = 0;if (LendMine2[i - 1, j] == "*"){ n++; }if (LendMine2[i - 1, j - 1] == "*"){ n++; }if (LendMine2[i - 1, j + 1] == "*"){ n++; }if (LendMine2[i, j + 1] == "*"){ n++; }if (LendMine2[i, j - 1] == "*"){ n++; }if (LendMine2[i + 1, j] == "*"){ n++; }if (LendMine2[i + 1, j + 1] == "*"){ n++; }if (LendMine2[i + 1, j - 1] == "*"){ n++; }LendMine2[i, j] = string.Format("{0}", n); }#endregion}}}#endregion}public static void SetCursorPos(ConsoleKey ck){if (ck == ConsoleKey.RightArrow){ rl++; }if (ck == ConsoleKey.LeftArrow){ rl--; }if (ck == ConsoleKey.UpArrow){ ud--; }if (ck == ConsoleKey.DownArrow){ ud++; }if (ck == ConsoleKey.R){ Console.Write("@"); } //按R显示@,玩家用来标记地雷位置Console.CursorLeft = rl;Console.CursorTop = ud;}public static void Show(){switch(level){case 1:if (LendMine[rl, ud-1] == "*"){Console.WriteLine("你输了");b = false;}else{Console.Write(LendMine[rl, ud-1]);Console.CursorLeft = rl;Console.CursorTop = ud;Win--;if (Win == 0) //Win=0.表示所有无雷区域已找出 {Console.WriteLine("你完成了游戏");}}break;case 2: if (LendMine2[rl, ud - 1] == "*"){Console.WriteLine("你输了");b = false;}else{Console.Write(LendMine2[rl, ud - 1]);Console.CursorLeft = rl;Console.CursorTop = ud;Win--;if (Win == 0){Console.WriteLine("你完成了游戏");}}break;default: break;}}}}。