From fd7934f40e77ad4fc8b50ffc58f2eb399c4553ae Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Mon, 22 Jul 2019 07:29:58 +0200 Subject: [PATCH] Fixed UCI option parsing --- .../kske/chess/game/UCIPlayerListener.java | 3 +- src/dev/kske/chess/uci/UCIHandle.java | 2 +- src/dev/kske/chess/uci/UCIReceiver.java | 44 ++++++++++--------- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/dev/kske/chess/game/UCIPlayerListener.java b/src/dev/kske/chess/game/UCIPlayerListener.java index b647e89..494de94 100644 --- a/src/dev/kske/chess/game/UCIPlayerListener.java +++ b/src/dev/kske/chess/game/UCIPlayerListener.java @@ -83,8 +83,7 @@ class UCIPlayerListener implements UCIListener { @Override public void onOption(String name, GUIType type, String defaultVal, String minVal, String maxVal, List var) { - // TODO Auto-generated method stub - + System.out.printf("Option %s of type %s, min %s, max %s, default %s%n", name, type, minVal, maxVal, defaultVal); } public String getName() { return name; }; diff --git a/src/dev/kske/chess/uci/UCIHandle.java b/src/dev/kske/chess/uci/UCIHandle.java index bf02f4c..6772201 100644 --- a/src/dev/kske/chess/uci/UCIHandle.java +++ b/src/dev/kske/chess/uci/UCIHandle.java @@ -22,7 +22,7 @@ public class UCIHandle { } public void start() { - new Thread(receiver).start(); + new Thread(receiver, "UCI Receiver").start(); uci(); } diff --git a/src/dev/kske/chess/uci/UCIReceiver.java b/src/dev/kske/chess/uci/UCIReceiver.java index 5af8d61..89155c7 100644 --- a/src/dev/kske/chess/uci/UCIReceiver.java +++ b/src/dev/kske/chess/uci/UCIReceiver.java @@ -9,6 +9,7 @@ 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; @@ -30,20 +31,21 @@ public class UCIReceiver implements Runnable { @Override public void run() { - try { - String line; - while (!Thread.currentThread().isInterrupted()) - if ((line = in.readLine()) != null) parse(line); - } catch (IndexOutOfBoundsException ex) { - System.err.println("Too few arguments were provided!"); - ex.printStackTrace(); - } catch (IOException ex) { - ex.printStackTrace(); - } + String line; + while (!Thread.currentThread().isInterrupted()) + try { + if ((line = in.readLine()) != null && !line.isBlank()) parse(line); + } catch (IndexOutOfBoundsException ex) { + System.err.println("Too few arguments were provided!"); + ex.printStackTrace(); + } catch (IOException ex) { + ex.printStackTrace(); + } } private void parse(String line) { - String command = line.substring(0, line.indexOf(' ')); + int spaceIndex = line.indexOf(' '); + String command = spaceIndex == -1 ? line : line.substring(0, spaceIndex); switch (command) { case "id": parseId(line.substring(command.length() + 1)); @@ -154,7 +156,7 @@ public class UCIReceiver implements Runnable { private void parseOption(String line) { String[] tokens = line.split(" "); - String name = ""; + StringJoiner name = new StringJoiner(" "); GUIType type = null; String defaultVal = null; String minVal = null; @@ -164,18 +166,20 @@ public class UCIReceiver implements Runnable { 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]; + while (!Arrays.asList("type", "default", "min", "max", "var").contains(tokens[i + 1])) + name.add(tokens[++i]); break; case "type": - type = GUIType.valueOf(tokens[i].toUpperCase()); - case "defaultVal": - defaultVal = tokens[++i]; + type = GUIType.valueOf(tokens[++i].toUpperCase()); break; - case "minVal": + case "default": + // Default string may be empty + defaultVal = i == tokens.length - 1 ? "" : tokens[++i]; + break; + case "min": minVal = tokens[++i]; break; - case "maxVal": + case "max": maxVal = tokens[++i]; break; case "var": @@ -184,7 +188,7 @@ public class UCIReceiver implements Runnable { default: System.err.printf("Unknown parameter '%s' for command 'option' found!%n", tokens[i]); } - listener.onOption(name, type, defaultVal, minVal, maxVal, var); + listener.onOption(name.toString(), type, defaultVal, minVal, maxVal, var); } public void setListener(UCIListener listener) { this.listener = listener; }