/// <summary> /// Keeps track of the current player score and updates the player's score. /// </summary> /// <param name="currentScore"></param> /// <returns></returns> static PlayerScoreTracker CurrentPlayerScore(string userName, PlayerScoreTracker playerScore, FinchOperator myFinchOperator) { playerScore.PlayerScore = myFinchOperator.HitsSuffered * myFinchOperator.ScoreModifier; playerScore.PlayerName = userName; return(playerScore); }
/// <summary> /// Game Over Screen: /// </summary> /// <param name="currentScore"></param> static void DisplayGameOver(PlayerScoreTracker currentScore, Finch myFinch) { DisplayHeader("GAME OVER"); // Displays the player's score. DisplayPlayerScore(currentScore); // Bit of a musical blurb for the user. FinchOperator.PlayGameOverMarch(myFinch); DisplayContinuePrompt(); }
/// <summary> /// Processes menu selection for DisplayMainMenu. /// </summary> /// <param name="userName"></param> /// <param name="menuChoice"></param> /// <returns></returns> static bool MenuSwitchBoard(string userName, string menuChoice, Finch myFinch, FinchOperator myFinchOperator) { //Local Variables: Monitor saberHit = new Monitor(); bool runApp = true; PlayerScoreTracker playerScore = new PlayerScoreTracker(); playerScore.PlayerName = userName; switch (menuChoice.ToUpper()) { case "1": DisplayGameInProgress(myFinch, myFinchOperator); playerScore = CurrentPlayerScore(userName, playerScore, myFinchOperator); DisplayGameOver(playerScore, myFinch); break; case "2": DisplayInformationScreen(userName); break; case "3": DisplaySetupMenu(userName, myFinch, myFinchOperator); break; case "4": DisplaySavePlayerScoreToFile(playerScore); break; case "5": DisplayReadScoreFromFile(); break; case "6": FinchOperator.PlayImpMarch(myFinch); DisplayHeader("END OF SONG"); DisplayContinuePrompt(); break; case "E": runApp = false; break; default: Console.WriteLine("\t'{0}' is not a valid input. Please try again!", menuChoice); DisplayContinuePrompt(); Console.Clear(); break; } return(runApp); }
/// <summary> /// Reads the high scores list from the file. /// </summary> static void DisplayReadScoreFromFile() { // Local Variables: string dataPath = @"Data\Scores.txt"; List <PlayerScoreTracker> scoreList = new List <PlayerScoreTracker>(); PlayerScoreTracker tempPlayerScore = new PlayerScoreTracker(); try { DisplayHeader("Load Scores:"); Console.WriteLine(); Console.WriteLine("\tPress any key to retrieve saved scores:"); Console.ReadKey(); //File.WriteAllLines(dataPath, scoreList); string[] myArray = File.ReadAllLines(dataPath); DisplaySpacer(); foreach (string score in myArray) { string[] scoreArray = score.Split(','); tempPlayerScore.PlayerName = scoreArray[0]; tempPlayerScore.PlayerScore = int.Parse(scoreArray[1]); Console.WriteLine($"{tempPlayerScore.PlayerName}: {tempPlayerScore.PlayerScore} points"); } DisplayContinuePrompt(); } catch (FileNotFoundException) { DisplayHeader("ERROR"); Console.WriteLine("\tUnable to read scores from the file. The file may have been deleted or corrupted."); Console.WriteLine(); } catch (NullReferenceException) { DisplayHeader("ERROR"); Console.WriteLine("\tUnable to retrieve scores. No scores were found."); Console.WriteLine(); } catch (Exception e) { DisplayHeader("ERROR"); Console.WriteLine("\tAn error has occured. Please see the following for more information"); Console.WriteLine("[{0}]", e); } }
/// <summary> /// Saves the players score to a file: /// </summary> /// <param name="currentScore"></param> static void DisplaySavePlayerScoreToFile(PlayerScoreTracker currentScore) { // Adds the player's score to a list and then adds it to the file. try { // Varables: string dataPath = @"Data\Scores.txt"; List <string> playerScoreList = new List <string>(); string playerScoreToSave = "\n" + currentScore.PlayerName + ", " + currentScore.PlayerScore.ToString(); playerScoreList.Add(playerScoreToSave); File.AppendAllLines(dataPath, playerScoreList); // Message for user. DisplayHeader("Save Scores"); Console.WriteLine(); Console.WriteLine("\tScore have been successfully saved."); } catch (FileNotFoundException) { DisplayHeader("ERROR"); Console.WriteLine("\tUnable find the file containing scores. The file may have been deleted or corrupted."); Console.WriteLine(); } catch (NullReferenceException) { DisplayHeader("ERROR"); Console.WriteLine("\tUnable to save scores. No scores were found."); Console.WriteLine(); } catch (Exception e) { DisplayHeader("ERROR"); Console.WriteLine("\tAn error has occured. Please see the following for more information"); Console.WriteLine("[{0}]", e); } DisplayContinuePrompt(); }
/// <summary> /// Display the player's score: /// </summary> /// <param name="userName"></param> /// <param name="myFinchOperator"></param> static void DisplayPlayerScore(PlayerScoreTracker currentScore) { Console.WriteLine("\t{0} scored {1}", currentScore.PlayerName, currentScore.PlayerScore, " points"); }