Added engine info serialization and integration into MenuBar

This commit is contained in:
Kai S. K. Engelbart 2019-07-23 16:28:53 +02:00
parent 0a1ab4bab6
commit 5aa48edb93
Signed by: kske
GPG Key ID: 8BEB13EC5DF7EF13
5 changed files with 96 additions and 58 deletions

1
.gitignore vendored
View File

@ -22,3 +22,4 @@ local.properties
# Annotation Processing
.apt_generated/
/engine_infos.ser

View File

@ -144,11 +144,7 @@ public class Board implements Cloneable {
// Update the king's position if the moved piece is the king and castling
// availability
if (piece.getType() == Type.KING) {
kingPos.put(piece.getColor(), move.dest);
castlingRights.get(piece.getColor()).put(Type.KING, false);
castlingRights.get(piece.getColor()).put(Type.QUEEN, false);
}
if (piece.getType() == Type.KING) kingPos.put(piece.getColor(), move.dest);
// Update log
log.add(move, capturePiece);
@ -162,6 +158,9 @@ public class Board implements Cloneable {
// Increment halfmove clock
++halfmoveClock;
// Reset halfmove clock
if (piece.getType() == Type.PAWN || capturePiece != null) halfmoveClock = 0;
updateCastlingRights();
}

View File

@ -1,6 +1,11 @@
package dev.kske.chess.ui;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@ -17,8 +22,10 @@ public class EngineUtil {
private static volatile List<EngineInfo> engineInfos;
private static final String engineInfoFile = "engine_infos.ser";
static {
engineInfos = new ArrayList<>();
loadEngineInfos();
}
private EngineUtil() {}
@ -42,7 +49,8 @@ public class EngineUtil {
@Override
public void onUCIOk() {
engineInfos.add(info);
System.out.println("Engine: " + info.name);
handle.quit();
saveEngineInfos();
}
});
handle.start();
@ -51,11 +59,40 @@ public class EngineUtil {
}
}
public static class EngineInfo {
@SuppressWarnings("unchecked")
private static void loadEngineInfos() {
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(engineInfoFile))) {
Object obj = in.readObject();
if (obj instanceof ArrayList<?>) engineInfos = (ArrayList<EngineInfo>) obj;
else throw new IOException("Serialized object has the wrong class.");
} catch (ClassNotFoundException | IOException ex) {
engineInfos = new ArrayList<>();
}
}
private static void saveEngineInfos() {
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(engineInfoFile))) {
out.writeObject(engineInfos);
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static class EngineInfo implements Serializable {
private static final long serialVersionUID = -474177108900833005L;
public String path, name, author;
public EngineInfo(String path) {
this.path = path;
}
@Override
public String toString() {
return name + " by " + author + " at " + path;
}
}
public static List<EngineInfo> getEngineInfos() { return engineInfos; }
}

View File

@ -8,7 +8,7 @@ import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import dev.kske.chess.board.Log;
import dev.kske.chess.board.Move;
/**
* Project: <strong>Chess</strong><br>
@ -23,8 +23,6 @@ public class LogFrame extends JFrame {
private JPanel mcontentPane;
private JTable mtable;
private Log log;
/**
* Create the frame.
*/
@ -38,17 +36,12 @@ public class LogFrame extends JFrame {
setContentPane(mcontentPane);
mtable = new JTable();
mtable.setModel(new DefaultTableModel(new Object[][] {}, new String[] { "Black", "New column" }));
mtable.setModel(new DefaultTableModel(new Object[][] {}, new String[] { "White", "Black" }));
mtable.setEnabled(false);
mcontentPane.add(mtable, BorderLayout.CENTER);
}
public void update() {
public void add(Move move) {
}
public Log getLog() { return log; }
public void setLog(Log log) { this.log = log; }
}

View File

@ -6,6 +6,7 @@ import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import dev.kske.chess.game.Game;
import dev.kske.chess.ui.EngineUtil.EngineInfo;
/**
* Project: <strong>Chess</strong><br>
@ -49,10 +50,10 @@ public class MenuBar extends JMenuBar {
uciMenuItem.addActionListener((evt) -> {
String enginePath = JOptionPane.showInputDialog(getParent(),
"Enter the path to a UCI-compatible chess engine:", "Engine selection",
"Enter the path to a UCI-compatible chess engine:",
"Engine selection",
JOptionPane.QUESTION_MESSAGE);
if (enginePath != null)
startGame(Game.createUCI(boardPane, enginePath));
if (enginePath != null) startGame(Game.createUCI(boardPane, enginePath));
});
gameMenu.add(naturalMenuItem);
@ -70,17 +71,24 @@ public class MenuBar extends JMenuBar {
JMenu engineMenu = new JMenu("Engine");
JMenuItem addEngineMenuItem = new JMenuItem("Add engine");
addEngineMenuItem.addActionListener((evt) -> {
String enginePath = JOptionPane.showInputDialog(getParent(),
"Enter the path to a UCI-compatible chess engine:", "Engine selection",
"Enter the path to a UCI-compatible chess engine:",
"Engine selection",
JOptionPane.QUESTION_MESSAGE);
if(enginePath != null)
if (enginePath != null) {
EngineUtil.addEngine(enginePath);
// TODO: Rebuilt the engine menu
}
});
engineMenu.add(addEngineMenuItem);
for (EngineInfo info : EngineUtil.getEngineInfos()) {
JMenuItem engineMenuItem = new JMenuItem(info.name);
engineMenuItem.addActionListener((evt) -> startGame(Game.createUCI(boardPane, info.path)));
engineMenu.add(engineMenuItem);
}
add(engineMenu);
}