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 36597ac6f1
commit 1ce8b8355a

View File

@ -1,6 +1,7 @@
package dev.kske.chess.uci; package dev.kske.chess.uci;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import dev.kske.chess.board.Move; import dev.kske.chess.board.Move;
@ -20,6 +21,28 @@ public class UCIInfo {
private Score score; private Score score;
private String displayString; 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) { public UCIInfo(String line) {
pv = new ArrayList<>(); pv = new ArrayList<>();
refutation = new ArrayList<>(); refutation = new ArrayList<>();
@ -70,13 +93,21 @@ public class UCIInfo {
break; break;
case "score": case "score":
score = new Score(line.substring(line.indexOf("score") + tokens[i].length() + 1)); score = new Score(line.substring(line.indexOf("score") + tokens[i].length() + 1));
i += score.isLowerbound() || score.isUpperbound() ? 1 : 2; i += score.getLength() + 1;
// TODO: pv break;
// TODO: refutation
// TODO: currline
case "pv": case "pv":
while (++i < tokens.length && !params.contains(tokens[i]))
pv.add(Move.fromSAN(tokens[i]));
break;
case "refutation": case "refutation":
while (++i < tokens.length && !params.contains(tokens[i]))
refutation.add(Move.fromSAN(tokens[i]));
break;
// TODO: currline
case "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; break;
default: default:
System.err.printf("Unknown parameter '%s' for command 'info' found!%n", tokens[i]); 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 int cp, mate;
private boolean lowerbound, upperbound; private boolean lowerbound, upperbound;
private int length;
public Score(String line) { public Score(String line) {
String[] tokens = line.split(" "); String[] tokens = line.split(" ");
switch (tokens[0]) { int i = 0;
case "cp": for (; i < tokens.length; i++) {
cp = Integer.parseInt(tokens[1]); if (params.contains(tokens[i])) break;
break; switch (tokens[i]) {
case "mate": case "cp":
mate = Integer.parseInt(tokens[1]); cp = Integer.parseInt(tokens[++i]);
break; break;
case "lowerbound": case "mate":
lowerbound = true; mate = Integer.parseInt(tokens[++i]);
break; break;
case "upperbound": case "lowerbound":
upperbound = true; lowerbound = true;
break; break;
default: case "upperbound":
System.err.printf("Unknown parameter '%s' for command 'score' found!%n", tokens[0]); 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; } public int getCp() { return cp; }
@ -151,5 +188,11 @@ public class UCIInfo {
public boolean isLowerbound() { return lowerbound; } public boolean isLowerbound() { return lowerbound; }
public boolean isUpperbound() { return upperbound; } public boolean isUpperbound() { return upperbound; }
/**
* @return The number of tokens this 'score' command contains (including
* itself).
*/
public int getLength() { return length; }
} }
} }