diff --git a/src/dev/kske/chess/uci/UCIListener.java b/src/dev/kske/chess/uci/UCIListener.java index e003dcf..6bb84ed 100644 --- a/src/dev/kske/chess/uci/UCIListener.java +++ b/src/dev/kske/chess/uci/UCIListener.java @@ -15,31 +15,31 @@ public interface UCIListener { * * @param name The name of the engine */ - void onIdName(String name); + default void onIdName(String name) {} /** * Identifies the author of the engine. * * @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. */ - void onUCIOk(); + default void onUCIOk() {} /** * 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. * * @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. @@ -47,49 +47,48 @@ public interface UCIListener { * @param move The best move the engine has found * @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. */ - void onCopyProtectionChecking(); - + default void onCopyProtectionChecking() {} /** * The engine has successfully checked the copy protection. */ - void onCopyProtectionOk(); + default void onCopyProtectionOk() {} /** * The engine has encountered an error during copy protection checking. */ - void onCopyProtectionError(); + default void onCopyProtectionError() {} /** * The engine will check the registration now. */ - void onRegistrationChecking(); + default void onRegistrationChecking() {} /** * The engine has successfully checked the registration. */ - void onRegistrationOk(); + default void onRegistrationOk() {} /** * The engine has encountered an error during registration checking. */ - void onRegistrationError(); + default void onRegistrationError() {} /** * The engine sends information to the GUI. * * @param additionalInfo Contains all pieces of information to be sent */ - void onInfo(Map additionalInfo); + default void onInfo(Map additionalInfo) {} /** * Tells the GUI which parameters can be changed in the engine. * * @param option Option object describing the parameter */ - void onOption(UCIOption option); + default void onOption(UCIOption option) {} } diff --git a/src/dev/kske/chess/ui/EngineUtil.java b/src/dev/kske/chess/ui/EngineUtil.java new file mode 100644 index 0000000..41b8ef8 --- /dev/null +++ b/src/dev/kske/chess/ui/EngineUtil.java @@ -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: Chess
+ * File: MenuBar.java
+ * Created: 23.07.2019
+ * Author: Leon Hofmeister + */ +public class EngineUtil { + + private static volatile List 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; + } + } +} diff --git a/src/dev/kske/chess/ui/MenuBar.java b/src/dev/kske/chess/ui/MenuBar.java index d4c9564..4be983d 100644 --- a/src/dev/kske/chess/ui/MenuBar.java +++ b/src/dev/kske/chess/ui/MenuBar.java @@ -25,6 +25,7 @@ public class MenuBar extends JMenuBar { boardPane = mainWindow.getBoardPane(); initGameMenu(); + initEngineMenu(); } private void initGameMenu() { @@ -65,6 +66,24 @@ public class MenuBar extends JMenuBar { 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) { mainWindow.setGame(game);