Working on UCI support

+ UCIHandle class for communicating with an engine through UCI
+ UCIPlayer class for using an engine within the gui
This commit is contained in:
Kai S. K. Engelbart 2019-07-19 08:34:31 +02:00
parent d8f5f3bbf4
commit 29e17d90a5
2 changed files with 113 additions and 0 deletions

View File

@ -0,0 +1,40 @@
package dev.kske.chess.game;
import java.io.IOException;
import dev.kske.chess.board.Piece.Color;
import dev.kske.chess.uci.UCIHandle;
/**
* Project: <strong>Chess</strong><br>
* File: <strong>UCIPlayer.java</strong><br>
* Created: <strong>18.07.2019</strong><br>
* Author: <strong>Kai S. K. Engelbart</strong>
*/
public class UCIPlayer extends Player {
private UCIHandle handle;
public UCIPlayer(Color color, String enginePath) {
super(color);
try {
handle = new UCIHandle(enginePath);
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public void requestMove() {
}
@Override
public void cancelMove() {
}
public void disconnect() {
}
}

View File

@ -0,0 +1,73 @@
package dev.kske.chess.uci;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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 {
private final Process process;
private final BufferedReader in;
private final PrintWriter out;
private String name, author;
public UCIHandle(String enginePath) throws IOException {
process = new ProcessBuilder(enginePath).start();
in = new BufferedReader(new InputStreamReader(process.getInputStream()));
out = new PrintWriter(process.getOutputStream());
}
public void processInput() {
try {
while (in.ready())
parse(in.readLine());
} catch (IndexOutOfBoundsException ex) {
System.err.println("Too few arguments were provided!");
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
private void parse(String line) {
String command = line.substring(0, line.indexOf(' '));
switch (command) {
case "id":
parseId(line.substring(command.length() + 1));
break;
default:
System.err.printf("Unknown command '%s' found!%n", command);
}
}
private void parseId(String line) {
String param = line.substring(line.indexOf(' '));
String arg = line.substring(param.length() + 1);
switch (line) {
case "name":
name = arg;
break;
case "author":
author = arg;
break;
default:
System.err.printf("Unknown parameter '%s' for command 'id' found!%n", param);
}
}
public void uci() {
out.println("uci");
}
public String getName() { return name; }
public String getAuthor() { return author; }
}