diff --git a/DroidFish/src/org/petero/droidfish/book/EcoDb.java b/DroidFish/src/org/petero/droidfish/book/EcoDb.java index 09a73e3..b4a66de 100644 --- a/DroidFish/src/org/petero/droidfish/book/EcoDb.java +++ b/DroidFish/src/org/petero/droidfish/book/EcoDb.java @@ -52,10 +52,14 @@ public class EcoDb { public String getEco(GameTree gt, GameTree.Node node) { ArrayList gtNodePath = new ArrayList(); int nodeIdx = -1; + boolean inEcoTree = true; while (node != null) { - nodeIdx = findNode(node); - if (nodeIdx != -1) + CacheEntry e = findNode(node); + if (e != null) { + nodeIdx = e.nodeIdx; + inEcoTree = e.inEcoTree; break; + } if (node == gt.rootNode) { Short idx = posHashToNodeIdx.get(gt.startPos.zobristHash()); if (idx != null) { @@ -68,11 +72,10 @@ public class EcoDb { } if (nodeIdx != -1) { Node ecoNode = readNode(nodeIdx); - boolean childFound = true; for (int i = gtNodePath.size() - 1; i >= 0; i--) { GameTree.Node gtNode = gtNodePath.get(i); int m = gtNode.move.getCompressedMove(); - int child = childFound ? ecoNode.firstChild : -1; + int child = inEcoTree ? ecoNode.firstChild : -1; while (child != -1) { Node cNode = readNode(child); if (cNode.move == m) @@ -83,8 +86,8 @@ public class EcoDb { nodeIdx = child; ecoNode = readNode(nodeIdx); } else - childFound = false; - cacheNode(gtNode, nodeIdx); + inEcoTree = false; + cacheNode(gtNode, nodeIdx, inEcoTree); } } @@ -107,23 +110,31 @@ public class EcoDb { private byte[] nodesBuffer; private String[] ecoNames; private HashMap posHashToNodeIdx; - private WeakLRUCache gtNodeToIdx; + + private static class CacheEntry { + final int nodeIdx; + final boolean inEcoTree; + CacheEntry(int n, boolean i) { + nodeIdx = n; + inEcoTree = i; + } + } + private WeakLRUCache gtNodeToIdx; /** Return cached Node index corresponding to a GameTree.Node, or -1 if not found. */ - private int findNode(GameTree.Node node) { - Integer idx = gtNodeToIdx.get(node); - return idx == null ? -1 : idx; + private CacheEntry findNode(GameTree.Node node) { + return gtNodeToIdx.get(node); } /** Store GameTree.Node to Node index in cache. */ - private void cacheNode(GameTree.Node node, int nodeIdx) { - gtNodeToIdx.put(node, nodeIdx); + private void cacheNode(GameTree.Node node, int nodeIdx, boolean inTree) { + gtNodeToIdx.put(node, new CacheEntry(nodeIdx, inTree)); } /** Constructor. */ private EcoDb(Context context) { posHashToNodeIdx = new HashMap(); - gtNodeToIdx = new WeakLRUCache(50); + gtNodeToIdx = new WeakLRUCache(50); try { ByteArrayOutputStream bufStream = new ByteArrayOutputStream(); InputStream inStream = context.getAssets().open("eco.dat"); diff --git a/DroidFishTest/src/org/petero/droidfish/book/EcoTest.java b/DroidFishTest/src/org/petero/droidfish/book/EcoTest.java new file mode 100644 index 0000000..07ef58f --- /dev/null +++ b/DroidFishTest/src/org/petero/droidfish/book/EcoTest.java @@ -0,0 +1,142 @@ +/* + DroidFish - An Android chess program. + Copyright (C) 2016 Peter Ă–sterlund, peterosterlund2@gmail.com + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package org.petero.droidfish.book; + +import org.petero.droidfish.PGNOptions; +import org.petero.droidfish.gamelogic.Game; +import org.petero.droidfish.gamelogic.GameTree; +import org.petero.droidfish.gamelogic.TimeControlData; + +import android.test.AndroidTestCase; + +/** Test of EcoDb class. */ +public class EcoTest extends AndroidTestCase { + + public EcoTest() { + } + + public void testEco() throws Throwable { + EcoDb ecoDb = EcoDb.getInstance(getContext()); + { + String pgn = "e4 e5 Nf3 Nc6 Bb5 a6 Ba4 Nf6 O-O Be7 Re1"; + GameTree gt = readPGN(pgn); + String eco = ecoDb.getEco(gt, gt.currentNode); + assertEquals("", eco); + + gt.goForward(0); + eco = ecoDb.getEco(gt, gt.currentNode); + assertEquals("B00: King's pawn opening", eco); + + gt.goForward(0); + eco = ecoDb.getEco(gt, gt.currentNode); + assertEquals("C20: King's pawn game", eco); + + gt.goForward(0); + eco = ecoDb.getEco(gt, gt.currentNode); + assertEquals("C40: King's knight opening", eco); + + gt.goForward(0); + eco = ecoDb.getEco(gt, gt.currentNode); + assertEquals("C44: King's pawn game", eco); + + gt.goForward(0); + eco = ecoDb.getEco(gt, gt.currentNode); + assertEquals("C60: Ruy Lopez (Spanish opening)", eco); + } + { + Game game = new Game(null, new TimeControlData()); + game.processString("e4"); + game.processString("e5"); + game.processString("Nf3"); + game.processString("Nf6"); + String eco = ecoDb.getEco(game.tree, game.tree.currentNode); + assertEquals("C42: Petrov's defence", eco); + + game.processString("Nxe5"); + eco = ecoDb.getEco(game.tree, game.tree.currentNode); + assertEquals("C42: Petrov's defence", eco); + + game.processString("d6"); + eco = ecoDb.getEco(game.tree, game.tree.currentNode); + assertEquals("C42: Petrov's defence", eco); + + game.processString("Nxf7"); + eco = ecoDb.getEco(game.tree, game.tree.currentNode); + assertEquals("C42: Petrov, Cochrane gambit", eco); + + game.undoMove(); + eco = ecoDb.getEco(game.tree, game.tree.currentNode); + assertEquals("C42: Petrov's defence", eco); + + game.processString("Nf3"); + game.processString("Nxe4"); + game.processString("d4"); + eco = ecoDb.getEco(game.tree, game.tree.currentNode); + assertEquals("C42: Petrov, classical attack", eco); + } + { + Game game = new Game(null, new TimeControlData()); + game.processString("e4"); + game.processString("c5"); + String eco = ecoDb.getEco(game.tree, game.tree.currentNode); + assertEquals("B20: Sicilian defence", eco); + + game.processString("h3"); + eco = ecoDb.getEco(game.tree, game.tree.currentNode); + assertEquals("B20: Sicilian defence", eco); + + game.processString("Nc6"); + eco = ecoDb.getEco(game.tree, game.tree.currentNode); + assertEquals("B20: Sicilian defence", eco); + + game.processString("g3"); + eco = ecoDb.getEco(game.tree, game.tree.currentNode); + assertEquals("B20: Sicilian defence", eco); + } + } + + public void testEcoFromFEN() throws Throwable { + EcoDb ecoDb = EcoDb.getInstance(getContext()); + GameTree gt = gtFromFEN("rnbqkbnr/ppp2ppp/8/3p4/3P4/8/PPP2PPP/RNBQKBNR w KQkq - 0 4"); + String eco = ecoDb.getEco(gt, gt.currentNode); + assertEquals("C01: French, exchange variation", eco); + + gt = gtFromFEN("rnbqk1nr/ppppppbp/6p1/8/3PP3/8/PPP2PPP/RNBQKBNR w KQkq - 1 3"); + eco = ecoDb.getEco(gt, gt.currentNode); + assertEquals("B06: Robatsch (modern) defence", eco); + } + + /** Create GameTree from PGN. */ + private GameTree readPGN(String pgn) throws Throwable { + GameTree gt = new GameTree(null); + PGNOptions options = new PGNOptions(); + options.imp.variations = true; + options.imp.comments = true; + options.imp.nag = true; + boolean res = gt.readPGN(pgn, options); + assertEquals(true, res); + return gt; + } + + /** Create a GameTree starting from a FEN position, and containing no moves. */ + private GameTree gtFromFEN(String fen) throws Throwable { + String pgn = String.format("[FEN \"%s\"][SetUp \"1\"", fen); + return readPGN(pgn); + } +}