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:
parent
d8f5f3bbf4
commit
29e17d90a5
40
src/dev/kske/chess/game/UCIPlayer.java
Normal file
40
src/dev/kske/chess/game/UCIPlayer.java
Normal 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() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
73
src/dev/kske/chess/uci/UCIHandle.java
Normal file
73
src/dev/kske/chess/uci/UCIHandle.java
Normal 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; }
|
||||||
|
}
|
Reference in New Issue
Block a user