コード例 #1
0
        /// <summary>
        /// The method get the user input for wanted baloon to pop. It is used if no other command from the user
        /// is executed. If the user input for row and col is not valid, the method print appropriate message to the user.
        /// </summary>
        /// <param name="input"></param>
        private void ProcessInputBalloonPosition(string input)
        {
            try
            {
                var splittedUserInput = input.Split(' ');
                int currentRow        = int.Parse(splittedUserInput[0]);
                int currentCol        = int.Parse(splittedUserInput[1]);

                if (this.IsLegalMove(currentRow, currentCol))
                {
                    this.RemoveAllBaloons(currentRow, currentCol);
                }
                else
                {
                    ConsoleIOFacade.PrintInvalidMove();
                }
            }
            catch (FormatException)
            {
                //extract to consoleIO or remove
                Console.WriteLine("Row and col are not entered in the valid format.");
                ConsoleIOFacade.PrintInvalidInput();
            }
            catch (IndexOutOfRangeException)
            {
                // extract to ConsoleIOFacade or remove
                Console.WriteLine("You did not enter two numbers for row and col.");
                ConsoleIOFacade.PrintInvalidInput();
            }
        }
コード例 #2
0
        /// <summary>
        /// After winning a game, this method is used to ask the user for it's name
        /// and add him / her to the scoreboard.
        /// </summary>
        private void AddUserToScoreboard()
        {
            ConsoleIOFacade.PrintWinMessage(this.userMoves);

            string username = ConsoleIOFacade.ReadUserName();

            this.statistics.Add(this.userMoves, username);
        }
コード例 #3
0
        /// <summary>
        /// This method initialize the playfield and the strategy for popping the balloons and then
        /// initilize the game. After everything is ready starts the game life cycle.
        /// </summary>
        public void Start()
        {
            Playfield   gamePlayfield   = this.InitializePlayfield();
            PopStrategy gamePopStrategy = new RecursivePopStrategy();

            this.InitializeGame(gamePlayfield, gamePopStrategy);
            ConsoleIOFacade.PrintWelcomeMessage();
            ConsoleIOFacade.PrintTable(this.playfield);
            this.PlayGame();
        }
        public void ScoreboardStringCreationTest()
        {
            var statistics = new OrderedMultiDictionary <int, string>(true);

            statistics.Add(5, "Gosho");
            statistics.Add(15, "Pesho");

            var scoreboard = "Scoreboard:" + Environment.NewLine +
                             "2. {Gosho} --> 5 moves" + Environment.NewLine +
                             "2. {Pesho} --> 15 moves" + Environment.NewLine;

            Assert.AreEqual(scoreboard, ConsoleIOFacade.CreateScoreboardString(statistics));
        }
コード例 #5
0
        /// <summary>
        /// This method is responsible for processing the user input on each loop. If the user input is not
        /// a valid text command it process it as coordinates for a move.
        /// </summary>
        /// <param name="input"></param>
        private void ProcessInput(string input)
        {
            switch (input)
            {
            case "top":
                ConsoleIOFacade.CreateScoreboardString(this.statistics);
                break;

            case "restart":
                this.Start();
                break;

            case "exit":
                this.Exit();
                break;

            default:
                this.ProcessInputBalloonPosition(input);
                break;
            }
        }
コード例 #6
0
        /// <summary>
        /// This method runs the game life cycle, and waits for a user input on each loop. When the life cycle
        /// ends it adds the user to the score board and print the statistics.
        /// </summary>
        private void PlayGame()
        {
            while (!IsFinished)
            {
                this.userMoves++;

                string currentInput = ConsoleIOFacade.ReadInput();

                this.ProcessInput(currentInput);

                ConsoleIOFacade.PrintTable(this.playfield);
            }

            this.AddUserToScoreboard();

            string scoreboard = ConsoleIOFacade.CreateScoreboardString(this.statistics);

            Console.WriteLine(scoreboard);

            this.ProcessUserDescision();
        }
コード例 #7
0
        /// <summary>
        /// This method implements the factory design pattern for instantiating a playfield
        /// with the user desired dimensions.
        /// </summary>
        /// <returns></returns>
        private Playfield InitializePlayfield()
        {
            int       playfieldSize = ConsoleIOFacade.ReadPlayfieldSize();
            Playfield playfield     = null;

            bool isPlayfieldSizeIncorrect = true;

            while (isPlayfieldSizeIncorrect)
            {
                PlayfieldFactory playfiledFactory = null;

                switch (playfieldSize)
                {
                case 1:
                    playfiledFactory         = new SmallPlayfieldFactory();
                    playfield                = playfiledFactory.CreatePlayfield();
                    isPlayfieldSizeIncorrect = false;
                    break;

                case 2:
                    playfiledFactory         = new MediumPlayfieldFactory();
                    playfield                = playfiledFactory.CreatePlayfield();
                    isPlayfieldSizeIncorrect = false;
                    break;

                case 3:
                    playfiledFactory         = new LargePlayfieldFactory();
                    playfield                = playfiledFactory.CreatePlayfield();
                    isPlayfieldSizeIncorrect = false;
                    break;

                default:
                    // Extract to consoleIO
                    Console.WriteLine("You have entered incorrect field size");
                    break;
                }
            }

            return(playfield);
        }
コード例 #8
0
        /// <summary>
        /// The method prints an exit message and exit the game.
        /// </summary>
        private void Exit()
        {
            ConsoleIOFacade.PrintExitMessage(this.userMoves, this.balloonsLeft);

            Environment.Exit(0);
        }