Esempio n. 1
0
        /// <summary>
        /// This method is used to decide wheather you should jump to the goal or not.
        /// </summary>
        /// <param name="sequence"></param>
        /// <returns></returns>
        public static bool IsItSafeToJump(int[] sequence)
        {
            JumpParameters parameters = new JumpParameters(sequence);

            if (parameters.Solutions.Count > 0)
            {
                RouteHelper.DisplaySolutions(parameters.Solutions);
                RouteHelper.DisplayRoute(sequence);
                parameters.JumpChoice = GetToSolution(parameters, sequence);
            }
            return(parameters.JumpChoice);
        }
Esempio n. 2
0
        /// <summary>
        /// This method is used to show the best path to the goal.
        /// </summary>
        /// <param name="parameters"></param>
        private static void DisplayBestRoute(JumpParameters parameters)
        {
            int position     = parameters.Position;
            int jumpStrength = parameters.JumpStrength;

            Console.WriteLine($"\nYou have found a way to the goal by getting " +
                              $"to position {position + 1} with the jump strength " +
                              $"of {parameters.JumpStrength}\n");
            //RouteHelper.DisplayRoute(sequence);
            Console.WriteLine("\nYou need to take the following path:\n");
            RouteHelper.DisplayRoute(parameters.BestRoute);
        }
Esempio n. 3
0
        /// <summary>
        /// This method is used to check if it is possible to arrive to goal, and if possible, it prints out the best path.
        /// </summary>
        /// <param name="parameters"></param>
        /// <param name="sequence"></param>
        /// <returns></returns>
        private static bool GetToSolution(JumpParameters parameters, int[] sequence)
        {
            while (parameters.ArrivedToSolution != true && parameters.DeadEnd != true)
            {
                parameters.BestRoute[parameters.Position] = sequence[parameters.Position];
                parameters.JumpStrength = sequence[parameters.Position];

                if (parameters.JumpStrength > 0)
                {
                    int jump         = parameters.JumpStrength + parameters.Position;
                    int initialCheck = parameters.Goal - jump;
                    // Initial jump check
                    if (initialCheck <= 0)
                    {
                        parameters.ArrivedToSolution = true;
                        DisplayBestRoute(parameters);
                        return(parameters.ArrivedToSolution);
                    }
                    else
                    {
                        // Optional jump checks from current position
                        Dictionary <int, int> jumpOptions = new Dictionary <int, int>();
                        int deadEndCount = 0;
                        for (int option = jump; option >= parameters.Position + 1; option--)
                        {
                            int optionJumpStrength = sequence[option];
                            if (optionJumpStrength == 0)
                            {
                                deadEndCount++;
                                if (parameters.JumpStrength == deadEndCount)
                                {
                                    parameters.DeadEnd = true;
                                    break;
                                }
                                continue;
                            }

                            int  optionalJump         = optionJumpStrength + option;
                            bool checkIfSolutionFound = parameters.Solutions.ContainsKey(optionalJump);

                            if (checkIfSolutionFound)
                            {
                                parameters.BestRoute[option]       = sequence[option];
                                parameters.BestRoute[optionalJump] = sequence[optionalJump];
                                parameters.Position          = optionalJump;
                                parameters.JumpStrength      = sequence[optionalJump];
                                parameters.ArrivedToSolution = true;
                                DisplayBestRoute(parameters);
                                break;
                            }
                            else
                            {
                                jumpOptions.Add(option, optionalJump);
                            }
                        }
                        //Jump to the best option if applicable
                        if (jumpOptions.Any())
                        {
                            parameters.Position = GetBestJumpOption(jumpOptions);
                        }
                    }
                }
            }
            return(parameters.ArrivedToSolution);
        }