DroidFish: Made the move variation up/down functions work also when the the current position is not a branch point in the game tree.

This commit is contained in:
Peter Osterlund 2014-10-04 12:21:53 +00:00
parent 8609a05d10
commit 5ed85f4360
3 changed files with 60 additions and 12 deletions

View File

@ -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);
}

View File

@ -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();
}

View File

@ -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) {