[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);
}
}
}
}
}
}