Implemented option setting, added UCIOption class

This commit is contained in:
Kai S. K. Engelbart 2019-07-22 08:59:13 +02:00
parent ab54f88a89
commit a68a87962c
5 changed files with 85 additions and 33 deletions

View File

@ -1,9 +1,11 @@
package dev.kske.chess.game; package dev.kske.chess.game;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import dev.kske.chess.uci.UCIListener; import dev.kske.chess.uci.UCIListener;
import dev.kske.chess.uci.UCIOption;
/** /**
* Project: <strong>Chess</strong><br> * Project: <strong>Chess</strong><br>
@ -13,7 +15,12 @@ import dev.kske.chess.uci.UCIListener;
*/ */
class UCIPlayerListener implements UCIListener { class UCIPlayerListener implements UCIListener {
private String name, author; private String name, author;
private List<UCIOption> options;
public UCIPlayerListener() {
options = new ArrayList<>();
}
@Override @Override
public void onIdName(String name) { public void onIdName(String name) {
@ -27,12 +34,12 @@ class UCIPlayerListener implements UCIListener {
@Override @Override
public void onUCIOk() { public void onUCIOk() {
// TODO Auto-generated method stub System.out.println("UCI ok");
} }
@Override @Override
public void onReadyOk() { public void onReadyOk() {
// TODO Auto-generated method stub System.out.println("Ready ok");
} }
@Override @Override
@ -82,8 +89,8 @@ class UCIPlayerListener implements UCIListener {
} }
@Override @Override
public void onOption(String name, GUIType type, String defaultVal, String minVal, String maxVal, List<String> var) { public void onOption(UCIOption option) {
System.out.printf("Option %s of type %s, min %s, max %s, default %s%n", name, type, minVal, maxVal, defaultVal); options.add(option);
} }
public String getName() { return name; }; public String getName() { return name; };

View File

@ -49,6 +49,25 @@ public class UCIHandle {
out.println("isready"); out.println("isready");
} }
/**
* Signifies a button press to the engine.
*
* @param name The name of the button
*/
public void setOption(String name) {
out.println("setoption name " + name);
}
/**
* Changes an internal parameter of the engine.
*
* @param name The name of the parameter
* @param value The value of the parameter
*/
public void setOption(String name, String value) {
out.printf("setoption name %s value %s%n", name, value);
}
/** /**
* Registers the engine * Registers the engine
* *
@ -66,6 +85,16 @@ public class UCIHandle {
out.println("register later"); out.println("register later");
} }
/**
* Tells the engine that the next search will be from a different game.
*/
public void uciNewGame() {
out.println("ucinewgame");
}
// TODO: position
// TODO: go
/** /**
* Stops calculation as soon as possible. * Stops calculation as soon as possible.
*/ */
@ -73,6 +102,13 @@ public class UCIHandle {
out.println("stop"); out.println("stop");
} }
/**
* Tells the engine that the user has played the expected move.
*/
public void ponderHit() {
out.println("ponderhit");
}
/** /**
* Quits the engine process as soon as possible. * Quits the engine process as soon as possible.
*/ */

View File

@ -1,6 +1,5 @@
package dev.kske.chess.uci; package dev.kske.chess.uci;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
@ -90,16 +89,7 @@ public interface UCIListener {
/** /**
* Tells the GUI which parameters can be changed in the engine. * Tells the GUI which parameters can be changed in the engine.
* *
* @param name The name of the option * @param option Option object describing the parameter
* @param type The GUI component through which the option should be set
* @param defaultVal The default value of this parameter
* @param minVal The minimum value of this parameter
* @param maxVal The maximum value of this parameter
* @param var A predefined value of this parameter
*/ */
void onOption(String name, GUIType type, String defaultVal, String minVal, String maxVal, List<String> var); void onOption(UCIOption option);
public static enum GUIType {
CHECK, SPIN, COMBO, BUTTON, STRING
}
} }

View File

@ -0,0 +1,25 @@
package dev.kske.chess.uci;
import java.util.ArrayList;
import java.util.List;
/**
* Project: <strong>Chess</strong><br>
* File: <strong>UCIOption.java</strong><br>
* Created: <strong>22.07.2019</strong><br>
* Author: <strong>Kai S. K. Engelbart</strong>
*/
public class UCIOption {
public String name, defaultVal, minVal, maxVal;
public GUIType type;
public List<String> varList;
public UCIOption() {
varList = new ArrayList<>();
}
public static enum GUIType {
CHECK, SPIN, COMBO, BUTTON, STRING
}
}

View File

@ -4,14 +4,12 @@ import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.StringJoiner; import java.util.StringJoiner;
import dev.kske.chess.uci.UCIListener.GUIType; import dev.kske.chess.uci.UCIOption.GUIType;
/** /**
* Project: <strong>Chess</strong><br> * Project: <strong>Chess</strong><br>
@ -155,40 +153,36 @@ public class UCIReceiver implements Runnable {
private void parseOption(String line) { private void parseOption(String line) {
String[] tokens = line.split(" "); String[] tokens = line.split(" ");
UCIOption option = new UCIOption();
StringJoiner name = new StringJoiner(" ");
GUIType type = null;
String defaultVal = null;
String minVal = null;
String maxVal = null;
List<String> var = new ArrayList<>();
for (int i = 0; i < tokens.length; i++) for (int i = 0; i < tokens.length; i++)
switch (tokens[i]) { switch (tokens[i]) {
case "name": case "name":
StringJoiner name = new StringJoiner(" ");
while (!Arrays.asList("type", "default", "min", "max", "var").contains(tokens[i + 1])) while (!Arrays.asList("type", "default", "min", "max", "var").contains(tokens[i + 1]))
name.add(tokens[++i]); name.add(tokens[++i]);
option.name = name.toString();
break; break;
case "type": case "type":
type = GUIType.valueOf(tokens[++i].toUpperCase()); option.type = GUIType.valueOf(tokens[++i].toUpperCase());
break; break;
case "default": case "default":
// Default string may be empty // Default string may be empty
defaultVal = i == tokens.length - 1 ? "" : tokens[++i]; option.defaultVal = i == tokens.length - 1 ? "" : tokens[++i];
break; break;
case "min": case "min":
minVal = tokens[++i]; option.minVal = tokens[++i];
break; break;
case "max": case "max":
maxVal = tokens[++i]; option.maxVal = tokens[++i];
break; break;
case "var": case "var":
var.add(tokens[++i]); option.varList.add(tokens[++i]);
break; break;
default: default:
System.err.printf("Unknown parameter '%s' for command 'option' found!%n", tokens[i]); System.err.printf("Unknown parameter '%s' for command 'option' found!%n", tokens[i]);
} }
listener.onOption(name.toString(), type, defaultVal, minVal, maxVal, var); listener.onOption(option);
} }
public void setListener(UCIListener listener) { this.listener = listener; } public void setListener(UCIListener listener) { this.listener = listener; }