From e030f68f7eb0f72a0ca3c817d5c0407c4ff63ade Mon Sep 17 00:00:00 2001 From: Peter Osterlund Date: Sat, 17 Dec 2016 10:15:58 +0100 Subject: [PATCH] DroidFish: Added function to add opening name to game headers. --- DroidFish/res/values/strings.xml | 1 + .../src/org/petero/droidfish/DroidFish.java | 14 +++-- .../src/org/petero/droidfish/book/EcoDb.java | 4 +- .../gamelogic/DroidChessController.java | 13 +++++ .../petero/droidfish/gamelogic/GameTree.java | 52 ++++++++++++++----- 5 files changed, 64 insertions(+), 20 deletions(-) diff --git a/DroidFish/res/values/strings.xml b/DroidFish/res/values/strings.xml index 9d14728..6461453 100644 --- a/DroidFish/res/values/strings.xml +++ b/DroidFish/res/values/strings.xml @@ -107,6 +107,7 @@ you are not actively using the program.\ Edit Game Edit Headers Edit Comments + Add opening name Go Back Go Forward Goto Start of Game diff --git a/DroidFish/src/org/petero/droidfish/DroidFish.java b/DroidFish/src/org/petero/droidfish/DroidFish.java index 09e6216..c82198b 100644 --- a/DroidFish/src/org/petero/droidfish/DroidFish.java +++ b/DroidFish/src/org/petero/droidfish/DroidFish.java @@ -112,7 +112,6 @@ import android.os.Handler; import android.os.Vibrator; import android.preference.PreferenceManager; import android.support.v4.app.ActivityCompat; -import android.support.v4.app.ShareCompat; import android.support.v4.content.ContextCompat; import android.support.v4.content.FileProvider; import android.support.v4.view.MotionEventCompat; @@ -2752,10 +2751,11 @@ public class DroidFish extends Activity private final Dialog moveListMenuDialog() { final int EDIT_HEADERS = 0; final int EDIT_COMMENTS = 1; - final int REMOVE_SUBTREE = 2; - final int MOVE_VAR_UP = 3; - final int MOVE_VAR_DOWN = 4; - final int ADD_NULL_MOVE = 5; + final int ADD_ECO = 2; + final int REMOVE_SUBTREE = 3; + final int MOVE_VAR_UP = 4; + final int MOVE_VAR_DOWN = 5; + final int ADD_NULL_MOVE = 6; setAutoMode(AutoMode.OFF); List lst = new ArrayList(); @@ -2764,6 +2764,7 @@ public class DroidFish extends Activity if (ctrl.humansTurn()) { lst.add(getString(R.string.edit_comments)); actions.add(EDIT_COMMENTS); } + lst.add(getString(R.string.add_eco)); actions.add(ADD_ECO); lst.add(getString(R.string.truncate_gametree)); actions.add(REMOVE_SUBTREE); if (ctrl.canMoveVariationUp()) { lst.add(getString(R.string.move_var_up)); actions.add(MOVE_VAR_UP); @@ -2867,6 +2868,9 @@ public class DroidFish extends Activity builder.show(); break; } + case ADD_ECO: + ctrl.addECO(); + break; case REMOVE_SUBTREE: ctrl.removeSubTree(); break; diff --git a/DroidFish/src/org/petero/droidfish/book/EcoDb.java b/DroidFish/src/org/petero/droidfish/book/EcoDb.java index 5906686..ee3c078 100644 --- a/DroidFish/src/org/petero/droidfish/book/EcoDb.java +++ b/DroidFish/src/org/petero/droidfish/book/EcoDb.java @@ -122,7 +122,7 @@ public class EcoDb { int currEcoNode = 0; boolean foundDup = false; while (!treePath.isEmpty()) { - gt.goForward(treePath.get(treePath.size() - 1)); + gt.goForward(treePath.get(treePath.size() - 1), false); treePath.remove(treePath.size() - 1); int m = gt.currentNode.move.getCompressedMove(); @@ -154,7 +154,7 @@ public class EcoDb { } for (int i = treePath.size() - 1; i >= 0; i--) - gt.goForward(treePath.get(i)); + gt.goForward(treePath.get(i), false); for (int i = toCache.size() - 1; i >= 0; i--) { Pair p = toCache.get(i); distToEcoTree++; diff --git a/DroidFish/src/org/petero/droidfish/gamelogic/DroidChessController.java b/DroidFish/src/org/petero/droidfish/gamelogic/DroidChessController.java index 79eaa77..f0a11a4 100644 --- a/DroidFish/src/org/petero/droidfish/gamelogic/DroidChessController.java +++ b/DroidFish/src/org/petero/droidfish/gamelogic/DroidChessController.java @@ -27,6 +27,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.TreeMap; import org.petero.droidfish.EngineOptions; import org.petero.droidfish.GUIInterface; @@ -630,6 +631,18 @@ public class DroidChessController { updateGUI(); } + /** Add ECO classification headers. */ + public final synchronized void addECO() { + EcoDb.Result r = game.tree.getGameECO(); + Map headers = new TreeMap(); + headers.put("ECO", r.eco.isEmpty() ? null : r.eco); + headers.put("Opening", r.opn.isEmpty() ? null : r.opn); + headers.put("Variation", r.var.isEmpty() ? null : r.var); + game.tree.setHeaders(headers); + gameTextListener.clear(); + updateGUI(); + } + /** Comments associated with a move. */ public static final class CommentInfo { public String move; diff --git a/DroidFish/src/org/petero/droidfish/gamelogic/GameTree.java b/DroidFish/src/org/petero/droidfish/gamelogic/GameTree.java index 25527d9..35d8cf7 100644 --- a/DroidFish/src/org/petero/droidfish/gamelogic/GameTree.java +++ b/DroidFish/src/org/petero/droidfish/gamelogic/GameTree.java @@ -31,6 +31,7 @@ import java.util.Map; import java.util.Map.Entry; import org.petero.droidfish.PGNOptions; +import org.petero.droidfish.book.EcoDb; import org.petero.droidfish.gamelogic.Game.GameState; import org.petero.droidfish.gamelogic.TimeControlData.TimeControlField; @@ -272,6 +273,21 @@ public class GameTree { return pgnText.getPgnString(); } + /** Get ECO classification corresponding to the end of mainline. */ + public final EcoDb.Result getGameECO() { + ArrayList currPath = currentNode.getPathFromRoot(); + while (currentNode != rootNode) + goBack(); + while (variations().size() > 0) + goForward(0, false); + EcoDb.Result ecoData = EcoDb.getInstance().getEco(this); + while (currentNode != rootNode) + goBack(); + for (int i : currPath) + goForward(i, false); + return ecoData; + } + /** Walks the game tree in PGN export order. */ public final void pgnTreeWalker(PGNOptions options, PgnToken.PgnTokenReceiver out) { // Go to end of mainline to evaluate PGN result string. @@ -1480,7 +1496,8 @@ public class GameTree { } } - /** Set PGN header tags and values. */ + /** Set PGN header tags and values. Setting a non-required + * tag to null causes it to be removed. */ void setHeaders(Map headers) { for (Entry entry : headers.entrySet()) { String tag = entry.getKey(); @@ -1492,19 +1509,28 @@ public class GameTree { else if (tag.equals("White")) white = val; else if (tag.equals("Black")) black = val; else { - boolean found = false; - for (TagPair t : tagPairs) { - if (t.tagName.equals(tag)) { - t.tagValue = val; - found = true; - break; + if (val != null) { + boolean found = false; + for (TagPair t : tagPairs) { + if (t.tagName.equals(tag)) { + t.tagValue = val; + found = true; + break; + } + } + if (!found) { + TagPair tp = new TagPair(); + tp.tagName = tag; + tp.tagValue = val; + tagPairs.add(tp); + } + } else { + for (int i = 0; i < tagPairs.size(); i++) { + if (tagPairs.get(i).tagName.equals(tag)) { + tagPairs.remove(i); + break; + } } - } - if (!found) { - TagPair tp = new TagPair(); - tp.tagName = tag; - tp.tagValue = val; - tagPairs.add(tp); } } }