You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# 如何在数组中放置指定数量的随机地雷实现扫雷功能

扫雷游戏实现方案

完整可运行代码

using System;

namespace MinesweeperDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // 读取用户输入参数
            Console.Write("Enter the width of the field: ");
            int width = Convert.ToInt32(Console.ReadLine());
            
            Console.Write("Enter the length of the field: ");
            int length = Convert.ToInt32(Console.ReadLine());
            
            Console.Write("Enter the number of bombs: ");
            int bombCount = Convert.ToInt32(Console.ReadLine());
            
            // 非法输入校验
            if (bombCount >= width * length || bombCount < 0)
            {
                Console.WriteLine("错误:地雷数量需大于0且小于总格子数");
                return;
            }

            Random rand = new Random();
            char[,] rawField = new char[width, length]; // 存储原始雷区,仅包含_和*
            int[,] solvedField = new int[width, length]; // 存储解谜结果
            
            // 初始化所有格子为无雷状态
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < length; j++)
                {
                    rawField[i, j] = '_';
                }
            }
            
            // 随机放置指定数量地雷,保证数量完全匹配
            int placed = 0;
            while (placed < bombCount)
            {
                int x = rand.Next(width);
                int y = rand.Next(length);
                // 避免重复放雷
                if (rawField[x, y] != '*')
                {
                    rawField[x, y] = '*';
                    placed++;
                }
            }
            
            // 计算每个非雷格子的相邻地雷数
            // 8个相邻方向的坐标偏移量
            int[] dx = {-1, -1, -1, 0, 0, 1, 1, 1};
            int[] dy = {-1, 0, 1, -1, 1, -1, 0, 1};
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < length; j++)
                {
                    if (rawField[i, j] == '*')
                    {
                        solvedField[i, j] = -1; // 用-1标记地雷位置
                        continue;
                    }
                    int bombNum = 0;
                    // 遍历8个相邻方向
                    for (int k = 0; k < 8; k++)
                    {
                        int nx = i + dx[k];
                        int ny = j + dy[k];
                        // 校验相邻坐标是否在雷区范围内
                        if (nx >= 0 && nx < width && ny >=0 && ny < length)
                        {
                            if (rawField[nx, ny] == '*')
                            {
                                bombNum++;
                            }
                        }
                    }
                    solvedField[i, j] = bombNum;
                }
            }
            
            // 输出第一个数组:原始雷区
            Console.WriteLine("\n=== 原始雷区 ===");
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < length; j++)
                {
                    Console.Write(rawField[i, j] + " ");
                }
                Console.WriteLine();
            }
            
            // 输出第二个数组:解谜结果
            Console.WriteLine("\n=== 解谜结果 ===");
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < length; j++)
                {
                    if (solvedField[i, j] == -1)
                    {
                        Console.Write("* ");
                    }
                    else
                    {
                        Console.Write(solvedField[i, j] + " ");
                    }
                }
                Console.WriteLine();
            }
        }
    }
}

实现说明

  • 地雷数量匹配:先初始化所有格子为无雷状态,再循环随机生成坐标放置地雷,每次放置前校验是否重复,直到实际放置数量和用户输入值完全一致,同时新增非法输入校验避免逻辑异常。
  • 解谜结果计算:通过预定义的8个方向偏移量,遍历每个非雷格子的相邻坐标,统计合法范围内的地雷总数,结果存入数组。
  • 双数组输出:先后输出仅含_和*的原始雷区、带相邻地雷数的解谜结果,完全符合需求。

内容的提问来源于stack exchange,提问作者sfsiee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 02:15:04