Added UCIListener, started working on an implementation

This commit is contained in:
Kai S. K. Engelbart 2019-07-20 06:36:56 +02:00
parent 062a5c3075
commit 347eb5d531
4 changed files with 188 additions and 19 deletions

View File

@ -13,12 +13,15 @@ import dev.kske.chess.uci.UCIHandle;
*/ */
public class UCIPlayer extends Player { public class UCIPlayer extends Player {
private UCIHandle handle; private UCIHandle handle;
private UCIPlayerListener listener;
public UCIPlayer(Color color, String enginePath) { public UCIPlayer(Color color, String enginePath) {
super(color); super(color);
try { try {
handle = new UCIHandle(enginePath); handle = new UCIHandle(enginePath);
listener = new UCIPlayerListener();
handle.setListener(listener);
} catch (IOException ex) { } catch (IOException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
@ -31,7 +34,7 @@ public class UCIPlayer extends Player {
@Override @Override
public void cancelMove() { public void cancelMove() {
handle.stop();
} }
public void disconnect() { public void disconnect() {

View File

@ -0,0 +1,97 @@
package dev.kske.chess.game;
import java.util.Map;
import dev.kske.chess.uci.UCIListener;
/**
* Project: <strong>Chess</strong><br>
* File: <strong>UCIPlayerListener.java</strong><br>
* Created: <strong>20.07.2019</strong><br>
* Author: <strong>Kai S. K. Engelbart</strong>
*/
class UCIPlayerListener implements UCIListener {
private String name, author;
@Override
public void onIdName(String name) {
this.name = name;
}
@Override
public void onIdAuthor(String author) {
this.author = author;
}
@Override
public void onUCIOk() {
// TODO Auto-generated method stub
}
@Override
public void onReadyOk() {
// TODO Auto-generated method stub
}
@Override
public void onBestMove(String move) {
// TODO Auto-generated method stub
}
@Override
public void onBestMove(String move, String ponderMove) {
// TODO Auto-generated method stub
}
@Override
public void onCopyProtectionChecking() {
System.out.println("Copy protection checking...");
}
@Override
public void onCopyProtectionOk() {
System.out.println("Copy protection ok");
}
@Override
public void onCopyProtectionError() {
System.err.println("Copy protection error!");
}
@Override
public void onRegistrationChecking() {
System.out.println("Registration checking...");
}
@Override
public void onRegistrationOk() {
System.out.println("Registration ok");
}
@Override
public void onRegistrationError() {
System.err.println("Registration error!");
}
@Override
public void onInfo(Map<String, String> additionalInfo) {
System.out.println("Info:");
additionalInfo.forEach((k, v) -> System.out.printf("%s: %s%n", k, v));
}
@Override
public void onOption(String name, GUIType type, String defaultVal, String minVal, String maxVal, String var) {
// TODO Auto-generated method stub
}
public String getName() { return name; };
public String getAuthor() { return author; };
}

View File

@ -11,15 +11,13 @@ import java.io.PrintWriter;
*/ */
public class UCIHandle { public class UCIHandle {
private final Process process; private final Process process;
private final PrintWriter out; private final PrintWriter out;
private final UCIReceiver receiver; private final UCIReceiver receiver;
private String name, author;
public UCIHandle(String enginePath) throws IOException { public UCIHandle(String enginePath) throws IOException {
process = new ProcessBuilder(enginePath).start(); process = new ProcessBuilder(enginePath).start();
out = new PrintWriter(process.getOutputStream()); out = new PrintWriter(process.getOutputStream());
receiver = new UCIReceiver(process.getInputStream()); receiver = new UCIReceiver(process.getInputStream());
} }
@ -82,13 +80,7 @@ public class UCIHandle {
out.println("quit"); out.println("quit");
} }
/** public void setListener(UCIListener listener) {
* @return The name of the engine receiver.setListener(listener);
*/ }
public String getName() { return name; }
/**
* @return The author of the engine
*/
public String getAuthor() { return author; }
} }

View File

@ -4,6 +4,11 @@ import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import dev.kske.chess.uci.UCIListener.GUIType;
/** /**
* Project: <strong>Chess</strong><br> * Project: <strong>Chess</strong><br>
@ -53,8 +58,12 @@ public class UCIReceiver implements Runnable {
case "registration": case "registration":
parseRegistration(line.substring(command.length() + 1)); parseRegistration(line.substring(command.length() + 1));
break; break;
// TODO: info case "info":
// TODO: option parseInfo(line.substring(command.length() + 1));
break;
case "option":
parseOption(line.substring(command.length() + 1));
break;
default: default:
System.err.printf("Unknown command '%s' found!%n", command); System.err.printf("Unknown command '%s' found!%n", command);
} }
@ -107,5 +116,73 @@ public class UCIReceiver implements Runnable {
} }
} }
private void parseInfo(String line) {
String[] tokens = line.split(" ");
Map<String, String> additionalInfo = new HashMap<>();
for (int i = 0; i < tokens.length; i++)
switch (tokens[i]) {
// Single parameter info
case "depth":
case "seldepth":
case "time":
case "nodes":
case "multipv":
case "currmove":
case "currmovenumber":
case "hashfull":
case "nps":
case "tbhits":
case "sbhits":
case "cpuload":
case "string":
additionalInfo.put(tokens[i], tokens[++i]);
break;
// TODO: pv
// TODO: score
// TODO: refutation
// TODO: currline
default:
System.err.printf("Unknown parameter '%s' for command 'info' found!%n", tokens[i]);
}
listener.onInfo(additionalInfo);
}
private void parseOption(String line) {
String[] tokens = line.split(" ");
String name = "";
GUIType type = null;
String defaultVal = null;
String minVal = null;
String maxVal = null;
String var = null;
for (int i = 0; i < tokens.length; i++)
switch (tokens[i]) {
case "name":
while (!Arrays.asList("type", "defaultVal", "minVal", "maxVal", "var").contains(tokens[++i]))
name += tokens[i];
break;
case "type":
type = GUIType.valueOf(tokens[i].toUpperCase());
case "defaultVal":
defaultVal = tokens[++i];
break;
case "minVal":
minVal = tokens[++i];
break;
case "maxVal":
maxVal = tokens[++i];
break;
case "var":
var = tokens[++i];
break;
default:
System.err.printf("Unknown parameter '%s' for command 'option' found!%n", tokens[i]);
}
listener.onOption(name, type, defaultVal, minVal, maxVal, var);
}
public void setListener(UCIListener listener) { this.listener = listener; } public void setListener(UCIListener listener) { this.listener = listener; }
} }