@@ -22,7 +22,7 @@ public static void main(String[] args) {
2222 }
2323 }
2424
25- public static void printEachStep (State state ) {
25+ private static void printEachStep (State state ) {
2626 List <Stack <String >> stackList = state .getState ();
2727 System .out .println ("----------------" );
2828 stackList .forEach (stack -> {
@@ -33,23 +33,18 @@ public static void printEachStep(State state) {
3333 });
3434 }
3535
36- public Stack <String > getStackWithValues (String [] blocks ) {
37- Stack <String > stack = new Stack <String >();
36+ private Stack <String > getStackWithValues (String [] blocks ) {
37+ Stack <String > stack = new Stack <>();
3838 for (String block : blocks )
3939 stack .push (block );
4040 return stack ;
4141 }
4242
4343 /**
4444 * This method prepares path from init state to goal state
45- *
46- * @param initStateStack
47- * @param goalStateStack
48- * @return
49- * @throws Exception
5045 */
5146 public List <State > getRouteWithHillClimbing (Stack <String > initStateStack , Stack <String > goalStateStack ) throws Exception {
52- List <Stack <String >> initStateStackList = new ArrayList <Stack < String > >();
47+ List <Stack <String >> initStateStackList = new ArrayList <>();
5348 initStateStackList .add (initStateStack );
5449 int initStateHeuristics = getHeuristicsValue (initStateStackList , goalStateStack );
5550 State initState = new State (initStateStackList , initStateHeuristics );
@@ -71,27 +66,21 @@ public List<State> getRouteWithHillClimbing(Stack<String> initStateStack, Stack<
7166 }
7267 }
7368
74- if (noStateFound )
75- throw new Exception ("No path found" );
7669 return resultPath ;
7770 }
7871
7972 /**
8073 * This method finds new state from current state based on goal and
8174 * heuristics
82- *
83- * @param currentState
84- * @param goalStateStack
85- * @return a next state
8675 */
8776 public State findNextState (State currentState , Stack <String > goalStateStack ) {
8877 List <Stack <String >> listOfStacks = currentState .getState ();
8978 int currentStateHeuristics = currentState .getHeuristics ();
9079
91- Optional < State > newState = listOfStacks .stream ()
80+ return listOfStacks .stream ()
9281 .map (stack -> {
93- State tempState = null ;
94- List <Stack <String >> tempStackList = new ArrayList <Stack < String > >(listOfStacks );
82+ State tempState ;
83+ List <Stack <String >> tempStackList = new ArrayList <>(listOfStacks );
9584 String block = stack .pop ();
9685 if (stack .size () == 0 )
9786 tempStackList .remove (stack );
@@ -104,24 +93,17 @@ public State findNextState(State currentState, Stack<String> goalStateStack) {
10493 return tempState ;
10594 })
10695 .filter (Objects ::nonNull )
107- .findFirst ();
108-
109- return newState .orElse (null );
96+ .findFirst ()
97+ .orElse (null );
11098 }
11199
112100 /**
113101 * Operation to be applied on a state in order to find new states. This
114102 * operation pushes an element into a new stack
115- *
116- * @param currentStackList
117- * @param block
118- * @param currentStateHeuristics
119- * @param goalStateStack
120- * @return a new state
121103 */
122104 private State pushElementToNewStack (List <Stack <String >> currentStackList , String block , int currentStateHeuristics , Stack <String > goalStateStack ) {
123105 State newState = null ;
124- Stack <String > newStack = new Stack <String >();
106+ Stack <String > newStack = new Stack <>();
125107 newStack .push (block );
126108
127109 currentStackList .add (newStack );
@@ -138,13 +120,6 @@ private State pushElementToNewStack(List<Stack<String>> currentStackList, String
138120 * Operation to be applied on a state in order to find new states. This
139121 * operation pushes an element into one of the other stacks to explore new
140122 * states
141- *
142- * @param stack
143- * @param currentStackList
144- * @param block
145- * @param currentStateHeuristics
146- * @param goalStateStack
147- * @return a new state
148123 */
149124 private State pushElementToExistingStacks (Stack currentStack , List <Stack <String >> currentStackList , String block , int currentStateHeuristics , Stack <String > goalStateStack ) {
150125
@@ -168,14 +143,10 @@ private State pushElementToExistingStacks(Stack currentStack, List<Stack<String>
168143 /**
169144 * This method returns heuristics value for given state with respect to goal
170145 * state
171- *
172- * @param currentState
173- * @param goalStateStack
174- * @return
175146 */
176147 public int getHeuristicsValue (List <Stack <String >> currentState , Stack <String > goalStateStack ) {
177148
178- Integer heuristicValue = 0 ;
149+ Integer heuristicValue ;
179150 heuristicValue = currentState .stream ()
180151 .mapToInt (stack -> {
181152 int stackHeuristics = 0 ;
0 commit comments