Skip to content

Commit

Permalink
New opening library strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
fathzer committed Mar 12, 2024
1 parent 5aa31fd commit ec029a7
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.fathzer.jchess.lichess;

import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.zip.GZIPInputStream;

import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

import com.fathzer.games.movelibrary.AbstractMoveLibrary;
import com.fathzer.games.MoveGenerator;

public abstract class AbstractDefaultOpenings<M, B extends MoveGenerator<M>> extends AbstractMoveLibrary<JSONObject, M, B> {
//TODO Commonalize with chesslib-uci-engine (probably in an external lichess related library
private final JSONObject db;

protected AbstractDefaultOpenings(Supplier<InputStream> stream, boolean zipped) throws IOException {
db = readJSON(stream, zipped);
this.setMoveSelector(weightedMoveSelector());
}

private JSONObject readJSON(Supplier<InputStream> stream, boolean zipped) throws IOException {
try (InputStream in = zipped ? new GZIPInputStream(stream.get()) : stream.get()) {
return new JSONObject(new JSONTokener(in));
}
}

private String toReducedXFen(B board) {
final String fen = toXFEN(board);
int index = fen.lastIndexOf(' ', fen.lastIndexOf(' ')-1);
return fen.substring(0, index);
}

@Override
protected Optional<List<JSONObject>> getRecord(B board) {
final String fen = toReducedXFen(board);
final JSONObject theRecord = db.optJSONObject(fen);
final JSONArray moves = theRecord==null ? null : theRecord.optJSONArray("moves");
if (moves==null) {
return Optional.empty();
}
final List<JSONObject> result = new LinkedList<>();
for (int i=0;i<moves.length();i++) {
result.add(moves.getJSONObject(i));
}
return Optional.of(result);
}

@Override
protected M toMove(B board, JSONObject move) {
return fromUCI(board, getCoord(move));
}

@Override
protected long getWeight(JSONObject move) {
return move.getLong("count");
}

protected String getCoord(JSONObject move) {
return move.getString("coord");
}

protected abstract String toXFEN(B board);
protected abstract M fromUCI(B board, String move);
}
39 changes: 8 additions & 31 deletions src/main/java/com/fathzer/jchess/lichess/DefaultOpenings.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,16 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.util.Random;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.zip.GZIPInputStream;

import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

import com.fathzer.jchess.Board;
import com.fathzer.jchess.Move;
import com.fathzer.jchess.fen.FENUtils;
import com.fathzer.jchess.uci.JChessUCIEngine;
import com.fathzer.jchess.uci.UCIMove;

public class DefaultOpenings implements Function<Board<Move>, Move> {
private static final Random RND = new Random();
private static final String KNOWN = "/lichess/masters-shrink-full.json.gz";
public class DefaultOpenings extends AbstractDefaultOpenings<Move, Board<Move>> {
private static final String KNOWN = "/lichess/masters-shrink.json.gz";

public static final DefaultOpenings INSTANCE;

Expand All @@ -32,36 +24,21 @@ public class DefaultOpenings implements Function<Board<Move>, Move> {
}
}

private final JSONObject db;

private DefaultOpenings() throws IOException {
this(() -> DefaultOpenings.class.getResourceAsStream(KNOWN), true);
}

public DefaultOpenings(Supplier<InputStream> stream, boolean zipped) throws IOException {
db = readJSON(stream, zipped);
}

private JSONObject readJSON(Supplier<InputStream> stream, boolean zipped) throws IOException {
try (InputStream in = zipped ? new GZIPInputStream(stream.get()) : stream.get()) {
return new JSONObject(new JSONTokener(in));
}
super(stream, zipped);
}

private String toFen(Board<Move> board) {
String fen = FENUtils.to(board);
int index = fen.lastIndexOf(' ', fen.lastIndexOf(' ')-1);
return fen.substring(0, index);
@Override
protected Move fromUCI(Board<Move> board, String move) {
return JChessUCIEngine.toMove(board, UCIMove.from(move));
}

@Override
public Move apply(Board<Move> board) {
final String fen = toFen(board);
final JSONArray moves = db.optJSONArray(fen);
if (moves==null) {
return null;
}
final String move = moves.getString(RND.nextInt(moves.length()));
return JChessUCIEngine.toMove(board, UCIMove.from(move));
protected String toXFEN(Board<Move> board) {
return FENUtils.to(board);
}
}
Binary file not shown.
Binary file not shown.

0 comments on commit ec029a7

Please sign in to comment.