From 495e7848524f492f59252102eddb7d8710b2180d Mon Sep 17 00:00:00 2001 From: kske Date: Fri, 19 Jul 2019 08:34:31 +0200 Subject: [PATCH] Working on UCI support + UCIHandle class for communicating with an engine through UCI + UCIPlayer class for using an engine within the gui --- src/dev/kske/chess/game/UCIPlayer.java | 40 ++++++++++++++ src/dev/kske/chess/uci/UCIHandle.java | 73 ++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 src/dev/kske/chess/game/UCIPlayer.java create mode 100644 src/dev/kske/chess/uci/UCIHandle.java diff --git a/src/dev/kske/chess/game/UCIPlayer.java b/src/dev/kske/chess/game/UCIPlayer.java new file mode 100644 index 0000000..bb1bde3 --- /dev/null +++ b/src/dev/kske/chess/game/UCIPlayer.java @@ -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: Chess
+ * File: UCIPlayer.java
+ * Created: 18.07.2019
+ * Author: Kai S. K. Engelbart + */ +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() { + + } +} diff --git a/src/dev/kske/chess/uci/UCIHandle.java b/src/dev/kske/chess/uci/UCIHandle.java new file mode 100644 index 0000000..84d13f4 --- /dev/null +++ b/src/dev/kske/chess/uci/UCIHandle.java @@ -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: Chess
+ * File: UCIHandle.java
+ * Created: 18.07.2019
+ * Author: Kai S. K. Engelbart + */ +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; } +}