C#仿Windows XP自带的扫雷游戏
作者:敲代码两年半的练习生 时间:2023-07-30 07:40:48
本文实例为大家分享了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 实现效果
来源:https://blog.csdn.net/Gyangxixi/article/details/115347458
标签:C#,扫雷游戏
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Eclipse+ADT+Android SDK搭建安卓开发环境的实现步骤
2021-12-20 19:59:32
![](https://img.aspxhome.com/file/2023/9/138589_0s.png)
SpringBoot项目部署到腾讯云的实现步骤
2023-01-01 16:58:55
![](https://img.aspxhome.com/file/2023/7/70557_0s.png)
Android仿美团分类下拉菜单实例代码
2022-09-04 09:59:02
![](https://img.aspxhome.com/file/2023/2/137112_0s.gif)
Java如何实现支付宝电脑支付基于servlet版本
2021-12-11 04:39:47
![](https://img.aspxhome.com/file/2023/7/81237_0s.png)
浅谈shiro的SecurityManager类结构
2022-08-25 14:13:32
![](https://img.aspxhome.com/file/2023/9/129969_0s.jpg)
关于SpringCloud的微服务结构及微服务远程调用
2021-11-06 20:11:45
![](https://img.aspxhome.com/file/2023/8/77088_0s.png)
Maven的安装配置详解
2023-11-24 08:52:05
![](https://img.aspxhome.com/file/2023/3/59323_0s.png)
Android 自定义view和属性动画实现充电进度条效果
2023-08-24 00:18:37
![](https://img.aspxhome.com/file/2023/4/96014_0s.gif)
IDEA2022.2的简介、下载与安装、配置教程
2023-01-13 21:21:03
![](https://img.aspxhome.com/file/2023/4/85914_0s.jpg)
android在连拍菜单中增加连拍张数选项功能实现代码
2023-05-27 19:08:38
SpringBoot实战之SSL配置详解
2022-08-03 19:38:17
![](https://img.aspxhome.com/file/2023/1/77331_0s.png)
解决Android Studio电脑不支持HAXM的问题
2022-05-28 04:48:51
![](https://img.aspxhome.com/file/2023/4/108314_0s.jpg)
Java线程组与未处理异常实例分析
2021-12-01 12:21:08
C#实现猜数字小游戏
2023-01-13 16:39:38
![](https://img.aspxhome.com/file/2023/3/91373_0s.jpg)
Unity之绕轴进行旋转的操作
2021-11-14 17:53:10
![](https://img.aspxhome.com/file/2023/9/104329_0s.jpg)
Java实现inputstream流的复制代码实例
2021-08-30 11:52:11
.net 随机生成汉字
2022-01-22 08:33:33
搭建简单的Spring-Data JPA项目
2023-04-05 01:06:30
Unity UGUI实现卡片椭圆方向滚动
2022-06-03 06:02:53
![](https://img.aspxhome.com/file/2023/2/77292_0s.jpg)
解决Android Studio 3.0 butterknife:7.0.1配置的问题
2021-08-20 09:23:05