コード例 #1
0
        /// <summary>
        /// Represents a method that executes commands
        /// entered from the console depending on the user input.
        /// </summary>
        /// <param name="input">Console Input Commands</param>
        /// <remarks>
        /// Only commands like "exit", "top", "restart" or
        /// number between [1, 15] is valid.
        /// </remarks>
        private void ParseInput(string input)
        {
            bool isMoveValid = false;

            if (input == Message.EXIT)
            {
                this.StopGame();
                return;
            }

            if (input == Message.RESTART)
            {
                this.RestartGame();
                return;
            }

            if (input == Message.TOP)
            {
                TopScore.PrintTopScores();
                return;
            }

            Position currentPosition = field.GetPosition(input);

            if (currentPosition == null)
            {
                isMoveValid = false;
                return;
            }

            // Atempts to move a number in the direction that has empty cell
            Movement.TryMovingInDirection(ref moveCount, ref isMoveValid, currentPosition, MoveDirection.Up, this.field);
            Movement.TryMovingInDirection(ref moveCount, ref isMoveValid, currentPosition, MoveDirection.Down, this.field);
            Movement.TryMovingInDirection(ref moveCount, ref isMoveValid, currentPosition, MoveDirection.Left, this.field);
            Movement.TryMovingInDirection(ref moveCount, ref isMoveValid, currentPosition, MoveDirection.Right, this.field);

            if (!isMoveValid)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(Message.INVALID_MOVE);
                Console.ResetColor();
            }
        }