mirror of
https://github.com/peterosterlund2/droidfish.git
synced 2025-02-26 22:33:53 +01:00
DroidFish: Fixed wrong ECO classification after "e4 c5 h3 Nc6 g3".
This commit is contained in:
parent
e03550027d
commit
1b35f9590e
DroidFish/src/org/petero/droidfish/book
DroidFishTest/src/org/petero/droidfish/book
|
@ -52,10 +52,14 @@ public class EcoDb {
|
|||
public String getEco(GameTree gt, GameTree.Node node) {
|
||||
ArrayList<GameTree.Node> gtNodePath = new ArrayList<GameTree.Node>();
|
||||
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<Long, Short> posHashToNodeIdx;
|
||||
private WeakLRUCache<GameTree.Node, Integer> gtNodeToIdx;
|
||||
|
||||
private static class CacheEntry {
|
||||
final int nodeIdx;
|
||||
final boolean inEcoTree;
|
||||
CacheEntry(int n, boolean i) {
|
||||
nodeIdx = n;
|
||||
inEcoTree = i;
|
||||
}
|
||||
}
|
||||
private WeakLRUCache<GameTree.Node, CacheEntry> 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<Long, Short>();
|
||||
gtNodeToIdx = new WeakLRUCache<GameTree.Node, Integer>(50);
|
||||
gtNodeToIdx = new WeakLRUCache<GameTree.Node, CacheEntry>(50);
|
||||
try {
|
||||
ByteArrayOutputStream bufStream = new ByteArrayOutputStream();
|
||||
InputStream inStream = context.getAssets().open("eco.dat");
|
||||
|
|
142
DroidFishTest/src/org/petero/droidfish/book/EcoTest.java
Normal file
142
DroidFishTest/src/org/petero/droidfish/book/EcoTest.java
Normal file
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user