吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1253|回复: 21
收起左侧

[其他原创] 一个小时用c#写的命令行扫雷..

[复制链接]
onlyclxy 发表于 2023-12-14 20:03
哈哈刚才发帖前见到有用python写的,
看来扫雷果真是练手的游戏..
[C#] 纯文本查看 复制代码
namespace 扫雷
{

    using System;

    class Minesweeper
    {
        static void Main(string[] args)
        {
            while (true) { 
            
            const int width = 10;
            const int height = 10;
            const int mineCount = 10;

            char[,] board = GenerateBoard(width, height, mineCount);
            char[,] displayBoard = new char[width, height];
            InitializeDisplayBoard(displayBoard, width, height);

            PlayGame(board, displayBoard, width, height, mineCount);

            }
        }

        static void InitializeDisplayBoard(char[,] displayBoard, int width, int height)
        {
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    displayBoard[i, j] = '-';
                }
            }
        }

        static char[,] GenerateBoard(int width, int height, int mineCount)
        {
            char[,] board = new char[width, height];
            Random rand = new Random();

            // 初始化板块
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    board[i, j] = '-';
                }
            }

            // 放置地雷
            for (int i = 0; i < mineCount; i++)
            {
                int x, y;
                do
                {
                    x = rand.Next(width);
                    y = rand.Next(height);
                } while (board[x, y] == '*');

                board[x, y] = '*';
            }

            return board;
        }

        static void PlayGame(char[,] board, char[,] displayBoard, int width, int height, int mineCount)
        {
            int markedMines = 0;
            bool gameOver = false;
            while (!gameOver)
            {
                Console.Clear();
                Console.WriteLine($"剩余雷数: {mineCount - markedMines}\n");
                DisplayBoard(displayBoard, width, height);
                gameOver = ProcessPlayerMove(board, displayBoard, width, height, ref markedMines);
            }



            if (IsWin(board, displayBoard, width, height))
            {
                Console.WriteLine("你赢了!恭喜大吉大利,今晚吃鸡!游戏胜利!");
        
            }
            else
            {

                Console.WriteLine("游戏结束!");
            
            }
            Console.WriteLine("\n最终雷区布局:");
            DisplayBoard(board, width, height);
            Console.ReadLine();
        }




        static bool ProcessPlayerMove(char[,] board, char[,] displayBoard, int width, int height, ref int markedMines)
        {
            Console.WriteLine("请输入操作的坐标(格式:行 列,从1开始计数),或输入 'p 行 列' 来标记/取消标记雷:");
            string input = Console.ReadLine();
            string[] parts = input.Split(' ');

            if (parts.Length >= 2 && int.TryParse(parts[parts.Length - 2], out int row) && int.TryParse(parts[parts.Length - 1], out int col)
                && row >= 1 && col >= 1 && row <= width && col <= height)
            {
                row--; col--;

                if (parts[0].ToLower() == "p")
                {
                    // 标记或取消标记
                    if (displayBoard[row, col] == '-')
                    {
                        displayBoard[row, col] = 'P';
                        markedMines++;
                    }
                    else if (displayBoard[row, col] == 'P')
                    {
                        displayBoard[row, col] = '-';
                        markedMines--;
                    }
                }
                
                else if (displayBoard[row, col] == '-' || displayBoard[row, col] == 'P')
                {
                    if (board[row, col] == '*')
                    {
                        Console.WriteLine("你踩到雷了!");             
                        return true; // 游戏结束
                    }

                    RevealCell(board, displayBoard, row, col, width, height);
                }

                return IsWin(board, displayBoard, width, height);
            }

            Console.WriteLine("无效输入,请重新输入!");
            return false;
        }



        static int CountMinesAround(char[,] board, int row, int col, int width, int height)
        {
            int count = 0;
            for (int i = row - 1; i <= row + 1; i++)
            {
                for (int j = col - 1; j <= col + 1; j++)
                {
                    if (i >= 0 && i < width && j >= 0 && j < height && board[i, j] == '*')
                    {
                        count++;
                    }
                }
            }
            return count;
        }

        static bool IsWin(char[,] board, char[,] displayBoard, int width, int height)
        {
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    if (board[i, j] != '*' && displayBoard[i, j] == '-')
                    {
                        return false; // 存在未被揭露的非雷区域
                    }
                }
            }
            return true; // 所有非雷区域已揭露
        }




        static void DisplayBoard(char[,] displayBoard, int width, int height)
        {
            // 打印列号
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write("   "); // 留空位对齐行号
            for (int j = 0; j < height; j++)
            {
                Console.Write($"{j + 1} ");
            }
            Console.WriteLine();

            // 打印行号和行内容
            for (int i = 0; i < width; i++)
            {
                Console.ForegroundColor = ConsoleColor.Green; // 设置行号为绿色
                Console.Write($"{i + 1} ".PadLeft(3)); // 打印行号
                Console.ResetColor(); // 重置为默认颜色
                for (int j = 0; j < height; j++)
                {
                    Console.Write($"{displayBoard[i, j]} ");
                }
                Console.WriteLine();
            }
        }



        static void RevealCell(char[,] board, char[,] displayBoard, int row, int col, int width, int height)
        {
            if (row < 0 || col < 0 || row >= width || col >= height || displayBoard[row, col] != '-')
            {
                return; // 越界或已经揭露的格子,直接返回
            }

            int minesAround = CountMinesAround(board, row, col, width, height);
            displayBoard[row, col] = minesAround == 0 ? ' ' : char.Parse(minesAround.ToString()); // 揭露此格

            if (minesAround == 0)
            {
                // 如果周围没有雷,递归揭露邻近的格子
                for (int i = row - 1; i <= row + 1; i++)
                {
                    for (int j = col - 1; j <= col + 1; j++)
                    {
                        RevealCell(board, displayBoard, i, j, width, height);
                    }
                }
            }
        }


    }

}



