From 5ed85f43602dcac1c3e09e531746bfc69f50ee85 Mon Sep 17 00:00:00 2001 From: Peter Osterlund Date: Sat, 4 Oct 2014 12:21:53 +0000 Subject: [PATCH] DroidFish: Made the move variation up/down functions work also when the the current position is not a branch point in the game tree. --- .../src/org/petero/droidfish/DroidFish.java | 4 +- .../gamelogic/DroidChessController.java | 13 ++++- .../org/petero/droidfish/gamelogic/Game.java | 55 +++++++++++++++---- 3 files changed, 60 insertions(+), 12 deletions(-) diff --git a/DroidFish/src/org/petero/droidfish/DroidFish.java b/DroidFish/src/org/petero/droidfish/DroidFish.java index 3ad29f1..dcfdfb5 100644 --- a/DroidFish/src/org/petero/droidfish/DroidFish.java +++ b/DroidFish/src/org/petero/droidfish/DroidFish.java @@ -2449,8 +2449,10 @@ public class DroidFish extends Activity implements GUIInterface { lst.add(getString(R.string.edit_comments)); actions.add(EDIT_COMMENTS); } lst.add(getString(R.string.truncate_gametree)); actions.add(REMOVE_SUBTREE); - if (ctrl.numVariations() > 1) { + if (ctrl.canMoveVariationUp()) { lst.add(getString(R.string.move_var_up)); actions.add(MOVE_VAR_UP); + } + if (ctrl.canMoveVariationDown()) { lst.add(getString(R.string.move_var_down)); actions.add(MOVE_VAR_DOWN); } diff --git a/DroidFish/src/org/petero/droidfish/gamelogic/DroidChessController.java b/DroidFish/src/org/petero/droidfish/gamelogic/DroidChessController.java index ea838f0..cadd103 100644 --- a/DroidFish/src/org/petero/droidfish/gamelogic/DroidChessController.java +++ b/DroidFish/src/org/petero/droidfish/gamelogic/DroidChessController.java @@ -477,6 +477,16 @@ public class DroidChessController { return game.numVariations(); } + /** Return true if the current variation can be moved closer to the main-line. */ + public final synchronized boolean canMoveVariationUp() { + return game.canMoveVariation(-1); + } + + /** Return true if the current variation can be moved farther away from the main-line. */ + public final synchronized boolean canMoveVariationDown() { + return game.canMoveVariation(1); + } + /** Get current variation in current position. */ public final synchronized int currVariation() { return game.currVariation(); @@ -504,7 +514,8 @@ public class DroidChessController { /** Move current variation up/down in the game tree. */ public final synchronized void moveVariation(int delta) { - if (game.numVariations() > 1) { + if (((delta > 0) && canMoveVariationDown()) || + ((delta < 0) && canMoveVariationUp())) { game.moveVariation(delta); updateGUI(); } diff --git a/DroidFish/src/org/petero/droidfish/gamelogic/Game.java b/DroidFish/src/org/petero/droidfish/gamelogic/Game.java index 67b6594..cc5a59a 100644 --- a/DroidFish/src/org/petero/droidfish/gamelogic/Game.java +++ b/DroidFish/src/org/petero/droidfish/gamelogic/Game.java @@ -289,20 +289,55 @@ public class Game { /** Move current variation up/down in the game tree. */ public final void moveVariation(int delta) { - if (tree.currentNode == tree.rootNode) - return; - tree.goBack(); - int varNo = tree.currentNode.defaultChild; - int nChildren = tree.variations().size(); - int newPos = varNo + delta; - newPos = Math.max(newPos, 0); - newPos = Math.min(newPos, nChildren - 1); - tree.reorderVariation(varNo, newPos); - tree.goForward(newPos); + int nBack = 0; + boolean found = false; + while (tree.currentNode != tree.rootNode) { + tree.goBack(); + nBack++; + if (((delta < 0) && tree.currentNode.defaultChild > 0) || + ((delta > 0) && tree.currentNode.defaultChild < tree.variations().size() - 1)) { + found = true; + break; + } + } + if (found) { + int varNo = tree.currentNode.defaultChild; + int nChildren = tree.variations().size(); + int newPos = varNo + delta; + newPos = Math.max(newPos, 0); + newPos = Math.min(newPos, nChildren - 1); + tree.reorderVariation(varNo, newPos); + tree.goForward(newPos); + nBack--; + } + while (nBack > 0) { + tree.goForward(-1); + nBack--; + } pendingDrawOffer = false; updateTimeControl(true); } + /** Return true if the current variation can be moved up/down. */ + public final boolean canMoveVariation(int delta) { + int nBack = 0; + boolean found = false; + while (tree.currentNode != tree.rootNode) { + tree.goBack(); + nBack++; + if (((delta < 0) && tree.currentNode.defaultChild > 0) || + ((delta > 0) && tree.currentNode.defaultChild < tree.variations().size() - 1)) { + found = true; + break; + } + } + while (nBack > 0) { + tree.goForward(-1); + nBack--; + } + return found; + } + /** Delete whole game sub-tree rooted at current position. */ public final void removeSubTree() { if (getLastMove() != null) {