/
ConsoleUiChannel.java
114 lines (90 loc) · 3.07 KB
/
ConsoleUiChannel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package net.marvk.chess.uci4j;
import net.marvk.chess.core.UciMove;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.stream.Collectors;
public class ConsoleUiChannel implements UiChannel {
private final Consumer<String> console;
public ConsoleUiChannel(final Consumer<String> console) {
this.console = console;
}
private void send(final String s) {
console.accept(s);
}
@Override
public void idName(final String name) {
Objects.requireNonNull(name);
CommandUtil.requireElseThrow(name, CommandUtil::isStringNotEmpty);
send("id name " + name);
}
@Override
public void idAuthor(final String author) {
Objects.requireNonNull(author);
CommandUtil.requireElseThrow(author, s -> !s.isEmpty());
send("id author " + author);
}
@Override
public void uciOk() {
send("uciok");
}
@Override
public void readyOk() {
send("readyok");
}
@Override
public void bestMove(final UciMove move) {
if (move == null) {
send("bestmove 0000");
} else {
send("bestmove " + move.toString());
}
}
@Override
public void bestMove(final UciMove move, final UciMove ponder) {
if (move == null) {
send("bestmove 0000 ponder " + ponder.toString());
} else {
send("bestmove " + move.toString() + " ponder " + ponder.toString());
}
}
@Override
public void copyProtection() {
send("copyprotection");
}
@Override
public void registration() {
send("registration");
}
@Override
public void info(final Info info) {
send(info.toCommand());
}
@Override
public void optionCheck(final String name, final boolean defaultValue) {
send(CommandUtil.optionCommand(name, "check", defaultValue));
}
@Override
public void optionSpin(final String name, final int defaultValue, final int min, final int max) {
send(CommandUtil.optionCommand(name, "spin", defaultValue) + " min " + min + " max " + max);
}
@Override
public void optionCombo(final String name, final String defaultValue, final List<String> possibleValues) {
CommandUtil.requireElseThrow(defaultValue, possibleValues::contains);
CommandUtil.requireElseThrow(possibleValues, list -> !list.isEmpty());
final String possibleValuesString =
possibleValues.stream()
.map(s -> "var " + s)
.map(s -> s.replaceAll("\n", ""))
.collect(Collectors.joining(" "));
send(CommandUtil.optionCommand(name, "combo", defaultValue) + " " + possibleValuesString);
}
@Override
public void optionString(final String name, final String defaultValue) {
send(CommandUtil.optionCommand(name, "string", defaultValue));
}
@Override
public void optionButton(final String name) {
send(CommandUtil.optionCommand(name, "button"));
}
}