private static void Main() { Console.Title = "Consoled Minesweeper 3000"; List<Score> scores = new List<Score>(6); string command = string.Empty; char[,] board = CreateGameField(); char[,] bombs = SetRandomBombs(); int roundCounter = 0; bool isBombed = false; int row = 0; int col = 0; bool isNewGame = true; bool isGameBeat = false; do { if (isNewGame) { Console.WriteLine("Let's play Minesweeper. Try your luck to find all hidden mines without detonating any of them."); Console.WriteLine("Command 'top' shows the scores"); Console.WriteLine("Command 'restart' starts a new game"); Console.WriteLine("Command 'exit' exits from the game"); PrintField(board); isNewGame = false; } Console.Write("Enter row and column : "); command = Console.ReadLine().Trim(); if (command.Length >= 3) { if (int.TryParse(command[0].ToString(), out row) && int.TryParse(command[2].ToString(), out col) && row <= board.GetLength(0) && col <= board.GetLength(1)) { command = "turn"; } } switch (command) { case "top": ExecuteTopCommand(scores); break; case "restart": board = CreateGameField(); bombs = SetRandomBombs(); PrintField(board); isBombed = false; isNewGame = false; break; case "exit": Console.WriteLine("Bye, Bye, Bye!"); break; case "turn": if (bombs[row, col] != '*') { if (bombs[row, col] == '-') { ExecuteRound(board, bombs, row, col); roundCounter++; } if (MaxRounds == roundCounter) { isGameBeat = true; } else { PrintField(board); } } else { isBombed = true; } break; default: Console.WriteLine("\nUnknown command.\n"); break; } if (isBombed) { PrintField(bombs); Console.WriteLine($"\nHrrrrrr! You died with {roundCounter} points."); Console.Write("Ener your nickname: "); string nickname = Console.ReadLine(); Score score = new Score(nickname, roundCounter); if (scores.Count < 5) { scores.Add(score); } else { for (int i = 0; i < scores.Count; i++) { if (scores[i].Points < score.Points) { scores.Insert(i, score); scores.RemoveAt(scores.Count - 1); break; } } } scores.Sort((r1, r2) => r2.Name.CompareTo(r1.Name)); scores.Sort((r1, r2) => r2.Points.CompareTo(r1.Points)); ExecuteTopCommand(scores); board = CreateGameField(); bombs = SetRandomBombs(); roundCounter = 0; isBombed = false; isNewGame = true; } if (isGameBeat) { Console.WriteLine("\nBRAVOOOS! You won the game!!! CONGRATULATIONS !"); PrintField(bombs); Console.WriteLine("Dude i must know your name: "); string nickname = Console.ReadLine(); Score score = new Score(nickname, roundCounter); scores.Add(score); ExecuteTopCommand(scores); board = CreateGameField(); bombs = SetRandomBombs(); roundCounter = 0; isGameBeat = false; isNewGame = true; } } while (command != "exit"); Console.WriteLine("Made in Bulgaria!"); Console.WriteLine("Have a nice day..."); Console.Read(); }
private static void Main() { const int MaxCommandInputLen = 3; const int MaxCells = 35; string command; char[,] gameField = SeedGame.CreateGameField(); char[,] board = SeedGame.SetBoard(); var champions = new List<Score>(6); var score = 0; var row = 0; var col = 0; var gameIsRunning = true; var gameWon = false; var gameOver = false; do { if (gameIsRunning) { Console.WriteLine( "Lets play Minesweeper. Test you luck in discovering cells without bombs." + " Command 'top' shows the scoreboard, 'restart' resets the game, 'exit' quits the game!"); Draw.DrawInnerField(gameField); gameIsRunning = false; } Console.Write("Row and Col please : "); command = Console.ReadLine().Trim(); if (command.Length >= MaxCommandInputLen) { if (int.TryParse(command[0].ToString(), out row) && int.TryParse(command[2].ToString(), out col) && row <= gameField.GetLength(0) && col <= gameField.GetLength(1)) { command = "turn"; } } switch (command) { case "top": Draw.Scoreboard(champions); break; case "restart": gameField = SeedGame.CreateGameField(); board = SeedGame.SetBoard(); Draw.DrawInnerField(gameField); break; case "exit": Console.WriteLine("Bye!"); break; case "turn": if (board[row, col] != '*') { if (board[row, col] == '-') { Engine.NextTurn(gameField, board, row, col); score++; } if (MaxCells == score) { gameWon = true; } else { Draw.DrawInnerField(gameField); } } else { gameOver = true; } break; default: Console.WriteLine("\nInvalid command!\n"); break; } if (gameOver) { Draw.DrawInnerField(board); Console.Write("\nYou died with {0} score. " + "What is your name? ", score); string name = Console.ReadLine(); Score t = new Score(name, score); if (champions.Count < 5) { champions.Add(t); } else { for (int i = 0; i < champions.Count; i++) { if (champions[i].CurrentScore < t.CurrentScore) { champions.Insert(i, t); champions.RemoveAt(champions.Count - 1); break; } } } champions.Sort((r1, r2) => r2.PlayerName.CompareTo(r1.PlayerName)); champions.Sort((r1, r2) => r2.CurrentScore.CompareTo(r1.CurrentScore)); Draw.Scoreboard(champions); gameField = SeedGame.CreateGameField(); board = SeedGame.SetBoard(); score = 0; gameOver = false; gameIsRunning = true; } if (gameWon) { Console.WriteLine("\nCongratulations! You opened all 35 cells without dying!."); Draw.DrawInnerField(board); Console.WriteLine("What is your name? "); string name = Console.ReadLine(); var bestScore = new Score(name, score); champions.Add(bestScore); Draw.Scoreboard(champions); gameField = SeedGame.CreateGameField(); board = SeedGame.SetBoard(); score = 0; gameWon = false; gameIsRunning = true; } } while (command != "exit"); Console.WriteLine("Made in Bulgaria!"); Console.Read(); }