软件编程
位置:首页>> 软件编程>> C#编程>> C#仿Windows XP自带的扫雷游戏

C#仿Windows XP自带的扫雷游戏

作者:敲代码两年半的练习生  发布时间:2023-07-30 07:40:48 

标签:C#,扫雷游戏

本文实例为大家分享了C#仿Windows XP自带的扫雷游戏的具体代码,供大家参考,具体内容如下

1 题目描述:模仿Windows XP自带的扫雷游戏

定义一个30×30的二维数组,模仿Windows XP自带的扫雷游戏对这个二维数组进行随机布雷,要求至少布雷30个。游戏规则是:某个元素的值是一周(相邻8个位置)存在的地雷的个数。

2 源码详解


using System;
using System.Collections;

namespace Csharp5_3
{
   class Program
   {
       static void Main(string[] args)
       {
           ArrayList arrayList = new();
           ArrayList arrayList_map = new();
           Random rd = new();
           for (int i = 0; i < 900; i++)
           {
               if (rd.Next() % 20 == 0)
               {
                   arrayList.Add(1);
               }
               else
               {
                   arrayList.Add(0);
               }
               arrayList_map.Add(0);
           }
           for (int i = 0; i < 30; i++)
           {
               for (int j = 0; j < 30; ++j)
               {
                   Console.Write(arrayList[i * 30 + j]);
                   Console.Write(" ");
               }
               Console.WriteLine();
           }
           Console.WriteLine();

for ( int i = 0; i < 30; i ++ )
           {
               for ( int j = 0; j < 30; j ++ )
               {
                   // 判断八个方位是否有雷,将雷相加
                   // 判断上
                   if (((i - 1) * 30 + j) >= 0 && ((i - 1) * 30 + j) < 900 && j != 0) // 边界值判断
                   {
                       if (Convert.ToInt32(arrayList[i * 30 + j - 30]) == 1)
                       {
                           arrayList_map[i * 30 + j] = Convert.ToInt32(arrayList[i * 30 + j]) + 1;
                       }
                   }

// 判断下
                   if (((i + 1) * 30 + j) >= 0 && ((i + 1) * 30 + j) < 900 && j != 29) // 边界值判断
                   {
                       if (Convert.ToInt32(arrayList[i * 30 + j + 30]) == 1)
                       {
                           arrayList_map[i * 30 + j] = Convert.ToInt32(arrayList[i * 30 + j]) + 1;
                       }
                   }

// 判断左
                   if ((i * 30 + j - 1) >= 0 && (i * 30 + j -1) < 900 && j != 0) // 边界值判断
                   {
                       if (Convert.ToInt32(arrayList[i * 30 + j -1]) == 1)
                       {
                           arrayList_map[i * 30 + j] = Convert.ToInt32(arrayList[i * 30 + j]) + 1;
                       }
                   }

// 判断右
                   if ((i * 30 + j + 1) >= 0 && (i * 30 + j + 1) < 900 && j != 29) // 边界值判断
                   {
                       if (Convert.ToInt32(arrayList[i * 30 + j + 1]) == 1)
                       {
                           arrayList_map[i * 30 + j] = Convert.ToInt32(arrayList[i * 30 + j]) + 1;
                       }
                   }

// 判断左上
                   if (((i - 1) * 30 + j -1 ) >= 0 && ((i - 1) * 30 + j - 1) < 900 && j != 0) // 边界值判断
                   {
                       if (Convert.ToInt32(arrayList[(i - 1) * 30 + j - 1]) == 1)
                       {
                           arrayList_map[i * 30 + j] = Convert.ToInt32(arrayList[i * 30 + j]) + 1;
                       }
                   }

// 判断左下
                   if (((i + 1) * 30 + j - 1) >= 0 && ((i + 1) * 30 + j - 1) < 900 && j != 29) // 边界值判断
                   {
                       if (Convert.ToInt32(arrayList[(i + 1) * 30 + j - 1]) == 1)
                       {
                           arrayList_map[i * 30 + j] = Convert.ToInt32(arrayList[i * 30 + j]) + 1;
                       }
                   }

// 判断右上
                   if (((i - 1) * 30 + j + 1) >= 0 && ((i - 1) * 30 + j + 1) < 900 && j != 0) // 边界值判断
                   {
                       if (Convert.ToInt32(arrayList[(i - 1) * 30 + j + 1]) == 1)
                       {
                           arrayList_map[i * 30 + j] = Convert.ToInt32(arrayList[i * 30 + j]) + 1;
                       }
                   }

// 判断右下
                   if (((i + 1) * 30 + j + 1) >= 0 && ((i + 1) * 30 + j + 1) < 900 && j != 29) // 边界值判断
                   {
                       if (Convert.ToInt32(arrayList[(i + 1) * 30 + j + 1]) == 1)
                       {
                           arrayList_map[i * 30 + j] = Convert.ToInt32(arrayList[i * 30 + j]) + 1;
                       }
                   }
               }
           }
           for (int i = 0; i < 30; i++)
           {
               for (int j = 0; j < 30; ++j)
               {
                   Console.Write(arrayList_map[i * 30 + j]);
                   Console.Write(" ");
               }
               Console.WriteLine();
           }
           Console.Read();
       }
   }
}

3 实现效果

C#仿Windows XP自带的扫雷游戏

来源:https://blog.csdn.net/Gyangxixi/article/details/115347458

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com