コード例 #1
0
        /// <summary>
        /// Uses the UI to display the player's death and observes if the
        /// player is in the top 10 scores, prompting him to input his name
        /// if necessary.
        /// </summary>
        private void EndGame()
        {
            UI.WriteMessage("You died.");

            if (highscoreTable.IsHighscore(gameValues.Level))
            {
                //Prompts player to type their username
                string username = UI.PromptUsername();
                //Adds score to highscoreTable
                highscoreTable.AddScore(username, gameValues.Level);
                //Save changes
                SaveManager.Save(highscoreTable);
            }

            //Reset values
            gameValues.Level = 0;
            gameValues.Hp    = (gameValues.Height * gameValues.Width) / 4;
        }
コード例 #2
0
ファイル: SaveManager.cs プロジェクト: condmaker/lp1_proj3
        /// <summary>
        /// Load the data of an Highscore Table form a file
        /// </summary>
        /// <returns> Properly filled Highscore Table </returns>
        public static HighscoreTable Load()
        {
            // Instatiate a new highscore table reference
            HighscoreTable highscore = new HighscoreTable();

            string scoreData = "";

            StreamWriter sw = new StreamWriter("scores.txt");

            sw.Close();

            // Open file in readeable mode
            StreamReader sr = new StreamReader("scores.txt");



            // Cycle through every line inside the file
            while ((scoreData = sr.ReadLine()) != null)
            {
                // Split the data in two strings: Name and Score.
                string[] splitData = scoreData.Split();
                int      score;

                // Parse the score data into an int
                if (!int.TryParse(splitData[1], out score))
                {
                    UI.WriteMessage("Error.");
                    Environment.Exit(0);
                }

                // Adds score to new HighscoreTable
                highscore.AddScore(splitData[0], score);
            }

            // Close the file.
            sr.Close();

            // Returns the Highscore table.
            return(highscore);
        }