How to get recursion and for loop work together in Java?

Your problem is not in the part you posted I added dummy implementations for all the classes and methods used in your method, and it runs quite fine, stopping at level 6 package de. Fencing_game.paul. Examples; import java.util.

*; public class RecursionAndLoop { private static class Move { private String id; public Move(String type) { this. Id = type; } public String toString(){ return "Move(" + id + ")"; } } private static class GameState { private static int nextID; private int id; GameState() { this. Id = nextID++; } public GameState getNewInstance(Move move) { return new GameState(); } public int getPossibleMoveCount(int index) { return 5; } public Vector getPossibleMoves(int index) { Vector v = new Vector(); for(int I = 0; I children; private Node parent; private int score; public Node(GameState s, Move m) { this.

Children = new ArrayList(); this. State = s; this. Move = m; } public void addChild(Node child) { children.

Add(child); } public void setParent(Node node) { parent = node; } public void setScore(int neu) { this. Score = neu; } public int getDepth() { if(parent == null) { return 0; } return 1 + parent.getDepth(); } /** * prints a simple tree view of this ZipNode and its descendants * on {@link System. Out}.

* @param prefix a prefix string to add before all lines. * @param self a prefix string to add before the line of this node * itself (after the general prefix). * @param sub a prefix string to add before the line of all subnodes * of this node (after the general prefix).

*/ private void printTree(String prefix, String self, String sub) { System.out. Println(prefix + self + state + " - " + move + " - " + score); String subPrefix = prefix + sub; // the prefix strings for the next level. String nextSelf = " ├─ "; String nextSub = " │ "; Iterator iterator = this.children.iterator(); while(iterator.hasNext()) { Node child = iterator.next(); if(!iterator.hasNext() ) { // last item, without the "|" nextSelf = " └─ "; nextSub = " "; } child.

PrintTree(subPrefix, nextSelf, nextSub); } } } int switchIndex(int index) { return index + 1; } private void makeTree(GameState prevState, Vector moves, Node parentNode, int index, int depthLimit) { if(prevState. GetPossibleMoveCount(index)! = 0){ for(int I = 0; I Get(i); GameState newState = prevState.

GetNewInstance(thisMove); Node child = new Node(newState, thisMove); parentNode. AddChild(child); child. SetParent(parentNode); if((child.getDepth() + 1) GetMarkCount(index)); } } } } public static void main(String params) { GameState start = new GameState(); Vector m = new Vector(); m.

Add(new Move("start")); Node root = new Node(start, null); int index = 7; int depthLimit = 6; new RecursionAndLoop(). MakeTree(start, m, root, index, depthLimit); root. PrintTree("", " ", ""); } } (I changed a bit to generic types to avoid compiler warnings.

) Here is the output for depthLimit=4: GameState0 - null - 0 └─ GameState1 - Move(start) - 0 ├─ GameState2 - Move(8×0) - 0 │ ├─ GameState3 - Move(9×0) - 29 │ ├─ GameState4 - Move(9×1) - 29 │ ├─ GameState5 - Move(9×2) - 29 │ ├─ GameState6 - Move(9×3) - 29 │ └─ GameState7 - Move(9×4) - 29 ├─ GameState8 - Move(8×1) - 0 │ ├─ GameState9 - Move(9×0) - 29 │ ├─ GameState10 - Move(9×1) - 29 │ ├─ GameState11 - Move(9×2) - 29 │ ├─ GameState12 - Move(9×3) - 29 │ └─ GameState13 - Move(9×4) - 29 ├─ GameState14 - Move(8×2) - 0 │ ├─ GameState15 - Move(9×0) - 29 │ ├─ GameState16 - Move(9×1) - 29 │ ├─ GameState17 - Move(9×2) - 29 │ ├─ GameState18 - Move(9×3) - 29 │ └─ GameState19 - Move(9×4) - 29 ├─ GameState20 - Move(8×3) - 0 │ ├─ GameState21 - Move(9×0) - 29 │ ├─ GameState22 - Move(9×1) - 29 │ ├─ GameState23 - Move(9×2) - 29 │ ├─ GameState24 - Move(9×3) - 29 │ └─ GameState25 - Move(9×4) - 29 └─ GameState26 - Move(8×4) - 0 ├─ GameState27 - Move(9×0) - 29 ├─ GameState28 - Move(9×1) - 29 ├─ GameState29 - Move(9×2) - 29 ├─ GameState30 - Move(9×3) - 29 └─ GameState31 - Move(9×4) - 29.

Your problem is not in the part you posted. I added dummy implementations for all the classes and methods used in your method, and it runs quite fine, stopping at level 6. Package de.