[Asm] 纯文本查看 复制代码
剩余雷数: 9

   1 2 3 4 5 6 7 8 9 10
 1 - 1     2 - - - - -
 2 - 1     2 - - - - -
 3 1 1     1 1 2 - - -
 4             1 - - -
 5 1 1 1       1 1 1 -
 6 - - 1           1 -
 7 - - 1 1 1 1     1 -
 8 - - - - P 1     1 -
 9 - - - - - 2 1 1 1 -
10 - - - - - - - - - -
请输入操作的坐标(格式:行 列,从1开始计数),或输入 'p 行 列' 来标记/取消标记雷:



哈哈要是有兴趣可以试试 感觉适合摸鱼啊
扫雷.exe - 蓝奏云 (lanzoul.com)

免费评分

参与人数 1吾爱币 +7 热心值 +1 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

geniusrot 发表于 2023-12-14 21:12
本帖最后由 苏紫方璇 于 2023-12-17 12:34 编辑

using System;

class Minesweeper
{
    static void Main(string[] args)
    {
        int rows = 10;
        int columns = 10;
        int bombs = 20;

        char[,] board = CreateBoard(rows, columns);
        PlaceBombs(board, bombs);
        FillHints(board);

        bool[,] revealed = new bool[rows, columns];
        bool gameOver = false;

        while (!gameOver)
        {
            PrintBoard(board, revealed);

            Console.Write("Enter row: ");
            int row = int.Parse(Console.ReadLine());

            Console.Write("Enter column: ");
            int column = int.Parse(Console.ReadLine());

            if (board[row, column] == '*')
            {
                Console.WriteLine("Game Over! You hit a bomb!");
                gameOver = true;
            }
            else
            {
                revealed[row, column] = true;

                if (IsBoardFullyRevealed(board, revealed))
                {
                    Console.WriteLine("Congratulations! You won!");
                    gameOver = true;
                }
            }
        }
    }

    static char[,] CreateBoard(int rows, int columns)
    {
        char[,] board = new char[rows, columns];

        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                board[i, j] = '-';
            }
        }

        return board;
    }

    static void PrintBoard(char[,] board, bool[,] revealed)
    {
        int rows = board.GetLength(0);
        int columns = board.GetLength(1);

        Console.WriteLine("   " + string.Join(" ", Enumerable.Range(0, columns)));
        Console.WriteLine("  " + new string('-', columns * 2));

        for (int i = 0; i < rows; i++)
        {
            Console.Write(i + " |");

            for (int j = 0; j < columns; j++)
            {
                if (revealed[i, j])
                {
                    Console.Write(board[i, j]);
                }
                else
                {
                    Console.Write("-");
                }

                Console.Write(" ");
            }

            Console.WriteLine();
        }
    }

    static void PlaceBombs(char[,] board, int bombs)
    {
        Random random = new Random();

        int rows = board.GetLength(0);
        int columns = board.GetLength(1);

        for (int i = 0; i < bombs; i++)
        {
            int row = random.Next(rows);
            int column = random.Next(columns);

            if (board[row, column] != '*')
            {
                board[row, column] = '*';
            }
            else
            {
                i--;
            }
        }
    }

    static void FillHints(char[,] board)
    {
        int rows = board.GetLength(0);
        int columns = board.GetLength(1);

        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                if (board[i, j] != '*')
                {
                    int bombCount = CountNeighboringBombs(board, i, j);
                    board[i, j] = bombCount.ToString()[0];
                }
            }
        }
    }

    static int CountNeighboringBombs(char[,] board, int row, int column)
    {
        int count = 0;
        int rows = board.GetLength(0);
        int columns = board.GetLength(1);

        for (int i = Math.Max(row - 1, 0); i <= Math.Min(row + 1, rows - 1); i++)
        {
            for (int j = Math.Max(column - 1, 0); j <= Math.Min(column + 1, columns - 1); j++)
            {
                if (board[i, j] == '*')
                {
                    count++;
                }
            }
        }

        return count;
    }

    static bool IsBoardFullyRevealed(char[,] board, bool[,] revealed)
    {
        int rows = board.GetLength(0);
        int columns = board.GetLength(1);

        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                if (board[i, j] != '*' && !revealed[i, j])
                {
                    return false;
                }
            }
        }

        return true;
    }
}

这是我花了1分钟辛苦写的扫雷游戏

免费评分

参与人数 1吾爱币 +2 热心值 +1 收起 理由
onlyclxy + 2 + 1 为啥如此叼

查看全部评分

kolt1911 发表于 2023-12-14 20:39
扫雷应该是比较简单的游戏吧,请问代码里是不是不需要特别的数据结构?
yyzzy 发表于 2023-12-14 20:40
vivvon 发表于 2023-12-14 20:42
不错不错!
Study0588 发表于 2023-12-14 20:45
不错 谢谢分享
fuvenusck 发表于 2023-12-14 20:54
谢谢分享,可以拿来做课件
 楼主| onlyclxy 发表于 2023-12-14 21:05
kolt1911 发表于 2023-12-14 20:39
扫雷应该是比较简单的游戏吧,请问代码里是不是不需要特别的数据结构?

逻辑写好了应该就行
感觉能用这个学语言...
寓教于乐
Reo_52 发表于 2023-12-14 21:13
最近也在学C# 感谢楼主分享,照着写一写练练手
zyh701 发表于 2023-12-14 22:31
学习下。。。。。。。。。。。。。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-4 14:44

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表