From 1f01daf133ae89e69e3ec8d5d2043ca7e831d3dc Mon Sep 17 00:00:00 2001 From: Peter Osterlund Date: Sun, 26 Aug 2012 13:34:54 +0000 Subject: [PATCH] DroidFish: Improved "one touch move" behavior. Based on patch from Leo Mayer. --- DroidFish/res/values/strings.xml | 4 +- DroidFish/res/xml/preferences.xml | 4 +- .../src/org/petero/droidfish/ChessBoard.java | 110 ++++++++++++------ .../src/org/petero/droidfish/DroidFish.java | 2 + .../droidfish/activities/EditBoard.java | 1 + 5 files changed, 81 insertions(+), 40 deletions(-) diff --git a/DroidFish/res/values/strings.xml b/DroidFish/res/values/strings.xml index 1dda1de..63e8aec 100644 --- a/DroidFish/res/values/strings.xml +++ b/DroidFish/res/values/strings.xml @@ -249,8 +249,8 @@ you are not actively using the program.\ User Interface Animate Moves Animate piece movements - One Touch Moves - Touching a square that corresponds to one unique move makes that move immediately + Quick Move Input + From and To squares can be touched in any order. Move is played as soon as uniquely defined. Enable Sounds Play sound when computer makes a move Enable Vibration diff --git a/DroidFish/res/xml/preferences.xml b/DroidFish/res/xml/preferences.xml index a157c9b..fdd64fd 100644 --- a/DroidFish/res/xml/preferences.xml +++ b/DroidFish/res/xml/preferences.xml @@ -125,8 +125,8 @@ moves = new MoveGen().legalMoves(pos); - Move matchingMove = null; - int toSq = -1; - for (Move m : moves) { - if ((m.from == sq) || (m.to == sq)) { - if (matchingMove == null) { - matchingMove = m; - toSq = m.to; - } else { - matchingMove = null; - break; - } - } - } - if (matchingMove != null) { - setSelection(toSq); - return matchingMove; - } + int prevSq = userSelectedSquare ? selectedSquare : -1; + if (prevSq == sq) + return null; + ArrayList moves = new MoveGen().legalMoves(pos); + Move matchingMove = null; + if (prevSq >= 0) + matchingMove = matchingMove(prevSq, sq, moves).first; + boolean anyMatch = false; + if (matchingMove == null) { + Pair match = matchingMove(-1, sq, moves); + matchingMove = match.first; + anyMatch = match.second; } - if (myColor(p)) { - setSelection(sq); + if (matchingMove != null) { + setSelection(matchingMove.to); + userSelectedSquare = false; + return matchingMove; } + setSelection(anyMatch ? sq : -1); } return null; } + /** + * Determine if there is a unique legal move corresponding to one or two selected squares. + * @param sq1 First square, or -1. + * @param sq2 Second square. + * @param moves List of legal moves. + * @return Matching move if unique. + * Boolean indicating if there was at least one match. + */ + private final Pair matchingMove(int sq1, int sq2, ArrayList moves) { + Move matchingMove = null; + boolean anyMatch = false; + for (Move m : moves) { + boolean match; + if (sq1 == -1) + match = (m.from == sq2) || (m.to == sq2); + else + match = (m.from == sq1) && (m.to == sq2) || + (m.from == sq2) && (m.to == sq1); + if (match) { + if (matchingMove == null) { + matchingMove = m; + anyMatch = true; + } else { + if ((matchingMove.from == m.from) && + (matchingMove.to == m.to)) { + matchingMove.promoteTo = Piece.EMPTY; + } else { + matchingMove = null; + break; + } + } + } + } + return new Pair(matchingMove, anyMatch); + } + public static class OnTrackballListener { public void onTrackballEvent(MotionEvent event) { } } diff --git a/DroidFish/src/org/petero/droidfish/DroidFish.java b/DroidFish/src/org/petero/droidfish/DroidFish.java index 9e1eff2..3bd2a0f 100644 --- a/DroidFish/src/org/petero/droidfish/DroidFish.java +++ b/DroidFish/src/org/petero/droidfish/DroidFish.java @@ -476,6 +476,7 @@ public class DroidFish extends Activity implements GUIInterface { cb.setDrawSquareLabels(oldCB.drawSquareLabels); cb.oneTouchMoves = oldCB.oneTouchMoves; setSelection(oldCB.selectedSquare); + cb.userSelectedSquare = oldCB.userSelectedSquare; setStatusString(statusStr); moveListUpdated(); updateThinkingInfo(); @@ -1175,6 +1176,7 @@ public class DroidFish extends Activity implements GUIInterface { @Override public void setSelection(int sq) { cb.setSelection(sq); + cb.userSelectedSquare = false; setEgtbHints(sq); } diff --git a/DroidFish/src/org/petero/droidfish/activities/EditBoard.java b/DroidFish/src/org/petero/droidfish/activities/EditBoard.java index c27cfad..a69183c 100644 --- a/DroidFish/src/org/petero/droidfish/activities/EditBoard.java +++ b/DroidFish/src/org/petero/droidfish/activities/EditBoard.java @@ -91,6 +91,7 @@ public class EditBoard extends Activity { cb.cursorVisible = oldCB.cursorVisible; cb.setPosition(oldCB.pos); setSelection(oldCB.selectedSquare); + cb.userSelectedSquare = oldCB.userSelectedSquare; status.setText(statusStr); }