Fencing_game.paul. Examples; import java.util. *; public class RecursionAndLoop { private static class Move { private String id; public Move(String type) { this.Id = type; } public String toString(){ return "Move(" + id + ")"; } } private static class GameState { private static int nextID; private int id; GameState() { this.

Id = nextID++; } public GameState getNewInstance(Move move) { return new GameState(); } public int getPossibleMoveCount(int index) { return 5; } public Vector getPossibleMoves(int index) { Vector v = new Vector(); for(int I = 0; I children; private Node parent; private int score; public Node(GameState s, Move m) { this. Children = new ArrayList(); this. State = s; this.

Move = m; } public void addChild(Node child) { children. Add(child); } public void setParent(Node node) { parent = node; } public void setScore(int neu) { this. Score = neu; } public int getDepth() { if(parent == null) { return 0; } return 1 + parent.getDepth(); } /** * prints a simple tree view of this ZipNode and its descendants * on {@link System.

Out}. * @param prefix a prefix string to add before all lines. * @param self a prefix string to add before the line of this node * itself (after the general prefix).

* @param sub a prefix string to add before the line of all subnodes * of this node (after the general prefix). */ private void printTree(String prefix, String self, String sub) { System.out. Println(prefix + self + state + " - " + move + " - " + score); String subPrefix = prefix + sub; // the prefix strings for the next level.

String nextSelf = " ├─ "; String nextSub = " │ "; Iterator iterator = this.children.iterator(); while(iterator.hasNext()) { Node child = iterator.next(); if(!iterator.hasNext() ) { // last item, without the "|" nextSelf = " └─ "; nextSub = " "; } child. PrintTree(subPrefix, nextSelf, nextSub); } } } int switchIndex(int index) { return index + 1; } private void makeTree(GameState prevState, Vector moves, Node parentNode, int index, int depthLimit) { if(prevState. GetPossibleMoveCount(index)!

= 0){ for(int I = 0; I GetNewInstance(thisMove); Node child = new Node(newState, thisMove); parentNode. AddChild(child); child. SetParent(parentNode); if((child.getDepth() + 1) GetPossibleMoves(newIndex); makeTree(newState, newMoves, child, newIndex, depthLimit); }else{ child.

SetScore(newState. GetMarkCount(index)); } } } } public static void main(String params) { GameState start = new GameState(); Vector m = new Vector(); m. Add(new Move("start")); Node root = new Node(start, null); int index = 7; int depthLimit = 6; new RecursionAndLoop().

MakeTree(start, m, root, index, depthLimit); root. PrintTree("", " ", ""); } } (I changed a bit to generic types to avoid compiler warnings.) Here is the output for depthLimit=4: GameState0 - null - 0 └─ GameState1 - Move(start) - 0 ├─ GameState2 - Move(8×0) - 0 │ ├─ GameState3 - Move(9×0) - 29 │ ├─ GameState4 - Move(9×1) - 29 │ ├─ GameState5 - Move(9×2) - 29 │ ├─ GameState6 - Move(9×3) - 29 │ └─ GameState7 - Move(9×4) - 29 ├─ GameState8 - Move(8×1) - 0 │ ├─ GameState9 - Move(9×0) - 29 │ ├─ GameState10 - Move(9×1) - 29 │ ├─ GameState11 - Move(9×2) - 29 │ ├─ GameState12 - Move(9×3) - 29 │ └─ GameState13 - Move(9×4) - 29 ├─ GameState14 - Move(8×2) - 0 │ ├─ GameState15 - Move(9×0) - 29 │ ├─ GameState16 - Move(9×1) - 29 │ ├─ GameState17 - Move(9×2) - 29 │ ├─ GameState18 - Move(9×3) - 29 │ └─ GameState19 - Move(9×4) - 29 ├─ GameState20 - Move(8×3) - 0 │ ├─ GameState21 - Move(9×0) - 29 │ ├─ GameState22 - Move(9×1) - 29 │ ├─ GameState23 - Move(9×2) - 29 │ ├─ GameState24 - Move(9×3) - 29 │ └─ GameState25 - Move(9×4) - 29 └─ GameState26 - Move(8×4) - 0 ├─ GameState27 - Move(9×0) - 29 ├─ GameState28 - Move(9×1) - 29 ├─ GameState29 - Move(9×2) - 29 ├─ GameState30 - Move(9×3) - 29 └─ GameState31 - Move(9×4) - 29.

Thanks for very clear and deep answer. I thought that the recursive function is making some weird mistakes, but maybe the problem is somewhere else then. I wonder where it might be.

Well more debugging now. – zaplec Mar 8 at 21:54.

It looks like you're passing in depth to the makeTree routine, yet expecting it to be the depth of the current node (this. Depth maybe?).

Sorry I forgot mention in the question that the depth argument is the depth limit. It is passed in the method during the first call and it remains the same during the tree creation. – zaplec Mar 8 at 20:27.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions