《C#控制台实现扫雷程序.docx》由会员分享,可在线阅读,更多相关《C#控制台实现扫雷程序.docx(14页珍藏版)》请在三一办公上搜索。
1、C#控制台实现扫雷程序秦申学 qinshenxue C#在控制台实现扫雷程序 最终运行结果如图所示 使用说明 玩法同系统自带扫雷游戏规则 按Enter键”踩有无雷” 按M键标记有雷区域 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace test class Program static void Main(string args) int n = int.Parse(Console.ReadLine);/用户输入扫雷二维数组大小 int, mine=ne
2、w intn,n;/定义一个扫雷二维数组 int, record = new intn, n;/定义一个记录用户当前判定情况的二维数组 int, amount = new intn, n;/记录以当前方块为中心的周围八个方块中雷的数量 int mc = 0; for (int i = 0; i n; i+) 秦申学 qinshenxue for (int j = 0; j n; j+) recordi, j = -2; Random r = new Random;/随机布雷 for (int i1 = 0; i1 n; i1+) for (int j1 = 0; j1 n; j1+) mine
3、i1, j1 = r.Next(0, 4)/3;/从0到2中产生随机数0,1其中0代表无雷,而1代表有雷 /遍历出雷的数量 for(int i2=0;i2n;i2+) for (int j2 = 0; j2 n; j2+) if (minei2, j2 = 1) mc+; /记录以当前方块为中心的周围八个方块中雷的数量的产生方法 int mine_count = 0;/雷的数量 for (int i = 0; i n; i+) for (int j = 0; j n; j+) if (minei, j != 1) if (i - 1 0 | j - 1 n - 1 | j + 1 n - 1)
4、 if (i - 1 0 & j - 1 = 0 & j + 1 = n - 1 & i - 1 0)/上边 if (minei, j - 1 = 1) mine_count+; if (minei + 1, j - 1 = 1) mine_count+; if (minei + 1, j = 1) mine_count+; if (minei + 1, j + 1 = 1) mine_count+; if (minei, j + 1 = 1) mine_count+; amounti, j = mine_count; mine_count = 0; if (i - 1 n - 1)/右上角
5、if (minei, j - 1 = 1) mine_count+; if (minei + 1, j - 1 = 1) mine_count+; if (minei + 1, j = 1) mine_count+; amounti, j = mine_count; mine_count = 0; if (j + 1 n - 1 & i - 1 = 0 & i + 1 n - 1 & j + 1 n - 1)/右下角 if (minei - 1, j - 1 = 1) 秦申学 qinshenxue mine_count+; if (minei - 1, j = 1) mine_count+;
6、if (minei, j - 1 = 1) mine_count+; amounti, j = mine_count; mine_count = 0; if (i + 1 n - 1 & j - 1 = 0 & j + 1 = n - 1)/下边 if (minei - 1, j - 1 = 1) mine_count+; if (minei, j - 1 = 1) mine_count+; if (minei - 1, j = 1) mine_count+; if (minei - 1, j + 1 = 1) mine_count+; if (minei, j + 1 = 1) mine_c
7、ount+; amounti, j = mine_count; mine_count = 0; if (j - 1 n - 1)/左下角 if (minei - 1, j = 1) mine_count+; if (minei - 1, j + 1 = 1) mine_count+; if (minei, j + 1 = 1) mine_count+; amounti, j = mine_count; mine_count = 0; if (j - 1 = 0 & i + 1 = n - 1)/左边 if (minei - 1, j = 1) mine_count+; if (minei -
8、1, j + 1 = 1) mine_count+; if (minei, j + 1 = 1) mine_count+; if (minei + 1, j + 1 = 1) mine_count+; 秦申学 qinshenxue if (minei + 1, j = 1) mine_count+; amounti, j = mine_count; mine_count = 0; else if (minei - 1, j - 1 = 1) mine_count+; if (minei - 1, j + 1 = 1) mine_count+; if (minei + 1, j - 1 = 1)
9、 mine_count+; if (minei + 1, j + 1 = 1) mine_count+; if (minei - 1, j = 1) mine_count+; if (minei + 1, j = 1) mine_count+; if (minei, j - 1 = 1) mine_count+; if (minei, j + 1 = 1) mine_count+; amounti, j = mine_count; mine_count = 0; else amounti, j = -1; /扫雷布局,灰色为判断区域 for (int i = 0; i n; i+)/行扫描 f
10、or (int j = 0; j n; j+)/列扫描 Console.BackgroundColor = ConsoleColor.Gray;/判断区域 Console.Write( );/空格代表 Console.BackgroundColor = ConsoleColor.Black;/返回系统默认颜色 Console.Write( );/将判断区域隔开 秦申学 qinshenxue Console.WriteLine(n);/两次换行 Console.CursorLeft = 0;/将光标定位到第0列 Console.CursorTop = 1;/将光标定位到第1行,因为用户输入占了一
11、行 int x = 0;/数组横坐标,就是第几列 int y = 0;/数组纵坐标,就是第几行 while (true) int mark_count = 0;/标记雷的数量 int remainder = 0;/剩余的数量 for (int i = 0; i n; i+) for (int j = 0; j n; j+) if (recordi, j = -2) remainder+;/ if (recordi, j = -1) mark_count+; if (remainder = 0 & mark_count = mc) Console.Clear; Console.WriteLine
12、(You Win); break; ConsoleKey ck = Console.ReadKey(true).Key;/获取用户按键 if(ck=ConsoleKey.DownArrow)/向下键 if (Console.CursorTop = 2 * n - 1)/判断是否会超出雷区范围 Console.CursorTop = 1; y = 0; else Console.CursorTop += 2; y+; if (ck = ConsoleKey.UpArrow)/向上键 if (Console.CursorTop = 1)/判断是否会超出雷区范围 秦申学 qinshenxue Con
13、sole.CursorTop = 2 * n - 1; y = n - 1;/数组的值改变 else Console.CursorTop -= 2; y-; if (ck = ConsoleKey.LeftArrow)/向左键 if (Console.CursorLeft = 0) Console.CursorLeft = 2 * n-2; x = n - 1; else Console.CursorLeft -= 2; x-;/数组的值改变 if(ck=ConsoleKey.RightArrow)/向右键 if (Console.CursorLeft = 2 * n - 2) Console
14、.CursorLeft = 0; x = 0; else Console.CursorLeft += 2; x+; if(ck=ConsoleKey.M)/标记雷区 Console.Clear;/清屏重新打印 Console.WriteLine(n); recordx, y = -1; for (int i = 0; i n; i+) for (int j = 0; j n; j+) 秦申学 qinshenxue if (recordj,i!=-2)/若满足当前的坐标值的话,就用红色背景标注 if (recordj, i = -1) Console.BackgroundColor = Cons
15、oleColor.Red; Console.Write( ); Console.BackgroundColor = ConsoleColor.Black; Console.Write( ); if (recordj, i != -1) Console.BackgroundColor = ConsoleColor.Gray; Console.ForegroundColor = ConsoleColor.Red; Console.Write(amountj, i); Console.ForegroundColor = ConsoleColor.Gray; Console.BackgroundCol
16、or = ConsoleColor.Black; Console.Write( ); else/否则仍然按照红色标注 Console.BackgroundColor = ConsoleColor.Gray; Console.Write( ); Console.BackgroundColor = ConsoleColor.Black; Console.Write( ); Console.WriteLine(n); /行循环 Console.CursorLeft = 2*x; Console.CursorTop = 2*y+1; if (ck = ConsoleKey.Enter)/判断光标所在位置是否有地雷 if (minex, y = 1) Console.Clear; Console.WriteLine(You lose); break; else 秦申学 qinshenxue Console.Clear;/清屏重新打印 Console.WriteLine(n); recordx, y = amountx, y; for (int i = 0; i n; i+) for (int j = 0; j n; j+) if (recordj,i!=-2