public static void Main() { const int MaxFreeFields = 35; string command = string.Empty; char[,] gameBoard = CreateGameBoard(); char[,] mine = PutMines(); int moveCount = 0; bool steppedOnMine = false; List<Scores> topPlayers = new List<Scores>(6); int currentRow = 0; int currentCol = 0; bool isStartOfGame = true; bool isEndOfGame = false; do { if (isStartOfGame) { Console.WriteLine("Let`s play “Minesweeper”. Try to find fields without mines." + " Command 'top' show the scores, 'restart' starts a new game, 'exit' exit the game!"); DrawGameBoard(gameBoard); isStartOfGame = false; } Console.Write("Enter row and column : "); command = Console.ReadLine().Trim(); if (command.Length >= 3) { if (int.TryParse(command[0].ToString(), out currentRow) && int.TryParse(command[2].ToString(), out currentCol) && currentRow <= gameBoard.GetLength(0) && currentCol <= gameBoard.GetLength(1)) { command = "turn"; } } switch (command) { case "top": PrintResults(topPlayers); break; case "restart": gameBoard = CreateGameBoard(); mine = PutMines(); DrawGameBoard(gameBoard); steppedOnMine = false; isStartOfGame = false; break; case "exit": Console.WriteLine("Good bye!"); break; case "turn": if (mine[currentRow, currentCol] != '*') { if (mine[currentRow, currentCol] == '-') { SetFieldValue(gameBoard, mine, currentRow, currentCol); moveCount++; } if (MaxFreeFields == moveCount) { isEndOfGame = true; } else { DrawGameBoard(gameBoard); } } else { steppedOnMine = true; } break; default: Console.WriteLine("\nInavlid command!\n"); break; } if (steppedOnMine) { DrawGameBoard(mine); Console.Write("\nThe End! You finished with {0} points. " + "Enter your name: ", moveCount); string playerName = Console.ReadLine(); Scores finalResult = new Scores(playerName, moveCount); if (topPlayers.Count < 5) { topPlayers.Add(finalResult); } else { for (int i = 0; i < topPlayers.Count; i++) { if (topPlayers[i].PlayerPoints < finalResult.PlayerPoints) { topPlayers.Insert(i, finalResult); topPlayers.RemoveAt(topPlayers.Count - 1); break; } } } topPlayers.Sort((Scores firstResult, Scores secondResult) => secondResult.PlayerName.CompareTo(firstResult.PlayerName)); topPlayers.Sort((Scores firstResult, Scores secondResult) => secondResult.PlayerPoints.CompareTo(firstResult.PlayerPoints)); PrintResults(topPlayers); gameBoard = CreateGameBoard(); mine = PutMines(); moveCount = 0; steppedOnMine = false; isStartOfGame = true; } if (isEndOfGame) { Console.WriteLine("\nCongratulations! Well done genius!!!."); DrawGameBoard(mine); Console.WriteLine("Enter your name: "); string playerName = Console.ReadLine(); Scores finalScores = new Scores(playerName, moveCount); topPlayers.Add(finalScores); PrintResults(topPlayers); gameBoard = CreateGameBoard(); mine = PutMines(); moveCount = 0; isEndOfGame = false; isStartOfGame = true; } } while (command != "exit"); Console.WriteLine("All rights reserved!"); Console.WriteLine("Press any key to exit!"); Console.Read(); }
private void ProcessCommand(string[] command) { ConsoleVisualizer visualizer = new ConsoleVisualizer(field); if (command.Length == 1) { string gameCommand = command[0]; switch (gameCommand) { case "restart": Play(); break; case "top": visualizer.VisualizeScore(this.Scores); break; case "exit": this.Exit(); break; default: visualizer.VisualizeField(); break; } } else if (command.Length == 2) { int row = 0; int col = 0; bool tryParse = false; tryParse = (Int32.TryParse(command[0], out row) || tryParse); tryParse = (Int32.TryParse(command[1], out col) || tryParse); if (!tryParse) { throw new CommandUnknownException(); } if (field.RevealCell(row, col) == '*') { field.MarkAndRevealEmptyFields('-'); field.RevealMines(); Console.WriteLine(field.ToString()); Console.WriteLine(String.Format("Booooom! You were killed by a mine. You revealed {0} cells without mines.", CurrentScore)); Console.Write("Please enter your name for the top scoreboard: "); string playerName = Console.ReadLine(); Scores.Add(new Score(playerName, this.CurrentScore)); Console.WriteLine(); PrintScoreBoard(); //Play(); } else { visualizer.VisualizeField(); //Console.WriteLine(field.ToString()); this.CurrentScore++; //ProcessCommand(command); } //TODO: Implement the missed logic } else { throw new CommandUnknownException("No exist such command!"); } }
public static void Main() { const int MaxFreeFields = 35; string command = string.Empty; char[,] gameBoard = CreateGameBoard(); char[,] mine = PutMines(); int moveCount = 0; bool steppedOnMine = false; List <Scores> topPlayers = new List <Scores>(6); int currentRow = 0; int currentCol = 0; bool isStartOfGame = true; bool isEndOfGame = false; do { if (isStartOfGame) { Console.WriteLine("Let`s play “Minesweeper”. Try to find fields without mines." + " Command 'top' show the scores, 'restart' starts a new game, 'exit' exit the game!"); DrawGameBoard(gameBoard); isStartOfGame = false; } Console.Write("Enter row and column : "); command = Console.ReadLine().Trim(); if (command.Length >= 3) { if (int.TryParse(command[0].ToString(), out currentRow) && int.TryParse(command[2].ToString(), out currentCol) && currentRow <= gameBoard.GetLength(0) && currentCol <= gameBoard.GetLength(1)) { command = "turn"; } } switch (command) { case "top": PrintResults(topPlayers); break; case "restart": gameBoard = CreateGameBoard(); mine = PutMines(); DrawGameBoard(gameBoard); steppedOnMine = false; isStartOfGame = false; break; case "exit": Console.WriteLine("Good bye!"); break; case "turn": if (mine[currentRow, currentCol] != '*') { if (mine[currentRow, currentCol] == '-') { SetFieldValue(gameBoard, mine, currentRow, currentCol); moveCount++; } if (MaxFreeFields == moveCount) { isEndOfGame = true; } else { DrawGameBoard(gameBoard); } } else { steppedOnMine = true; } break; default: Console.WriteLine("\nInavlid command!\n"); break; } if (steppedOnMine) { DrawGameBoard(mine); Console.Write("\nThe End! You finished with {0} points. " + "Enter your name: ", moveCount); string playerName = Console.ReadLine(); Scores finalResult = new Scores(playerName, moveCount); if (topPlayers.Count < 5) { topPlayers.Add(finalResult); } else { for (int i = 0; i < topPlayers.Count; i++) { if (topPlayers[i].PlayerPoints < finalResult.PlayerPoints) { topPlayers.Insert(i, finalResult); topPlayers.RemoveAt(topPlayers.Count - 1); break; } } } topPlayers.Sort((Scores firstResult, Scores secondResult) => secondResult.PlayerName.CompareTo(firstResult.PlayerName)); topPlayers.Sort((Scores firstResult, Scores secondResult) => secondResult.PlayerPoints.CompareTo(firstResult.PlayerPoints)); PrintResults(topPlayers); gameBoard = CreateGameBoard(); mine = PutMines(); moveCount = 0; steppedOnMine = false; isStartOfGame = true; } if (isEndOfGame) { Console.WriteLine("\nCongratulations! Well done genius!!!."); DrawGameBoard(mine); Console.WriteLine("Enter your name: "); string playerName = Console.ReadLine(); Scores finalScores = new Scores(playerName, moveCount); topPlayers.Add(finalScores); PrintResults(topPlayers); gameBoard = CreateGameBoard(); mine = PutMines(); moveCount = 0; isEndOfGame = false; isStartOfGame = true; } }while (command != "exit"); Console.WriteLine("All rights reserved!"); Console.WriteLine("Press any key to exit!"); Console.Read(); }