public static void ShowHighScores(CliController controller) { while (true) { Console.Clear(); Console.WriteLine(" High Scores"); Console.WriteLine(); Console.WriteLine(); controller.HighScores = FileOperations.LoadScores(new DifficultLevel { Width = 9, Height = 9, MinesCount = 10, Name = "Easy" }); var scores = controller.HighScores; var visibleScoresCount = Math.Min(10, scores.Count); for (int i = 0; i < visibleScoresCount; i++) { Console.WriteLine($" {scores[i].Name} - " + $"{scores[i].Time.Minutes:00}:{scores[i].Time.Seconds:00}:{scores[i].Time.Milliseconds / 10:00} - " + $"{scores[i].DifficultLevel}"); } Console.WriteLine(); Console.WriteLine("Press 'C' to clear scores or press any key to exit to main menu..."); if (Console.ReadKey(true).Key == ConsoleKey.C) { controller.ClearScores(); } else { return; } } }
static void Main() { Console.BackgroundColor = ConsoleColor.Gray; Console.ForegroundColor = ConsoleColor.Black; Console.SetWindowSize(120, 30); Console.OutputEncoding = Encoding.UTF8; Console.CursorVisible = false; var controller = new CliController(); while (true) { switch (Menu.ShowMainMenu(controller)) { case MainMenuItems.NewGame: StartNewGame(); break; case MainMenuItems.HighScores: Menu.ShowHighScores(controller); break; case MainMenuItems.Settings: Menu.ShowSettingsMenu(controller); break; case MainMenuItems.About: Menu.ShowAbout(); break; case MainMenuItems.Exit: return; } } }
public static void StartNewGame() { var controller = new CliController(); Console.CursorVisible = true; ShowGameField(controller); while (controller.GameStatus == GameStatus.Playing || controller.GameStatus == GameStatus.Wait) { var input = Console.ReadLine()?.ToUpper(); if (input == "EXIT") { Console.CursorVisible = false; return; } if (!controller.TryExecuteCommand(input)) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Wrong input!"); Console.ForegroundColor = ConsoleColor.Black; Console.ReadLine(); } ShowGameField(controller); } Console.CursorVisible = false; if (controller.GameStatus == GameStatus.GameOver) { Console.ReadKey(true); } }
public static MainMenuItems ShowMainMenu(CliController controller) { Console.Clear(); Console.Title = "Minesweeper"; Console.WriteLine(" Minesweeper"); Console.WriteLine(); Console.WriteLine(); while (true) { Console.SetCursorPosition(0, 3); Console.WriteLine($" {(controller.SelectedMainMenuItem == MainMenuItems.NewGame ? "☼" : " ")} New Game"); Console.WriteLine($" {(controller.SelectedMainMenuItem == MainMenuItems.HighScores ? "☼" : " ")} High Scores"); Console.WriteLine($" {(controller.SelectedMainMenuItem == MainMenuItems.Settings ? "☼" : " ")} Settings"); Console.WriteLine($" {(controller.SelectedMainMenuItem == MainMenuItems.About ? "☼" : " ")} About"); Console.WriteLine($" {(controller.SelectedMainMenuItem == MainMenuItems.Exit ? "☼" : " ")} Exit"); var key = Console.ReadKey(true).Key; switch (key) { case ConsoleKey.UpArrow: if (controller.SelectedMainMenuItem == 0) { controller.SelectedMainMenuItem = Enum.GetValues(typeof(MainMenuItems)).Cast <MainMenuItems>().LastOrDefault(); } else { controller.SelectedMainMenuItem--; } break; case ConsoleKey.DownArrow: if (controller.SelectedMainMenuItem == Enum.GetValues(typeof(MainMenuItems)).Cast <MainMenuItems>().LastOrDefault()) { controller.SelectedMainMenuItem = 0; } else { controller.SelectedMainMenuItem++; } break; case ConsoleKey.Enter: return(controller.SelectedMainMenuItem); } } }
public static void ShowGameField(CliController controller) { var width = controller.FieldWidth; var extraChars = new[] { '@', '#', '$', '%' }; Console.Clear(); Console.Write(" "); for (int i = 0; i < width; i++) { Console.Write(i < 26 ? $"{Convert.ToChar(i + 65)} " : $"{extraChars[i - 26]} "); } Console.WriteLine(); Console.Write(" "); for (int i = 0; i < width; i++) { Console.Write("| "); } Console.WriteLine(); foreach (var row in controller.Field.Cells) { Console.Write($" {controller.Field.Cells.IndexOf(row) + 1,2} - "); foreach (var cell in row) { Console.ForegroundColor = CliController.CellColors[cell.Status]; Console.Write($"{cell} "); } Console.ForegroundColor = ConsoleColor.Black; Console.Write($"- {controller.Field.Cells.IndexOf(row) + 1,2}"); Console.WriteLine(); } Console.WriteLine(); if (controller.GameStatus != GameStatus.GameOver) { Console.WriteLine(" To open a cell, enter its coordinates ('B7' for example)"); Console.WriteLine(" To mark a cell, enter its coordinates with '?' prefix ('?B7')"); Console.WriteLine(" To open around unmarked cells, use a '!' prefix ('!B7')"); Console.WriteLine(" To exit enter 'Exit'"); } Console.WriteLine(); }
public static void ShowSettingsMenu(CliController controller) { while (true) { Console.Clear(); Console.CursorVisible = false; Console.WriteLine(" Settings"); Console.WriteLine(); Console.WriteLine(); var currentDifficultLevel = controller.DifficultLevel.ToString(); Console.ForegroundColor = currentDifficultLevel == "Easy" ? ConsoleColor.Red : ConsoleColor.Black; Console.WriteLine($" {(controller.SelectedSettingsMenuItem == SettingsMenuItems.Easy ? "☼" : " ")} Easy"); Console.ForegroundColor = currentDifficultLevel == "Medium" ? ConsoleColor.Red : ConsoleColor.Black; Console.WriteLine( $" {(controller.SelectedSettingsMenuItem == SettingsMenuItems.Medium ? "☼" : " ")} Medium"); Console.ForegroundColor = currentDifficultLevel == "Hard" ? ConsoleColor.Red : ConsoleColor.Black; Console.WriteLine( $" {(controller.SelectedSettingsMenuItem == SettingsMenuItems.Hard ? "☼" : " ")} Hard"); Console.ForegroundColor = currentDifficultLevel == "Custom Level" ? ConsoleColor.Red : ConsoleColor.Black; Console.WriteLine( $" {(controller.SelectedSettingsMenuItem == SettingsMenuItems.Custom ? "☼" : " ")} Custom ({controller.DifficultLevel.Width}x{controller.DifficultLevel.Height}x{controller.DifficultLevel.MinesCount})"); Console.ForegroundColor = ConsoleColor.Black; Console.WriteLine($" {(controller.SelectedSettingsMenuItem == SettingsMenuItems.Exit ? "☼" : " ")} Exit to main menu"); var key = Console.ReadKey(true).Key; switch (key) { case ConsoleKey.UpArrow: if (controller.SelectedSettingsMenuItem == 0) { controller.SelectedSettingsMenuItem = Enum.GetValues(typeof(SettingsMenuItems)).Cast <SettingsMenuItems>().LastOrDefault(); } else { controller.SelectedSettingsMenuItem--; } break; case ConsoleKey.DownArrow: if (controller.SelectedSettingsMenuItem == Enum.GetValues(typeof(SettingsMenuItems)).Cast <SettingsMenuItems>().LastOrDefault()) { controller.SelectedSettingsMenuItem = 0; } else { controller.SelectedSettingsMenuItem++; } break; case ConsoleKey.Enter: switch (controller.SelectedSettingsMenuItem) { case SettingsMenuItems.Easy: controller.SetDifficultLevel("0"); break; case SettingsMenuItems.Medium: controller.SetDifficultLevel("1"); break; case SettingsMenuItems.Hard: controller.SetDifficultLevel("2"); break; case SettingsMenuItems.Custom: controller.SetDifficultLevel("3"); Console.CursorVisible = true; Console.WriteLine("Game field Width:"); if (!int.TryParse(Console.ReadLine(), out var inputWidth)) { ShowErrorMessage("Wrong input!"); break; } if (inputWidth < 2 || inputWidth > 30) { ShowErrorMessage("Width of game field must be in range 2 to 30"); break; } Console.WriteLine("Game field Height:"); if (!int.TryParse(Console.ReadLine(), out var inputHeight)) { ShowErrorMessage("Wrong input!"); break; } if (inputHeight < 2 || inputHeight > 16) { ShowErrorMessage("Height of game field must be in range 2 to 16"); break; } Console.WriteLine("Mines count:"); if (!int.TryParse(Console.ReadLine(), out var inputMinesCount)) { ShowErrorMessage("Wrong input!"); break; } if (inputMinesCount < 1 || inputMinesCount > inputHeight * inputHeight - 1) { ShowErrorMessage($"Mines count must be in range 1 to {inputHeight * inputHeight - 1}"); break; } controller.DifficultLevel.Width = inputWidth; controller.DifficultLevel.Height = inputHeight; controller.DifficultLevel.MinesCount = inputMinesCount; break; case SettingsMenuItems.Exit: FileOperations.SaveSettings(controller.DifficultLevel); return; } break; } } }