<commit_before>﻿namespace Labyrinth
{
    using System;
    using System.Collections.Generic;
    using ConsoleMethods;

    /// <summary>
    /// This class controls the game Labyrinth using one instance at a time
    /// by the means of Singleton creative design pattern
    /// combined with the "Lazy Initialization" creative design pattern.
    /// </summary>
    public sealed class LabyrinthEngine : Game
    {
        private static readonly LabyrinthEngine gameInstance = new LabyrinthEngine();

        /// <summary>
        /// Explicit static constructor to tell C# compiler not to mark type as beforefieldinit;
        /// this provides for using the "Lazy Initialization" pattern, because
        /// the laziness of type initializers is only guaranteed by .NET when the type isn't marked
        /// with a special flag called "beforefieldinit".
        /// </summary>
        static LabyrinthEngine()
        {
        }

        private LabyrinthEngine()
        {
        }

        public static LabyrinthEngine GameInstance
        {
            get
            {
                return gameInstance;
            }
        }

        public static void Main()
        {           
            currentRow = GameStartRow;
            currentColumn = GameStartColumn;

            // char[,] labyrinth = new char[LabyrinthRowLength, LabyrinthColumnLength];

            var labyrinth = new Maze(LabyrinthRowLength);

            while (true)
            {
                Console.Clear();
                Console.WriteLine("Welcome to \"Labyrinth\" game. Please try to escape. Use 'top' to view the top \nscoreboard,'restart' to start a new game and 'exit' to quit the game.\n ");

                gameInitialized = false;
                gameEndedRecordScore = false;

                if (!gameInitialized)
                {
                    labyrinth.GenerateObstacles();
                }

                // Console.SetWindowSize(40, 50);
                // Console.BackgroundColor = ConsoleColor.DarkGray;

                DisplayLabyrinth(labyrinth);

                PlayGame(labyrinth, (uint)currentRow, (uint)currentColumn);

                if (gameEndedRecordScore) // used for adding score only when game is finished naturally and not by the restart command.
                {
                    AddScore(scores, currentMoves);
                }
            }
        }

        static void AddScore(List<Table> scores, int movesCount)
        {
            if (scores.Count != 0)
            {
                scores.Sort(delegate(Table s1, Table s2) { return s1.moves.CompareTo(s2.moves); });
            }

            if (scores.Count < 5)
            {
                Console.WriteLine("Please enter your nickname");
                string name = Console.ReadLine();
                scores.Add(new Table(movesCount, name));
                PrintTopScores(scores);
            }
            else 
            {
                if (scores[4].moves > movesCount)
                {
                    scores.Remove(scores[4]);
                    Console.WriteLine("Please enter your nickname");
                    string name = Console.ReadLine();
                    scores.Add(new Table(movesCount, name));
                    PrintTopScores(scores);
                }
            }

            gameEndedRecordScore = false;
        }

        static void PrintTopScores(List<Table> scores)
        {
            Console.WriteLine("\n");
            if (scores.Count == 0)
            {
                Console.WriteLine("The scoreboard is empty! ");
            }
            else
            {
                int i = 1;
                scores.Sort(delegate(Table s1, Table s2) { return s1.moves.CompareTo(s2.moves); });
                Console.WriteLine("Top 5: \n");
                scores.ForEach(delegate(Table s)
                {
                    Console.WriteLine(string.Format(i + ". {1} ---> {0} moves", s.moves, s.name));
                    i++;
                });

                Console.WriteLine("\n");
            }
        }

        static void PlayGame(Maze labyrinth, uint row, uint col)
        {      
            bool gameInProgress = true;
            currentMoves = 0;
            Console.WriteLine("\nEnter your move (Left Arrow = left, Right Arrow = right, Down Arrow = down, Up Arrow = up)");
            Console.SetCursorPosition(0, 15);

            while (gameInProgress)
            {
                ConsoleKeyInfo userChoice = Console.ReadKey(true);

                // Set currsor position outside of labyrinth and deletes previous turn message if any
                // I think this should be edited latter to not use magic numbers 
                Console.SetCursorPosition(0, 15);
                ConsoleManipulation.ClearCurrentLine();

                switch (userChoice.Key)
                {
                    case ConsoleKey.DownArrow:
                        TryMoveDown(labyrinth, ref gameInProgress, ref row, col);
                        break;

                    case ConsoleKey.UpArrow:
                        TryMoveUp(labyrinth, ref gameInProgress, ref row, col);
                        break;

                    case ConsoleKey.RightArrow:
                        TryMoveRight(labyrinth, ref gameInProgress, row, ref col);
                        break;

                    case ConsoleKey.LeftArrow:
                        TryMoveLeft(labyrinth, ref gameInProgress, row, ref col);
                        break;

                    case ConsoleKey.T:
                        PrintTopScores(scores);
                        Console.WriteLine("\n");
                        DisplayLabyrinth(labyrinth);
                        break;

                    case ConsoleKey.R:
                        gameInProgress = false;
                        break;

                    case ConsoleKey.E:
                        Console.WriteLine("Good bye!");
                        Environment.Exit(0);
                        break;

                    default:
                        Console.WriteLine("Invalid command!");
                        break;
                }
            }
        }

        private static void TryMoveLeft(Maze labyrinth, ref bool gameInProgress, uint row, ref uint col)
        {
            if (labyrinth[row, col - 1] == '-')
            {
                labyrinth[row, col] = '-';
                Console.SetCursorPosition(2* (int)col, (int)row +3);
                Console.Write('-');
                col--;<commit_msg>Fixed a bug where when the score was shown a new labyrinth was drawn bellow the existing<commit_after>﻿namespace Labyrinth
{
    using System;
    using System.Collections.Generic;
    using ConsoleMethods;

    /// <summary>
    /// This class controls the game Labyrinth using one instance at a time
    /// by the means of Singleton creative design pattern
    /// combined with the "Lazy Initialization" creative design pattern.
    /// </summary>
    public sealed class LabyrinthEngine : Game
    {
        private static readonly LabyrinthEngine gameInstance = new LabyrinthEngine();

        /// <summary>
        /// Explicit static constructor to tell C# compiler not to mark type as beforefieldinit;
        /// this provides for using the "Lazy Initialization" pattern, because
        /// the laziness of type initializers is only guaranteed by .NET when the type isn't marked
        /// with a special flag called "beforefieldinit".
        /// </summary>
        static LabyrinthEngine()
        {
        }

        private LabyrinthEngine()
        {
        }

        public static LabyrinthEngine GameInstance
        {
            get
            {
                return gameInstance;
            }
        }

        public static void Main()
        {           
            currentRow = GameStartRow;
            currentColumn = GameStartColumn;

            // char[,] labyrinth = new char[LabyrinthRowLength, LabyrinthColumnLength];

            var labyrinth = new Maze(LabyrinthRowLength);

            while (true)
            {
                Console.Clear();
                Console.WriteLine("Welcome to \"Labyrinth\" game. Please try to escape. Use 'top' to view the top \nscoreboard,'restart' to start a new game and 'exit' to quit the game.\n ");

                gameInitialized = false;
                gameEndedRecordScore = false;

                if (!gameInitialized)
                {
                    labyrinth.GenerateObstacles();
                }

                // Console.SetWindowSize(40, 50);
                // Console.BackgroundColor = ConsoleColor.DarkGray;

                DisplayLabyrinth(labyrinth);

                PlayGame(labyrinth, (uint)currentRow, (uint)currentColumn);

                if (gameEndedRecordScore) // used for adding score only when game is finished naturally and not by the restart command.
                {
                    AddScore(scores, currentMoves);
                }
            }
        }

        static void AddScore(List<Table> scores, int movesCount)
        {
            if (scores.Count != 0)
            {
                scores.Sort(delegate(Table s1, Table s2) { return s1.moves.CompareTo(s2.moves); });
            }

            if (scores.Count < 5)
            {
                Console.WriteLine("Please enter your nickname");
                string name = Console.ReadLine();
                scores.Add(new Table(movesCount, name));
                PrintTopScores(scores);
            }
            else 
            {
                if (scores[4].moves > movesCount)
                {
                    scores.Remove(scores[4]);
                    Console.WriteLine("Please enter your nickname");
                    string name = Console.ReadLine();
                    scores.Add(new Table(movesCount, name));
                    PrintTopScores(scores);
                }
            }

            gameEndedRecordScore = false;
        }

        static void PrintTopScores(List<Table> scores)
        {
            Console.SetCursorPosition(0, 15);
            Console.WriteLine("\n");
            if (scores.Count == 0)
            {
                Console.WriteLine("The scoreboard is empty! ");
            }
            else
            {
                int i = 1;
                scores.Sort(delegate(Table s1, Table s2) { return s1.moves.CompareTo(s2.moves); });
                Console.WriteLine("Top 5: \n");
                scores.ForEach(delegate(Table s)
                {
                    Console.WriteLine(string.Format(i + ". {1} ---> {0} moves", s.moves, s.name));
                    i++;
                });

                Console.WriteLine("\n");
            }
        }

        static void PlayGame(Maze labyrinth, uint row, uint col)
        {      
            bool gameInProgress = true;
            currentMoves = 0;
            Console.WriteLine("\nEnter your move (Left Arrow = left, Right Arrow = right, Down Arrow = down, Up Arrow = up)");
            Console.SetCursorPosition(0, 15);

            while (gameInProgress)
            {
                ConsoleKeyInfo userChoice = Console.ReadKey(true);

                // Set currsor position outside of labyrinth and deletes previous turn message if any
                // I think this should be edited latter to not use magic numbers 
                Console.SetCursorPosition(0, 15);
                ConsoleManipulation.ClearCurrentLine();

                switch (userChoice.Key)
                {
                    case ConsoleKey.DownArrow:
                        TryMoveDown(labyrinth, ref gameInProgress, ref row, col);
                        break;

                    case ConsoleKey.UpArrow:
                        TryMoveUp(labyrinth, ref gameInProgress, ref row, col);
                        break;

                    case ConsoleKey.RightArrow:
                        TryMoveRight(labyrinth, ref gameInProgress, row, ref col);
                        break;

                    case ConsoleKey.LeftArrow:
                        TryMoveLeft(labyrinth, ref gameInProgress, row, ref col);
                        break;

                    case ConsoleKey.T:
                        PrintTopScores(scores);
                        break;

                    case ConsoleKey.R:
                        gameInProgress = false;
                        break;

                    case ConsoleKey.E:
                        Console.WriteLine("Good bye!");
                        Environment.Exit(0);
                        break;

                    default:
                        Console.WriteLine("Invalid command!");
                        break;
                }
            }
        }

        private static void TryMoveLeft(Maze labyrinth, ref bool gameInProgress, uint row, ref uint col)
        {
            if (labyrinth[row, col - 1] == '-')
            {
                labyrinth[row, col] = '-';
                Console.SetCursorPosition(2* (int)col, (int)row +3);
                Console.Write('-');
                col--;
{'fraction_non_alphanumeric': 0.08501408021919477, 'fraction_numerical': 0.005099322627292792, 'mean_word_length': 1.2820423758249393, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '342437', 'n_tokens_mistral': 3308, 'n_tokens_neox': 2954, 'n_words': 847}