Skip to content

Commit

Permalink
Fixes bug in opening lib + auto start used engines on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
fathzer committed Feb 20, 2024
1 parent 313a2a8 commit 7195acf
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ Java 11+ to run the application and mvn to build it.
```java -Duci=true -jar ./target/jchess.jar```

# Known bugs
- Engine lists in player settings are not updated when variant changes.
- The displayed score is mutiplied per 100.
- The score + resign icon are not initialized at the same time as the board.
- The depth of internal engine is always set to 2

# TODO
- Allow engine settings to be defined in engines.json.
- Alert user when two external engines have the same name or add an interface to create engine.
- Implement a way to play again on missclick.
- Implement game loading and move backward/forward in the game.
- Use com.fathzer.jchess.gui.GameGUI interface or delete it.
- Make a more human friendly interface for settings
- Externalize perfT data (I think it's more than the half of the package size for a very limited usage).
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private String toFen(Board<Move> board) {

@Override
public Move apply(Board<Move> board) {
final JSONObject opening = db.getJSONObject(toFen(board));
final JSONObject opening = db.optJSONObject(toFen(board));
if (opening==null) {
return null;
}
Expand Down
52 changes: 45 additions & 7 deletions src/main/java/com/fathzer/jchess/swing/JChess.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.prefs.Preferences;

import javax.swing.AbstractAction;
Expand All @@ -15,8 +17,10 @@
import org.json.JSONObject;

import com.fathzer.jchess.bot.uci.EngineLoader;
import com.fathzer.jchess.bot.uci.EngineLoader.EngineData;
import com.fathzer.jchess.settings.Context;
import com.fathzer.jchess.settings.GameSettings;
import com.fathzer.jchess.settings.GameSettings.PlayerSettings;
import com.fathzer.jchess.swing.settings.SettingsDialog;
import com.fathzer.jchess.uci.JChessUCI;
import com.fathzer.soft.ajlib.swing.framework.Application;
Expand Down Expand Up @@ -85,13 +89,6 @@ public String getName() {

@Override
protected boolean onStart() {
this.game = new GameSession(panel.getGamePanel(), settings);
this.game.addListener((o,n) -> {
if (GameSession.State.ENDED.equals(n)) {
this.startAction.setEnabled(true);
this.panel.setMenuVisible(true);
}
});
try {
EngineLoader.init();
} catch (IOException e) {
Expand All @@ -100,9 +97,50 @@ protected boolean onStart() {
JOptionPane.ERROR_MESSAGE);
return result!=0;
}
fixSettings();
this.game = new GameSession(panel.getGamePanel(), settings);
this.game.addListener((o,n) -> {
if (GameSession.State.ENDED.equals(n)) {
this.startAction.setEnabled(true);
this.panel.setMenuVisible(true);
}
});
return true;
}

/** Fixes incompatibilities between settings and available engines and starts used engines.
*/
private void fixSettings() {
final List<EngineData> engines = EngineLoader.getEngines();
fixSettings(settings.getPlayer1(), engines);
fixSettings(settings.getPlayer2(), engines);
}

private void fixSettings(PlayerSettings player, List<EngineData> engines) {
final String engineName = player.getEngine()!=null ? player.getEngine().getName() : null;
if (engineName!=null && !engineName.isBlank()) {
final Optional<EngineData> engine = engines.stream().filter(e -> engineName.equals(e.getName())).findFirst();
boolean ok = false;
if (engine.isPresent()) {
// Ensure engine is started
if (engine.get().getEngine()!=null) {
ok = true;
} else {
try {
engine.get().start();
ok = true;
} catch (IOException e) {
// TODO Log the error?
}
}
}
if (!ok) {
// Engine does not exists or can't start, switch to human player
player.setEngine(null);
}
}
}

@Override
protected void saveState() {
super.saveState();
Expand Down

0 comments on commit 7195acf

Please sign in to comment.