一个小时用c#写的命令行扫雷..
哈哈刚才发帖前见到有用python写的,看来扫雷果真是练手的游戏..
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;
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 = '-';
}
}
}
static char[,] GenerateBoard(int width, int height, int mineCount)
{
char[,] board = new char;
Random rand = new Random();
// 初始化板块
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
board = '-';
}
}
// 放置地雷
for (int i = 0; i < mineCount; i++)
{
int x, y;
do
{
x = rand.Next(width);
y = rand.Next(height);
} while (board == '*');
board = '*';
}
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, out int row) && int.TryParse(parts, out int col)
&& row >= 1 && col >= 1 && row <= width && col <= height)
{
row--; col--;
if (parts.ToLower() == "p")
{
// 标记或取消标记
if (displayBoard == '-')
{
displayBoard = 'P';
markedMines++;
}
else if (displayBoard == 'P')
{
displayBoard = '-';
markedMines--;
}
}
else if (displayBoard == '-' || displayBoard == 'P')
{
if (board == '*')
{
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 == '*')
{
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 != '*' && displayBoard == '-')
{
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} ");
}
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 != '-')
{
return; // 越界或已经揭露的格子,直接返回
}
int minesAround = CountMinesAround(board, row, col, width, height);
displayBoard = 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);
}
}
}
}
}
}
剩余雷数: 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) 本帖最后由 苏紫方璇 于 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;
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 == '*')
{
Console.WriteLine("Game Over! You hit a bomb!");
gameOver = true;
}
else
{
revealed = true;
if (IsBoardFullyRevealed(board, revealed))
{
Console.WriteLine("Congratulations! You won!");
gameOver = true;
}
}
}
}
static char[,] CreateBoard(int rows, int columns)
{
char[,] board = new char;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
board = '-';
}
}
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)
{
Console.Write(board);
}
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 != '*')
{
board = '*';
}
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 != '*')
{
int bombCount = CountNeighboringBombs(board, i, j);
board = bombCount.ToString();
}
}
}
}
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 == '*')
{
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 != '*' && !revealed)
{
return false;
}
}
}
return true;
}
}
```
这是我花了1分钟辛苦写的扫雷游戏 扫雷应该是比较简单的游戏吧,请问代码里是不是不需要特别的数据结构? 感谢分享 不错不错! 不错 谢谢分享 谢谢分享,可以拿来做课件 kolt1911 发表于 2023-12-14 20:39
扫雷应该是比较简单的游戏吧,请问代码里是不是不需要特别的数据结构?
逻辑写好了应该就行
感觉能用这个学语言...
寓教于乐 最近也在学C# 感谢楼主分享,照着写一写练练手 学习下。。。。。。。。。。。。。