From 84314d931e280df2f17c096fe33904ed7d991f70 Mon Sep 17 00:00:00 2001 From: kske Date: Thu, 1 Aug 2019 21:34:01 +0200 Subject: [PATCH] Fixed and fully implemented UCI 'info' command parsing - Except for 'currline' which is a feature requested by the GUI and thus optional --- src/dev/kske/chess/uci/UCIInfo.java | 83 ++++++++++++++++++++++------- 1 file changed, 63 insertions(+), 20 deletions(-) diff --git a/src/dev/kske/chess/uci/UCIInfo.java b/src/dev/kske/chess/uci/UCIInfo.java index 23ed319..5c47f01 100644 --- a/src/dev/kske/chess/uci/UCIInfo.java +++ b/src/dev/kske/chess/uci/UCIInfo.java @@ -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 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; } } }