Fixed UCI option parsing

This commit is contained in:
Kai S. K. Engelbart 2019-07-22 07:29:58 +02:00
parent 77cf661f6f
commit fd7934f40e
3 changed files with 26 additions and 23 deletions

View File

@ -83,8 +83,7 @@ class UCIPlayerListener implements UCIListener {
@Override
public void onOption(String name, GUIType type, String defaultVal, String minVal, String maxVal, List<String> 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; };

View File

@ -22,7 +22,7 @@ public class UCIHandle {
}
public void start() {
new Thread(receiver).start();
new Thread(receiver, "UCI Receiver").start();
uci();
}

View File

@ -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,10 +31,10 @@ public class UCIReceiver implements Runnable {
@Override
public void run() {
try {
String line;
while (!Thread.currentThread().isInterrupted())
if ((line = in.readLine()) != null) parse(line);
try {
if ((line = in.readLine()) != null && !line.isBlank()) parse(line);
} catch (IndexOutOfBoundsException ex) {
System.err.println("Too few arguments were provided!");
ex.printStackTrace();
@ -43,7 +44,8 @@ public class UCIReceiver implements Runnable {
}
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; }