Working on external engine integration, added extra menu

+ EngineUtil for storing engine information
- Changed all UCIListener methods to default
This commit is contained in:
delvh 2019-07-23 11:54:43 +02:00
parent 490db60d6b
commit 85be1a182b
3 changed files with 94 additions and 15 deletions

View File

@ -15,31 +15,31 @@ public interface UCIListener {
* *
* @param name The name of the engine * @param name The name of the engine
*/ */
void onIdName(String name); default void onIdName(String name) {}
/** /**
* Identifies the author of the engine. * Identifies the author of the engine.
* *
* @param author The name of the engine's author * @param author The name of the engine's author
*/ */
void onIdAuthor(String author); default void onIdAuthor(String author) {}
/** /**
* The engine is ready in UCI mode. * The engine is ready in UCI mode.
*/ */
void onUCIOk(); default void onUCIOk() {}
/** /**
* The engine has processed all inputs and is ready for new commands. * The engine has processed all inputs and is ready for new commands.
*/ */
void onReadyOk(); default void onReadyOk() {}
/** /**
* The engine has stopped searching and has found the best move. * The engine has stopped searching and has found the best move.
* *
* @param move The best moves the engine has found * @param move The best moves the engine has found
*/ */
void onBestMove(String move); default void onBestMove(String move) {}
/** /**
* The engine has stopped searching and has found the best move. * The engine has stopped searching and has found the best move.
@ -47,49 +47,48 @@ public interface UCIListener {
* @param move The best move the engine has found * @param move The best move the engine has found
* @param ponderMove The move the engine likes to ponder on * @param ponderMove The move the engine likes to ponder on
*/ */
void onBestMove(String move, String ponderMove); default void onBestMove(String move, String ponderMove) {}
/** /**
* The engine will check the copy protection now. * The engine will check the copy protection now.
*/ */
void onCopyProtectionChecking(); default void onCopyProtectionChecking() {}
/** /**
* The engine has successfully checked the copy protection. * The engine has successfully checked the copy protection.
*/ */
void onCopyProtectionOk(); default void onCopyProtectionOk() {}
/** /**
* The engine has encountered an error during copy protection checking. * The engine has encountered an error during copy protection checking.
*/ */
void onCopyProtectionError(); default void onCopyProtectionError() {}
/** /**
* The engine will check the registration now. * The engine will check the registration now.
*/ */
void onRegistrationChecking(); default void onRegistrationChecking() {}
/** /**
* The engine has successfully checked the registration. * The engine has successfully checked the registration.
*/ */
void onRegistrationOk(); default void onRegistrationOk() {}
/** /**
* The engine has encountered an error during registration checking. * The engine has encountered an error during registration checking.
*/ */
void onRegistrationError(); default void onRegistrationError() {}
/** /**
* The engine sends information to the GUI. * The engine sends information to the GUI.
* *
* @param additionalInfo Contains all pieces of information to be sent * @param additionalInfo Contains all pieces of information to be sent
*/ */
void onInfo(Map<String, String> additionalInfo); default void onInfo(Map<String, String> additionalInfo) {}
/** /**
* Tells the GUI which parameters can be changed in the engine. * Tells the GUI which parameters can be changed in the engine.
* *
* @param option Option object describing the parameter * @param option Option object describing the parameter
*/ */
void onOption(UCIOption option); default void onOption(UCIOption option) {}
} }

View File

@ -0,0 +1,61 @@
package dev.kske.chess.ui;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import dev.kske.chess.uci.UCIHandle;
import dev.kske.chess.uci.UCIListener;
/**
* Project: <strong>Chess</strong><br>
* File: <strong>MenuBar.java</strong><br>
* Created: <strong>23.07.2019</strong><br>
* Author: <strong>Leon Hofmeister</strong>
*/
public class EngineUtil {
private static volatile List<EngineInfo> engineInfos;
static {
engineInfos = new ArrayList<>();
}
private EngineUtil() {}
public static void addEngine(String enginePath) {
try {
EngineInfo info = new EngineInfo(enginePath);
UCIHandle handle = new UCIHandle(enginePath);
handle.setListener(new UCIListener() {
@Override
public void onIdName(String name) {
info.name = name;
}
@Override
public void onIdAuthor(String author) {
info.author = author;
}
@Override
public void onUCIOk() {
engineInfos.add(info);
System.out.println("Engine: " + info.name);
}
});
handle.start();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static class EngineInfo {
public String path, name, author;
public EngineInfo(String path) {
this.path = path;
}
}
}

View File

@ -25,6 +25,7 @@ public class MenuBar extends JMenuBar {
boardPane = mainWindow.getBoardPane(); boardPane = mainWindow.getBoardPane();
initGameMenu(); initGameMenu();
initEngineMenu();
} }
private void initGameMenu() { private void initGameMenu() {
@ -65,6 +66,24 @@ public class MenuBar extends JMenuBar {
naturalMenuItem.doClick(); naturalMenuItem.doClick();
} }
private void initEngineMenu() {
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",
JOptionPane.QUESTION_MESSAGE);
if(enginePath != null)
EngineUtil.addEngine(enginePath);
});
engineMenu.add(addEngineMenuItem);
add(engineMenu);
}
private void startGame(Game game) { private void startGame(Game game) {
mainWindow.setGame(game); mainWindow.setGame(game);