From f92715e42f28b71d7f3fd2dfd96f86e67e99806a Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Mon, 22 Jul 2019 08:59:13 +0200 Subject: [PATCH] Implemented option setting, added UCIOption class --- .../kske/chess/game/UCIPlayerListener.java | 17 ++++++--- src/dev/kske/chess/uci/UCIHandle.java | 36 +++++++++++++++++++ src/dev/kske/chess/uci/UCIListener.java | 14 ++------ src/dev/kske/chess/uci/UCIOption.java | 25 +++++++++++++ src/dev/kske/chess/uci/UCIReceiver.java | 26 ++++++-------- 5 files changed, 85 insertions(+), 33 deletions(-) create mode 100644 src/dev/kske/chess/uci/UCIOption.java diff --git a/src/dev/kske/chess/game/UCIPlayerListener.java b/src/dev/kske/chess/game/UCIPlayerListener.java index 494de94..8bf61cd 100644 --- a/src/dev/kske/chess/game/UCIPlayerListener.java +++ b/src/dev/kske/chess/game/UCIPlayerListener.java @@ -1,9 +1,11 @@ package dev.kske.chess.game; +import java.util.ArrayList; import java.util.List; import java.util.Map; import dev.kske.chess.uci.UCIListener; +import dev.kske.chess.uci.UCIOption; /** * Project: Chess
@@ -13,7 +15,12 @@ import dev.kske.chess.uci.UCIListener; */ class UCIPlayerListener implements UCIListener { - private String name, author; + private String name, author; + private List options; + + public UCIPlayerListener() { + options = new ArrayList<>(); + } @Override public void onIdName(String name) { @@ -27,12 +34,12 @@ class UCIPlayerListener implements UCIListener { @Override public void onUCIOk() { - // TODO Auto-generated method stub + System.out.println("UCI ok"); } @Override public void onReadyOk() { - // TODO Auto-generated method stub + System.out.println("Ready ok"); } @Override @@ -82,8 +89,8 @@ class UCIPlayerListener implements UCIListener { } @Override - public void onOption(String name, GUIType type, String defaultVal, String minVal, String maxVal, List var) { - System.out.printf("Option %s of type %s, min %s, max %s, default %s%n", name, type, minVal, maxVal, defaultVal); + public void onOption(UCIOption option) { + options.add(option); } public String getName() { return name; }; diff --git a/src/dev/kske/chess/uci/UCIHandle.java b/src/dev/kske/chess/uci/UCIHandle.java index 6772201..a032473 100644 --- a/src/dev/kske/chess/uci/UCIHandle.java +++ b/src/dev/kske/chess/uci/UCIHandle.java @@ -49,6 +49,25 @@ public class UCIHandle { out.println("isready"); } + /** + * Signifies a button press to the engine. + * + * @param name The name of the button + */ + public void setOption(String name) { + out.println("setoption name " + name); + } + + /** + * Changes an internal parameter of the engine. + * + * @param name The name of the parameter + * @param value The value of the parameter + */ + public void setOption(String name, String value) { + out.printf("setoption name %s value %s%n", name, value); + } + /** * Registers the engine * @@ -66,6 +85,16 @@ public class UCIHandle { out.println("register later"); } + /** + * Tells the engine that the next search will be from a different game. + */ + public void uciNewGame() { + out.println("ucinewgame"); + } + + // TODO: position + // TODO: go + /** * Stops calculation as soon as possible. */ @@ -73,6 +102,13 @@ public class UCIHandle { out.println("stop"); } + /** + * Tells the engine that the user has played the expected move. + */ + public void ponderHit() { + out.println("ponderhit"); + } + /** * Quits the engine process as soon as possible. */ diff --git a/src/dev/kske/chess/uci/UCIListener.java b/src/dev/kske/chess/uci/UCIListener.java index 60e0361..e003dcf 100644 --- a/src/dev/kske/chess/uci/UCIListener.java +++ b/src/dev/kske/chess/uci/UCIListener.java @@ -1,6 +1,5 @@ package dev.kske.chess.uci; -import java.util.List; import java.util.Map; /** @@ -90,16 +89,7 @@ public interface UCIListener { /** * Tells the GUI which parameters can be changed in the engine. * - * @param name The name of the option - * @param type The GUI component through which the option should be set - * @param defaultVal The default value of this parameter - * @param minVal The minimum value of this parameter - * @param maxVal The maximum value of this parameter - * @param var A predefined value of this parameter + * @param option Option object describing the parameter */ - void onOption(String name, GUIType type, String defaultVal, String minVal, String maxVal, List var); - - public static enum GUIType { - CHECK, SPIN, COMBO, BUTTON, STRING - } + void onOption(UCIOption option); } diff --git a/src/dev/kske/chess/uci/UCIOption.java b/src/dev/kske/chess/uci/UCIOption.java new file mode 100644 index 0000000..ba6df2a --- /dev/null +++ b/src/dev/kske/chess/uci/UCIOption.java @@ -0,0 +1,25 @@ +package dev.kske.chess.uci; + +import java.util.ArrayList; +import java.util.List; + +/** + * Project: Chess
+ * File: UCIOption.java
+ * Created: 22.07.2019
+ * Author: Kai S. K. Engelbart + */ +public class UCIOption { + + public String name, defaultVal, minVal, maxVal; + public GUIType type; + public List varList; + + public UCIOption() { + varList = new ArrayList<>(); + } + + public static enum GUIType { + CHECK, SPIN, COMBO, BUTTON, STRING + } +} diff --git a/src/dev/kske/chess/uci/UCIReceiver.java b/src/dev/kske/chess/uci/UCIReceiver.java index 89155c7..2cce6cf 100644 --- a/src/dev/kske/chess/uci/UCIReceiver.java +++ b/src/dev/kske/chess/uci/UCIReceiver.java @@ -4,14 +4,12 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; -import java.util.List; import java.util.Map; import java.util.StringJoiner; -import dev.kske.chess.uci.UCIListener.GUIType; +import dev.kske.chess.uci.UCIOption.GUIType; /** * Project: Chess
@@ -155,40 +153,36 @@ public class UCIReceiver implements Runnable { private void parseOption(String line) { String[] tokens = line.split(" "); - - StringJoiner name = new StringJoiner(" "); - GUIType type = null; - String defaultVal = null; - String minVal = null; - String maxVal = null; - List var = new ArrayList<>(); + UCIOption option = new UCIOption(); for (int i = 0; i < tokens.length; i++) switch (tokens[i]) { case "name": + StringJoiner name = new StringJoiner(" "); while (!Arrays.asList("type", "default", "min", "max", "var").contains(tokens[i + 1])) name.add(tokens[++i]); + option.name = name.toString(); break; case "type": - type = GUIType.valueOf(tokens[++i].toUpperCase()); + option.type = GUIType.valueOf(tokens[++i].toUpperCase()); break; case "default": // Default string may be empty - defaultVal = i == tokens.length - 1 ? "" : tokens[++i]; + option.defaultVal = i == tokens.length - 1 ? "" : tokens[++i]; break; case "min": - minVal = tokens[++i]; + option.minVal = tokens[++i]; break; case "max": - maxVal = tokens[++i]; + option.maxVal = tokens[++i]; break; case "var": - var.add(tokens[++i]); + option.varList.add(tokens[++i]); break; default: System.err.printf("Unknown parameter '%s' for command 'option' found!%n", tokens[i]); } - listener.onOption(name.toString(), type, defaultVal, minVal, maxVal, var); + listener.onOption(option); } public void setListener(UCIListener listener) { this.listener = listener; }