/// <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); }
/// <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); }
public static bool CheckRouteIfJumpingIsPossible(int[] sequence) { bool jumpChoice = false; if (sequence.Length > 0) { List <int> solutions = GetSolutionsForGoal(sequence); if (solutions.Count > 0) { RouteHelper.DisplaySolutions(solutions, sequence); jumpChoice = GetToGoal(solutions, sequence); } } return(jumpChoice); }
private static bool GetToGoal(List <int> solutions, int[] sequence) { bool arrivedToSolution = false; int goal = sequence.Length; int currentPosition = 0; int jumpStrength = 0; bool deadEnd = false; int[] solutionRoute = new int[sequence.Length]; while (arrivedToSolution != true && deadEnd != true) { solutionRoute[currentPosition] = sequence[currentPosition]; jumpStrength = sequence[currentPosition]; if (jumpStrength > 0) { int initialCheck = goal - (jumpStrength + currentPosition); if (initialCheck <= 0) { arrivedToSolution = true; return(arrivedToSolution); } else { Dictionary <int, int> jumpOptions = new Dictionary <int, int>(); int deadEndCount = 0; for (int option = jumpStrength + currentPosition; option >= currentPosition + 1; option--) { int optionJumpStrength = sequence[option]; if (optionJumpStrength == 0) { deadEndCount++; if (jumpStrength == deadEndCount) { deadEnd = true; break; } continue; } int jump = optionJumpStrength + option; List <int> checkIfSolutionFound = solutions.Where(x => jump >= x).ToList(); if (checkIfSolutionFound.Count > 0) { solutionRoute[option] = sequence[option]; solutionRoute[checkIfSolutionFound[0]] = sequence[checkIfSolutionFound[0]]; Console.WriteLine($"You have found a way to solution by getting " + $"to position {option + 1} with the jump strength " + $"of {optionJumpStrength}\n"); arrivedToSolution = true; RouteHelper.DisplayRoute(sequence); Console.WriteLine("\nYou need to take the following path:\n"); RouteHelper.DisplayRoute(solutionRoute); break; } else { jumpOptions.Add(option, jump); } } if (jumpOptions.Any()) { int maxValue = jumpOptions.Values.Max(); currentPosition = jumpOptions.FirstOrDefault(x => x.Value == maxValue).Key; } } } } return(arrivedToSolution); }