From 347eb5d531be4fc6a0b0ce08593a9a5773fbc15e Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sat, 20 Jul 2019 06:36:56 +0200 Subject: [PATCH] Added UCIListener, started working on an implementation --- src/dev/kske/chess/game/UCIPlayer.java | 7 +- .../kske/chess/game/UCIPlayerListener.java | 97 +++++++++++++++++++ src/dev/kske/chess/uci/UCIHandle.java | 22 ++--- src/dev/kske/chess/uci/UCIReceiver.java | 81 +++++++++++++++- 4 files changed, 188 insertions(+), 19 deletions(-) create mode 100644 src/dev/kske/chess/game/UCIPlayerListener.java diff --git a/src/dev/kske/chess/game/UCIPlayer.java b/src/dev/kske/chess/game/UCIPlayer.java index 1cdbb70..376dc55 100644 --- a/src/dev/kske/chess/game/UCIPlayer.java +++ b/src/dev/kske/chess/game/UCIPlayer.java @@ -13,12 +13,15 @@ import dev.kske.chess.uci.UCIHandle; */ public class UCIPlayer extends Player { - private UCIHandle handle; + private UCIHandle handle; + private UCIPlayerListener listener; public UCIPlayer(Color color, String enginePath) { super(color); try { handle = new UCIHandle(enginePath); + listener = new UCIPlayerListener(); + handle.setListener(listener); } catch (IOException ex) { ex.printStackTrace(); } @@ -31,7 +34,7 @@ public class UCIPlayer extends Player { @Override public void cancelMove() { - + handle.stop(); } public void disconnect() { diff --git a/src/dev/kske/chess/game/UCIPlayerListener.java b/src/dev/kske/chess/game/UCIPlayerListener.java new file mode 100644 index 0000000..6dcfae0 --- /dev/null +++ b/src/dev/kske/chess/game/UCIPlayerListener.java @@ -0,0 +1,97 @@ +package dev.kske.chess.game; + +import java.util.Map; + +import dev.kske.chess.uci.UCIListener; + +/** + * Project: Chess
+ * File: UCIPlayerListener.java
+ * Created: 20.07.2019
+ * Author: Kai S. K. Engelbart + */ +class UCIPlayerListener implements UCIListener { + + private String name, author; + + @Override + public void onIdName(String name) { + this.name = name; + } + + @Override + public void onIdAuthor(String author) { + this.author = author; + } + + @Override + public void onUCIOk() { + // TODO Auto-generated method stub + + } + + @Override + public void onReadyOk() { + // TODO Auto-generated method stub + + } + + @Override + public void onBestMove(String move) { + // TODO Auto-generated method stub + + } + + @Override + public void onBestMove(String move, String ponderMove) { + // TODO Auto-generated method stub + + } + + @Override + public void onCopyProtectionChecking() { + System.out.println("Copy protection checking..."); + } + + @Override + public void onCopyProtectionOk() { + System.out.println("Copy protection ok"); + } + + @Override + public void onCopyProtectionError() { + System.err.println("Copy protection error!"); + } + + @Override + public void onRegistrationChecking() { + System.out.println("Registration checking..."); + } + + @Override + public void onRegistrationOk() { + System.out.println("Registration ok"); + + } + + @Override + public void onRegistrationError() { + System.err.println("Registration error!"); + } + + @Override + public void onInfo(Map additionalInfo) { + System.out.println("Info:"); + additionalInfo.forEach((k, v) -> System.out.printf("%s: %s%n", k, v)); + } + + @Override + public void onOption(String name, GUIType type, String defaultVal, String minVal, String maxVal, String var) { + // TODO Auto-generated method stub + + } + + public String getName() { return name; }; + + public String getAuthor() { return author; }; +} diff --git a/src/dev/kske/chess/uci/UCIHandle.java b/src/dev/kske/chess/uci/UCIHandle.java index 5b4b547..3988a16 100644 --- a/src/dev/kske/chess/uci/UCIHandle.java +++ b/src/dev/kske/chess/uci/UCIHandle.java @@ -11,15 +11,13 @@ import java.io.PrintWriter; */ public class UCIHandle { - private final Process process; - private final PrintWriter out; + private final Process process; + private final PrintWriter out; private final UCIReceiver receiver; - private String name, author; - public UCIHandle(String enginePath) throws IOException { - process = new ProcessBuilder(enginePath).start(); - out = new PrintWriter(process.getOutputStream()); + process = new ProcessBuilder(enginePath).start(); + out = new PrintWriter(process.getOutputStream()); receiver = new UCIReceiver(process.getInputStream()); } @@ -82,13 +80,7 @@ public class UCIHandle { out.println("quit"); } - /** - * @return The name of the engine - */ - public String getName() { return name; } - - /** - * @return The author of the engine - */ - public String getAuthor() { return author; } + public void setListener(UCIListener listener) { + receiver.setListener(listener); + } } diff --git a/src/dev/kske/chess/uci/UCIReceiver.java b/src/dev/kske/chess/uci/UCIReceiver.java index 8610e69..c9c013b 100644 --- a/src/dev/kske/chess/uci/UCIReceiver.java +++ b/src/dev/kske/chess/uci/UCIReceiver.java @@ -4,6 +4,11 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import dev.kske.chess.uci.UCIListener.GUIType; /** * Project: Chess
@@ -53,8 +58,12 @@ public class UCIReceiver implements Runnable { case "registration": parseRegistration(line.substring(command.length() + 1)); break; - // TODO: info - // TODO: option + case "info": + parseInfo(line.substring(command.length() + 1)); + break; + case "option": + parseOption(line.substring(command.length() + 1)); + break; default: System.err.printf("Unknown command '%s' found!%n", command); } @@ -107,5 +116,73 @@ public class UCIReceiver implements Runnable { } } + private void parseInfo(String line) { + String[] tokens = line.split(" "); + Map additionalInfo = new HashMap<>(); + + for (int i = 0; i < tokens.length; i++) + switch (tokens[i]) { + // Single parameter info + case "depth": + case "seldepth": + case "time": + case "nodes": + case "multipv": + case "currmove": + case "currmovenumber": + case "hashfull": + case "nps": + case "tbhits": + case "sbhits": + case "cpuload": + case "string": + additionalInfo.put(tokens[i], tokens[++i]); + break; + // TODO: pv + // TODO: score + // TODO: refutation + // TODO: currline + default: + System.err.printf("Unknown parameter '%s' for command 'info' found!%n", tokens[i]); + } + listener.onInfo(additionalInfo); + } + + private void parseOption(String line) { + String[] tokens = line.split(" "); + + String name = ""; + GUIType type = null; + String defaultVal = null; + String minVal = null; + String maxVal = null; + String var = null; + + for (int i = 0; i < tokens.length; i++) + switch (tokens[i]) { + case "name": + while (!Arrays.asList("type", "defaultVal", "minVal", "maxVal", "var").contains(tokens[++i])) + name += tokens[i]; + break; + case "type": + type = GUIType.valueOf(tokens[i].toUpperCase()); + case "defaultVal": + defaultVal = tokens[++i]; + break; + case "minVal": + minVal = tokens[++i]; + break; + case "maxVal": + maxVal = tokens[++i]; + break; + case "var": + var = tokens[++i]; + break; + default: + System.err.printf("Unknown parameter '%s' for command 'option' found!%n", tokens[i]); + } + listener.onOption(name, type, defaultVal, minVal, maxVal, var); + } + public void setListener(UCIListener listener) { this.listener = listener; } }