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:
		@@ -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,15 +154,19 @@ 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]) {
 | 
			
		||||
			int			i		= 0;
 | 
			
		||||
			for (; i < tokens.length; i++) {
 | 
			
		||||
				if (params.contains(tokens[i])) break;
 | 
			
		||||
				switch (tokens[i]) {
 | 
			
		||||
					case "cp":
 | 
			
		||||
					cp = Integer.parseInt(tokens[1]);
 | 
			
		||||
						cp = Integer.parseInt(tokens[++i]);
 | 
			
		||||
						break;
 | 
			
		||||
					case "mate":
 | 
			
		||||
					mate = Integer.parseInt(tokens[1]);
 | 
			
		||||
						mate = Integer.parseInt(tokens[++i]);
 | 
			
		||||
						break;
 | 
			
		||||
					case "lowerbound":
 | 
			
		||||
						lowerbound = true;
 | 
			
		||||
@@ -140,9 +175,11 @@ public class UCIInfo {
 | 
			
		||||
						upperbound = true;
 | 
			
		||||
						break;
 | 
			
		||||
					default:
 | 
			
		||||
					System.err.printf("Unknown parameter '%s' for command 'score' found!%n", tokens[0]);
 | 
			
		||||
						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; }
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user