Skip to content

Commit

Permalink
Simplifies UCI code by using jchess-uci enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
fathzer committed Jan 6, 2024
1 parent f4fd392 commit 34a76d6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ public Move apply(Board<Move> board) {
return null;
}
final LibraryMove move = opening.getMoves().get(RND.nextInt(opening.getMoves().size()));
return JChessUCIEngine.toMove(board.getCoordinatesSystem(), UCIMove.from(move.getCoord()), board.getActiveColor());
return JChessUCIEngine.toMove(board, UCIMove.from(move.getCoord()));
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/fathzer/jchess/uci/JChessUCI.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
import com.fathzer.jchess.ai.evaluator.BasicEvaluator;
import com.fathzer.jchess.ai.evaluator.simple.SimpleEvaluator;
import com.fathzer.jchess.fen.FENUtils;
import com.fathzer.jchess.uci.extended.ExtendedUCI;
import com.fathzer.plugin.loader.jar.JarPluginLoader;
import com.fathzer.plugin.loader.utils.FileUtils;

public class JChessUCI extends UCI {
public class JChessUCI extends ExtendedUCI {
public static void main(String[] args) {
try (UCI uci = new JChessUCI()) {
final JarPluginLoader loader = new JarPluginLoader();
Expand Down
77 changes: 25 additions & 52 deletions src/main/java/com/fathzer/jchess/uci/JChessUCIEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
import java.util.Optional;
import java.util.function.Supplier;

import com.fathzer.games.Color;
import com.fathzer.games.MoveGenerator;
import com.fathzer.games.MoveGenerator.MoveConfidence;
import com.fathzer.games.ai.evaluation.Evaluator;
import com.fathzer.games.ai.time.BasicTimeManager;
import com.fathzer.games.perft.TestableMoveGeneratorBuilder;
import com.fathzer.games.util.PhysicalCores;
import com.fathzer.jchess.Board;
Expand All @@ -22,12 +20,16 @@
import com.fathzer.jchess.fen.FENUtils;
import com.fathzer.jchess.generic.BasicMove;
import com.fathzer.jchess.lichess.DefaultOpenings;
import com.fathzer.jchess.time.VuckovicSolakOracle;
import com.fathzer.jchess.uci.extended.Displayable;
import com.fathzer.jchess.uci.helper.AbstractEngine;
import com.fathzer.jchess.uci.option.ComboOption;
import com.fathzer.jchess.uci.option.Option;
import com.fathzer.jchess.uci.option.SpinOption;
import com.fathzer.jchess.uci.parameters.GoParameters;

public class JChessUCIEngine implements Engine, MoveGeneratorSupplier<Move>, TestableMoveGeneratorBuilder<Move, MoveGenerator<Move>>, MoveToUCIConverter<Move> {
public class JChessUCIEngine extends AbstractEngine<Move, Board<Move>> implements TestableMoveGeneratorBuilder<Move, Board<Move>>, Displayable {
private static final BasicTimeManager<Board<Move>> TIME_MANAGER = new BasicTimeManager<>(VuckovicSolakOracle.INSTANCE);

private static final int SILLY_LEVEL_DEPTH = 4;
private static final int AVERAGE_LEVEL_DEPTH = 6;
private static final int BEST_LEVEL_DEPTH = 14;
Expand All @@ -38,11 +40,11 @@ public class JChessUCIEngine implements Engine, MoveGeneratorSupplier<Move>, Tes
private static final String NAIVE_EVALUATOR = "naive";
private static final String SIMPLIFIED_EVALUATOR = "simplified";

private Board<Move> board;
private final JChessEngine engine;

public JChessUCIEngine() {
engine = new JChessEngine(SimpleEvaluator::new, AVERAGE_LEVEL_DEPTH);
super (new JChessEngine(SimpleEvaluator::new, AVERAGE_LEVEL_DEPTH), TIME_MANAGER);
engine = (JChessEngine) this.getEngine();
engine.setOpenings(DefaultOpenings.INSTANCE);
engine.getDeepeningPolicy().setDeepenOnForced(false);
}
Expand Down Expand Up @@ -103,15 +105,16 @@ public boolean isChess960Supported() {
}

@Override
public void move(UCIMove move) {
board.makeMove(toMove(board.getCoordinatesSystem(), move, board.getActiveColor()), MoveConfidence.UNSAFE);
protected Move toMove(UCIMove move) {
return toMove(board, move);
}

public static Move toMove(CoordinatesSystem cs, UCIMove move, Color color) {

public static Move toMove(Board<Move> board, UCIMove move) {
final CoordinatesSystem cs = board.getCoordinatesSystem();
final int from = cs.getIndex(move.getFrom());
final int to = cs.getIndex(move.getTo());
String promotion = move.getPromotion();
if (promotion!=null && Color.WHITE.equals(color)) {
if (promotion!=null && board.isWhiteToMove()) {
// Warning the promotion code is always in lowercase
promotion = promotion.toUpperCase();
}
Expand All @@ -130,56 +133,26 @@ private static Move toMove(int from, int to, String promotionAsFen) {
}
}
}

@Override
public void setStartPosition(String fen) {
board = FENUtils.from(fen);
board.setMoveComparatorBuilder(engine.getMoveComparatorSupplier());
}

@Override
public MoveGenerator<Move> fromFEN(String fen) {
return FENUtils.from(fen);
}

@Override
public LongRunningTask<BestMoveReply> go(GoParameters options) {
return new LongRunningTask<>() {
@Override
public BestMoveReply get() {
final UCIEngineSearchConfiguration c = new UCIEngineSearchConfiguration();
final UCIEngineSearchConfiguration.EngineConfiguration previous = c.configure(engine, options, board);
final Move move = engine.apply(board);
c.set(engine, previous);
return new BestMoveReply(toMove(board.getCoordinatesSystem(), move));
}

@Override
public void stop() {
super.stop();
engine.interrupt();
}
};
}

private static UCIMove toMove(CoordinatesSystem cs, Move move) {
private static UCIMove toUCIMove(CoordinatesSystem cs, Move move) {
final String promotion = move.getPromotion()==null ? null : move.getPromotion().getNotation().toLowerCase();
return new UCIMove(cs.getAlgebraicNotation(move.getFrom()), cs.getAlgebraicNotation(move.getTo()), promotion);
}

@Override
public MoveGenerator<Move> get() {
return board.fork();
}

@Override
public UCIMove toUCI(Move move) {
return toMove(board.getCoordinatesSystem(), move);
return toUCIMove(board.getCoordinatesSystem(), move);
}

@Override
public String getBoardAsString() {
return board==null ? "no position defined" : board.toString();
public void setStartPosition(String fen) {
board = FENUtils.from(fen);
board.setMoveComparatorBuilder(engine.getMoveComparatorSupplier());
}

@Override
public Board<Move> fromFEN(String fen) {
return FENUtils.from(fen);
}

@Override
Expand Down

This file was deleted.

0 comments on commit 34a76d6

Please sign in to comment.