package dev.kske.chess.uci; import java.io.IOException; import java.io.PrintWriter; /** * Project: Chess
* File: UCIHandle.java
* Created: 18.07.2019
* Author: Kai S. K. Engelbart */ public class UCIHandle { private final Process process; private final PrintWriter out; private final UCIReceiver receiver; public UCIHandle(String enginePath) throws IOException { process = new ProcessBuilder(enginePath).start(); out = new PrintWriter(process.getOutputStream(), true); receiver = new UCIReceiver(process.getInputStream()); } public void start() { new Thread(receiver, "UCI Receiver").start(); uci(); } /** * Tells the engine to use UCI. */ public void uci() { out.println("uci"); } /** * 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")); } /** * Synchronized the engine with the GUI */ public void 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 * * @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"); } /** * Tells the engine that the next search will be from a different game. */ public void uciNewGame() { out.println("ucinewgame"); } // TODO: position /** * Sets up the position in its initial state. */ public void startPosition() { out.println("position startpos"); } /** * Sets up the position described in the FEN string. * * @param fen FEN representation of the current board */ public void positionFEN(String fen) { out.println("position fen " + fen); } // TODO: go with parameters public void go() { out.println("go"); } /** * Stops calculation as soon as possible. */ public void 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. */ public void quit() { out.println("quit"); } public void setListener(UCIListener listener) { receiver.addListener(listener); } }