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;
 | 
					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,15 +154,19 @@ 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;
 | 
				
			||||||
 | 
								for (; i < tokens.length; i++) {
 | 
				
			||||||
 | 
									if (params.contains(tokens[i])) break;
 | 
				
			||||||
 | 
									switch (tokens[i]) {
 | 
				
			||||||
					case "cp":
 | 
										case "cp":
 | 
				
			||||||
					cp = Integer.parseInt(tokens[1]);
 | 
											cp = Integer.parseInt(tokens[++i]);
 | 
				
			||||||
						break;
 | 
											break;
 | 
				
			||||||
					case "mate":
 | 
										case "mate":
 | 
				
			||||||
					mate = Integer.parseInt(tokens[1]);
 | 
											mate = Integer.parseInt(tokens[++i]);
 | 
				
			||||||
						break;
 | 
											break;
 | 
				
			||||||
					case "lowerbound":
 | 
										case "lowerbound":
 | 
				
			||||||
						lowerbound = true;
 | 
											lowerbound = true;
 | 
				
			||||||
@@ -140,9 +175,11 @@ public class UCIInfo {
 | 
				
			|||||||
						upperbound = true;
 | 
											upperbound = true;
 | 
				
			||||||
						break;
 | 
											break;
 | 
				
			||||||
					default:
 | 
										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; }
 | 
							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; }
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user