DroidFish: Don't use recursion in GameTree.translateMovesHelper(). Recursion can crash with stack overflow error for long pgn games.

This commit is contained in:
Peter Osterlund 2012-09-22 09:04:57 +00:00
parent b40717603f
commit 0456c02cf9

@ -233,17 +233,34 @@ public class GameTree {
} }
private final void translateMovesHelper() { private final void translateMovesHelper() {
ArrayList<Integer> currPath = new ArrayList<Integer>();
currPath.add(0);
while (!currPath.isEmpty()) {
int last = currPath.size() - 1;
int currChild = currPath.get(last);
if (currChild == 0) {
ArrayList<Move> moves = MoveGen.instance.legalMoves(currentPos); ArrayList<Move> moves = MoveGen.instance.legalMoves(currentPos);
currentNode.verifyChildren(currentPos, moves); currentNode.verifyChildren(currentPos, moves);
int nc = currentNode.children.size(); int nc = currentNode.children.size();
for (int i = 0; i < nc; i++) { for (int i = 0; i < nc; i++) {
Node child = currentNode.children.get(i); Node child = currentNode.children.get(i);
child.moveStrLocal = TextIO.moveToString(currentPos, child.move, false, true, moves); child.moveStrLocal = TextIO.moveToString(currentPos, child.move, false, true, moves);
goForward(i, false); }
translateMovesHelper(); }
int nc = currentNode.children.size();
if (currChild < nc) {
goForward(currChild, false);
currPath.add(0);
} else {
currPath.remove(last);
last--;
if (last >= 0) {
currPath.set(last, currPath.get(last) + 1);
goBack(); goBack();
} }
} }
}
}
/** Export game tree in PGN format. */ /** Export game tree in PGN format. */
public final String toPGN(PGNOptions options) { public final String toPGN(PGNOptions options) {