Fixed and fully implemented UCI 'info' command parsing

- Except for 'currline' which is a feature requested by the GUI and thus
optional
This commit is contained in:
Kai S. K. Engelbart 2019-08-01 21:34:01 +02:00
parent c940663fbf
commit 5bfc57a5af

View File

@ -1,6 +1,7 @@
package dev.kske.chess.uci;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import dev.kske.chess.board.Move;
@ -20,6 +21,28 @@ public class UCIInfo {
private Score score;
private String displayString;
/**
* Contains every parameter for the UCI info command. Helpful for parsing
* multi-value parameters.
*/
private static final List<String> params = Arrays.asList("depth",
"seldepth",
"time",
"nodes",
"multipv",
"currmove",
"currmovenumber",
"hashfull",
"nps",
"tbhits",
"sbhits",
"cpuload",
"string",
"score",
"pv",
"refutation",
"currline");
public UCIInfo(String line) {
pv = new ArrayList<>();
refutation = new ArrayList<>();
@ -70,13 +93,21 @@ public class UCIInfo {
break;
case "score":
score = new Score(line.substring(line.indexOf("score") + tokens[i].length() + 1));
i += score.isLowerbound() || score.isUpperbound() ? 1 : 2;
// TODO: pv
// TODO: refutation
// TODO: currline
i += score.getLength() + 1;
break;
case "pv":
while (++i < tokens.length && !params.contains(tokens[i]))
pv.add(Move.fromSAN(tokens[i]));
break;
case "refutation":
while (++i < tokens.length && !params.contains(tokens[i]))
refutation.add(Move.fromSAN(tokens[i]));
break;
// TODO: currline
case "currline":
while (++i < tokens.length && !params.contains(tokens[i]))
;
System.err.println("The parameter 'currline' for command 'info' is not yet implemented");
break;
default:
System.err.printf("Unknown parameter '%s' for command 'info' found!%n", tokens[i]);
@ -123,25 +154,31 @@ public class UCIInfo {
private int cp, mate;
private boolean lowerbound, upperbound;
private int length;
public Score(String line) {
String[] tokens = line.split(" ");
switch (tokens[0]) {
case "cp":
cp = Integer.parseInt(tokens[1]);
break;
case "mate":
mate = Integer.parseInt(tokens[1]);
break;
case "lowerbound":
lowerbound = true;
break;
case "upperbound":
upperbound = true;
break;
default:
System.err.printf("Unknown parameter '%s' for command 'score' found!%n", tokens[0]);
String[] tokens = line.split(" ");
int i = 0;
for (; i < tokens.length; i++) {
if (params.contains(tokens[i])) break;
switch (tokens[i]) {
case "cp":
cp = Integer.parseInt(tokens[++i]);
break;
case "mate":
mate = Integer.parseInt(tokens[++i]);
break;
case "lowerbound":
lowerbound = true;
break;
case "upperbound":
upperbound = true;
break;
default:
System.err.printf("Unknown parameter '%s' for command 'score' found!%n", tokens[i]);
}
}
length = i + 1;
}
public int getCp() { return cp; }
@ -151,5 +188,11 @@ public class UCIInfo {
public boolean isLowerbound() { return lowerbound; }
public boolean isUpperbound() { return upperbound; }
/**
* @return The number of tokens this 'score' command contains (including
* itself).
*/
public int getLength() { return length; }
}
}