DroidFish: Added function to add opening name to game headers.

This commit is contained in:
Peter Osterlund 2016-12-17 10:15:58 +01:00
parent 7dd20129ee
commit e030f68f7e
5 changed files with 64 additions and 20 deletions

View File

@ -107,6 +107,7 @@ you are not actively using the program.\
<string name="edit_game">Edit Game</string>
<string name="edit_headers">Edit Headers</string>
<string name="edit_comments">Edit Comments</string>
<string name="add_eco">Add opening name</string>
<string name="go_back">Go Back</string>
<string name="go_forward">Go Forward</string>
<string name="goto_start_game">Goto Start of Game</string>

View File

@ -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<CharSequence> lst = new ArrayList<CharSequence>();
@ -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;

View File

@ -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<GameTree.Node,Boolean> p = toCache.get(i);
distToEcoTree++;

View File

@ -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<String,String> headers = new TreeMap<String,String>();
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;

View File

@ -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<Integer> 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<String,String> headers) {
for (Entry<String, String> 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);
}
}
}