private static void SaveScores(IScoreTracker result) { //File returns object StreamWriter which is compatible with TextWriter that WriteGrades expects. //Wrap in using {} so that the complier sets up a try, finally to make sure all resources are closed //for example outputFile.Close(); using (StreamWriter outputFile = File.CreateText("scores.txt")) // Added using System.IO { result.WriteScores(outputFile); } }
private static void WriteResults(IScoreTracker result) { //Assign calculated results to ScoreBoardStats class: ScoreBoardStats playerStats = result.CalculateScoreBoardStats(); //Display results: WriteResult("The Highest Score is: ", playerStats.HighestScore); WriteResult("The Lowest Score is: ", playerStats.LowestScore); WriteResult("The Average Score is: ", playerStats.AverageScore); //Display the players Skill level: WriteResult("Your Skill Level is: ", playerStats.PlayerSkill); }
private static void GetPlayerName(IScoreTracker result) { //Handling the exception if the user leaves the Name empty bool correctNameInput = false; while (correctNameInput == false) { try { Console.WriteLine("Please Enter Your Name: "); result.PlayerName = Console.ReadLine(); correctNameInput = true; } catch (ArgumentException ex) { Console.WriteLine(ex.Message); //Console.WriteLine(ex.StackTrace); //shows the stack trace as well } } }
private static void HandlePlayerChoice(IScoreTracker result) { //Handling the exception if the user adds incorrect entry: bool correctMenuInput = false; while (correctMenuInput == false) { try { Console.WriteLine("Choose an Option"); result.MenuChoice = Console.ReadLine(); correctMenuInput = true; } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } } switch (result.MenuChoice) { case "1": float noOfTurns = Maze(); AddPlayerScores(result, noOfTurns); SaveScores(result); DisplayMenu(); HandlePlayerChoice(result); break; case "2": WriteResults(result); DisplayMenu(); HandlePlayerChoice(result); break; case "3": Console.WriteLine("THANKS FOR PLAYING, GOODBYE"); Environment.Exit(0); break; } }
static void Main(string[] args) { //Added System.Speech Assembly reference to use the following: SpeechSynthesizer synth = new SpeechSynthesizer(); synth.Speak("Welcome to the Maze"); //Output text Greeting: Console.WriteLine("Welcome to the Maze!\n"); //IScoreTracker is a n interface to keep track of users scores IScoreTracker result = CreatePlayerScore(); //Using the event delegte to listen to name change and print message when it does result.NameChanged += OnNameChanged; //Ask for the Players Name: GetPlayerName(result); //Display the Game Menu: DisplayMenu(); //Handle Players Menu choice HandlePlayerChoice(result); // }
private static void AddPlayerScores(IScoreTracker result, float noOfTurns) { //Add scores to the list: result.AddScores(noOfTurns); result.AddScores(noOfTurns); }