2019-07-19 08:34:31 +02:00
|
|
|
package dev.kske.chess.uci;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Project: <strong>Chess</strong><br>
|
|
|
|
* File: <strong>UCIHandle.java</strong><br>
|
|
|
|
* Created: <strong>18.07.2019</strong><br>
|
|
|
|
* Author: <strong>Kai S. K. Engelbart</strong>
|
|
|
|
*/
|
|
|
|
public class UCIHandle {
|
|
|
|
|
2019-07-20 06:36:56 +02:00
|
|
|
private final Process process;
|
|
|
|
private final PrintWriter out;
|
2019-07-19 22:16:02 +02:00
|
|
|
private final UCIReceiver receiver;
|
2019-07-19 08:34:31 +02:00
|
|
|
|
|
|
|
public UCIHandle(String enginePath) throws IOException {
|
2019-07-20 06:36:56 +02:00
|
|
|
process = new ProcessBuilder(enginePath).start();
|
2019-07-21 14:35:14 +02:00
|
|
|
out = new PrintWriter(process.getOutputStream(), true);
|
2019-07-19 22:16:02 +02:00
|
|
|
receiver = new UCIReceiver(process.getInputStream());
|
2019-07-19 08:34:31 +02:00
|
|
|
}
|
|
|
|
|
2019-07-19 22:16:02 +02:00
|
|
|
public void start() {
|
2019-07-22 07:29:58 +02:00
|
|
|
new Thread(receiver, "UCI Receiver").start();
|
2019-07-21 14:35:14 +02:00
|
|
|
uci();
|
2019-07-19 08:34:31 +02:00
|
|
|
}
|
|
|
|
|
2019-07-19 22:16:02 +02:00
|
|
|
/**
|
|
|
|
* Tells the engine to use UCI.
|
|
|
|
*/
|
|
|
|
public void uci() {
|
|
|
|
out.println("uci");
|
2019-07-19 08:34:31 +02:00
|
|
|
}
|
|
|
|
|
2019-07-19 22:16:02 +02:00
|
|
|
/**
|
|
|
|
* Switches the debug mode of the engine on or off.
|
|
|
|
*
|
|
|
|
* @param debug Enables debugging if set to {@code true}, disables it otherwise
|
|
|
|
*/
|
|
|
|
public void debug(boolean debug) {
|
|
|
|
out.println("debug " + (debug ? "on" : "off"));
|
2019-07-19 08:34:31 +02:00
|
|
|
}
|
|
|
|
|
2019-07-19 22:16:02 +02:00
|
|
|
/**
|
|
|
|
* Synchronized the engine with the GUI
|
|
|
|
*/
|
|
|
|
public void isready() {
|
|
|
|
out.println("isready");
|
|
|
|
}
|
|
|
|
|
2019-07-22 08:59:13 +02:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2019-07-19 22:16:02 +02:00
|
|
|
/**
|
|
|
|
* Registers the engine
|
|
|
|
*
|
|
|
|
* @param name The name the engine should be registered with
|
|
|
|
* @param code The code the engine should be registered with
|
|
|
|
*/
|
|
|
|
public void register(String name, String code) {
|
|
|
|
out.printf("register %s %s%n", name, code);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tells the engine to postpone the registration.
|
|
|
|
*/
|
|
|
|
public void registerLater() {
|
|
|
|
out.println("register later");
|
|
|
|
}
|
|
|
|
|
2019-07-22 08:59:13 +02:00
|
|
|
/**
|
|
|
|
* Tells the engine that the next search will be from a different game.
|
|
|
|
*/
|
|
|
|
public void uciNewGame() {
|
|
|
|
out.println("ucinewgame");
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: position
|
|
|
|
// TODO: go
|
|
|
|
|
2019-07-19 22:16:02 +02:00
|
|
|
/**
|
|
|
|
* Stops calculation as soon as possible.
|
|
|
|
*/
|
|
|
|
public void stop() {
|
|
|
|
out.println("stop");
|
|
|
|
}
|
|
|
|
|
2019-07-22 08:59:13 +02:00
|
|
|
/**
|
|
|
|
* Tells the engine that the user has played the expected move.
|
|
|
|
*/
|
|
|
|
public void ponderHit() {
|
|
|
|
out.println("ponderhit");
|
|
|
|
}
|
|
|
|
|
2019-07-19 22:16:02 +02:00
|
|
|
/**
|
|
|
|
* Quits the engine process as soon as possible.
|
|
|
|
*/
|
|
|
|
public void quit() {
|
|
|
|
out.println("quit");
|
2019-07-19 08:34:31 +02:00
|
|
|
}
|
|
|
|
|
2019-07-20 06:36:56 +02:00
|
|
|
public void setListener(UCIListener listener) {
|
|
|
|
receiver.setListener(listener);
|
|
|
|
}
|
2019-07-19 08:34:31 +02:00
|
|
|
}
|