Exemple #1
0
        /// <summary>
        /// Displays score history
        /// </summary>
        /// <returns>Play a new game (true or false)</returns>
        public static bool ShowScoreHistory()
        {
            // clear console
            Console.Clear();
            Console.CursorVisible = false;
            ShowGameScreen();

            // write title
            WriteRichText(
                new String(' ', Console.WindowWidth),
                PositionY: 6,
                ForegroundColor: ConsoleColor.Black,
                BackgroundColor: ConsoleColor.White);
            WriteRichText(
                "Historique des procès",
                -1,
                6,
                ForegroundColor: ConsoleColor.Black,
                BackgroundColor: ConsoleColor.White);

            // define first line to write scores
            int ScoreLine = 8;

            // define a list to store scores
            List <string> Scores = new List <string>();

            // read Scores file from relative \Resources folder
            // retrieve file path
            string FilePath = Path.GetDirectoryName(
                Path.GetDirectoryName(
                    Directory.GetCurrentDirectory()))
                              + @"\Resources\Scores";

            // read file
            Scores = File.ReadAllLines(
                FilePath)
                     .ToList();
            // reverse list order (newer first)
            Scores.Reverse();

            // browse each score and format it before displaying
            // {0} -> player name
            // {1} -> result (victory or defeat)
            // {2} -> date and time of the game
            // {3} -> difficulty level
            // {4} -> mystery word
            foreach (string CurrentScore in Scores)
            {
                // split score string by its separator (,)
                string[] CurrentScoreData = CurrentScore.Split(',');
                // define color depending on result (with a ternary)
                ConsoleColor ScoreColor =
                    (CurrentScoreData[1] == "GAGNÉ" ?
                     ConsoleColor.Green :
                     ConsoleColor.Red);
                // write formatted string
                string ScoreString = string.Format(
                    "{0} a {1} le {2} au niveau {3} avec le mot {4}",
                    CurrentScoreData[0],
                    CurrentScoreData[1],
                    CurrentScoreData[2],
                    CurrentScoreData[3],
                    CurrentScoreData[4]);
                WriteRichText(
                    ScoreString,
                    -1,
                    ScoreLine,
                    ScoreColor);
                // write name in white
                WriteRichText(
                    CurrentScoreData[0],
                    (Console.WindowWidth - ScoreString.Length) / 2,
                    ScoreLine,
                    ConsoleColor.White);

                // increment line number
                ScoreLine++;
            }

            // ask player for a new game
            string NewGameMessage = "Voulez-vous un nouveau procès ? (O)ui ou (N)on : ";

            WriteRichText(
                NewGameMessage,
                -1,
                3);
            Console.SetCursorPosition(
                ((Console.WindowWidth - NewGameMessage.Length) / 2) + NewGameMessage.Length,
                3);

            // wait for player entry
            ConsoleKeyInfo SubmittedCharacter = new ConsoleKeyInfo();

            while (SubmittedCharacter.Key != ConsoleKey.O &&
                   SubmittedCharacter.Key != ConsoleKey.N)
            {
                Console.SetCursorPosition(
                    ((Console.WindowWidth - NewGameMessage.Length) / 2) + NewGameMessage.Length,
                    3);
                SubmittedCharacter = Console.ReadKey(true);
            }

            // return result
            return(SubmittedCharacter.Key == ConsoleKey.O);
        }