提问人:Philippe Birkner 提问时间:11/13/2023 更新时间:11/14/2023 访问量:49
扫雷作为控制台应用程序
Minesweeper as a Console Application
问:
此代码应该在控制台中重新创建扫雷,但最后一个方法未正确调用。它应该显示场上所有周围的零。错误在哪里,以便发现场上的所有零?
class Minesweeper
{
static void Main()
{
Console.WriteLine("Willkommen beim Mienenwischen!");
Console.Write("Auf welcher Spielfeldgröße möchtet du starten (maximal 20x20) ein: ");
int size = int.Parse(Console.ReadLine());
if (size < 1 || size > 20)
{
Console.WriteLine("Ungültige Spielfeldgröße. Das Spielfeld muss zwischen 1x1 und 20x20 liegen.");
return;
}
Console.Write("Wie viele Mienen sollen auf dem Feld versteckt sein: ");
int mines = int.Parse(Console.ReadLine());
if (mines < 1 || mines > size * size)
{
Console.WriteLine("Schaffst du auch Irgendwas? Zähl nach! Es geht nicht");
return;
}
char[,] board = InitializeBoard(size);
char[,] minePositions = PlaceMines(size, mines);
bool[,] revealed = new bool[size, size];
bool gameOver = false;
while (!gameOver)
{
DisplayBoard(board, revealed);
Console.Write("Gib die Koordianten ein um mögliche Mienen aufzucken (Beispiel: A03): ");
string input = Console.ReadLine();
if (input.Length != 3 || !char.IsLetter(input[0]) || !char.IsDigit(input[1]) || !char.IsDigit(input[2]))
{
Console.WriteLine("Ungültige Eingabe. Verwende das Format wie Angegeben 'A03'.");
continue;
}
int column = input[0] - 'A';
int row = int.Parse(input.Substring(1, 2)) - 1;
if (column < 0 || column >= size || row < 0 || row >= size || revealed[row, column])
{
Console.WriteLine("Du bist außerhalb des Spielfelds. Probier es nochmal");
continue;
}
if (minePositions[row, column] == '*')
{
Console.WriteLine("Tja da war eine Miene, trainiere das Köpfchen und versuche es nochmal.");
gameOver = true;
}
else
{
int adjacentMines = CountAdjacentMines(minePositions, row, column);
board[row, column] = (char)(adjacentMines + '0');
if (adjacentMines == 0)
{
RevealAdjacentZeros(board, revealed, row, column);
}
if (adjacentMines > 0)
{
revealed[row, column] = true;
}
if (CheckWinCondition(board, revealed, mines))
{
Console.WriteLine("Glückwunsch! Du hast Gewonnen!");
gameOver = true;
}
}
}
}
static char[,] InitializeBoard(int size)
{
char[,] board = new char[size, size];
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
board[i, j] = '_';
}
}
return board;
}
static char[,] PlaceMines(int size, int mines)
{
char[,] minePositions = new char[size, size];
Random rand = new Random();
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
minePositions[i, j] = ' ';
}
}
while (mines > 0)
{
int row = rand.Next(size);
int col = rand.Next(size);
if (minePositions[row, col] == ' ')
{
minePositions[row, col] = '*';
mines--;
}
}
return minePositions;
}
static void DisplayBoard(char[,] board, bool[,] revealed)
{
int size = board.GetLength(0);
Console.WriteLine("\t" + string.Join(" ", "ABCDEFGHIJKLMNOPQRSTUVWXYZ".Substring(0, size)));
for (int i = 0; i < size; i++)
{
Console.Write((i + 1) + "\t");
for (int j = 0; j < size; j++)
{
if (revealed[i, j])
{
Console.Write(board[i, j] + "");
}
else
{
Console.Write("_");
}
}
Console.WriteLine();
}
}
static int CountAdjacentMines(char[,] minePositions, int row, int col)
{
int count = 0;
int size = minePositions.GetLength(0);
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
int newRow = row + i;
int newCol = col + j;
if (newRow >= 0 && newRow < size && newCol >= 0 && newCol < size && minePositions[newRow, newCol] == '*')
{
count++;
}
}
}
return count;
}
static bool CheckWinCondition(char[,] board, bool[,] revealed, int mines)
{
int size = board.GetLength(0);
int unrevealedCount = size * size - mines;
int revealedCount = 0;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (revealed[i, j])
{
revealedCount++;
}
}
}
return unrevealedCount == revealedCount;
}
static void RevealAdjacentZeros(char[,] board, bool[,] revealed, int row, int col)
{
int size = board.GetLength(0);
if (row < 0 || row >= size || col < 0 || col >= size || revealed[row, col])
{
return;
}
revealed[row, col] = true;
if (board[row, col] == '_')
{
RevealAdjacentZeros(board, revealed, row - 1, col);
RevealAdjacentZeros(board, revealed, row + 1, col);
RevealAdjacentZeros(board, revealed, row, col - 1);
RevealAdjacentZeros(board, revealed, row, col + 1);
RevealAdjacentZeros(board, revealed, row - 1, col - 1);
RevealAdjacentZeros(board, revealed, row - 1, col + 1);
RevealAdjacentZeros(board, revealed, row + 1, col - 1);
RevealAdjacentZeros(board, revealed, row + 1, col + 1);
}
}
}
我是编程新手,因此没有太多经验。但是我已经尝试使用来检查是否正在调用此方法,并且确实如此。这就是为什么我不知道错误可能在哪里。Console.WriteLine
答: 暂无答案
评论