static void Main(string[] args) { GameBoard gb = new GameBoard(); gb.GenerateNewGame(); gb.PrintGameBoard(); TopScore ts = new TopScore(); ts.OpenTopScoreList(); bool isCoordinates; Coordinates coordinates = new Coordinates(); Command command = new Command(); while (gb.RemainingBaloons > 0) { if (gb.ReadInput(out isCoordinates, ref coordinates, ref command)) { if (isCoordinates) { gb.Shoot(coordinates); gb.PrintGameBoard(); } else { switch (command.Value) { case "top": { ts.PrintScoreList(); } break; case "restart": { gb.GenerateNewGame(); gb.PrintGameBoard(); } break; case "exit": { return; } } } } else { Console.WriteLine("Wrong Input!"); } } Person player = new Person(); player.Score = gb.ShootCounter; if (ts.IsTopScore(player)) { Console.WriteLine("Please enter your name for the top scoreboard: "); player.Name = Console.ReadLine(); ts.AddToTopScoreList(player); } ts.SaveTopScoreList(); }
public void AddToTopScoreList(Person person) { topScoreList.Add(person); PersonScoreComparer comparer = new PersonScoreComparer(); topScoreList.Sort(comparer); while (topScoreList.Count > 5) { topScoreList.RemoveAt(5); } }
public bool IsTopScore(Person person) { if (topScoreList.Count >= MAX_TOP_SCORE_COUNT) { PersonScoreComparer comparer = new PersonScoreComparer(); topScoreList.Sort(comparer); if (topScoreList[MAX_TOP_SCORE_COUNT - 1] > person) { return true; } else { return false; } } return true; }
public void OpenTopScoreList() { using (StreamReader TopScoreStreamReader = new StreamReader("..\\..\\TopScore.txt")) { string line = TopScoreStreamReader.ReadLine(); while (line != null) { char[] separators = { ' ' }; string[] substrings = line.Split(separators); int substringsCount = substrings.Count<string>(); if (substringsCount > 0) { Person player = new Person(); player.Name = substrings[1]; player.Score = int.Parse(substrings[substringsCount - 2]); topScoreList.Add(player); } line = TopScoreStreamReader.ReadLine(); } } }