Skip to content

Commit

Permalink
Fixes bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
fathzer committed Feb 13, 2024
1 parent d5551bb commit 0281a92
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
18 changes: 8 additions & 10 deletions src/main/java/com/fathzer/jchess/bot/uci/UCIEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class UCIEngine implements Closeable, Engine {
private static final String PONDER_OPTION = "Ponder";

private final Process process;
private final String name;
private String name;
private final BufferedReader reader;
private final BufferedWriter writer;
private final StdErrReader errorReader;
Expand All @@ -44,21 +44,23 @@ public UCIEngine(String path) throws IOException {
this.writer = process.outputWriter();
this.errorReader = new StdErrReader(process);
this.options = new ArrayList<>();
this.name = init();
init();
if (this.name==null) {
throw new IOException("Engine has no name!");
}
}

private String init() throws IOException {
private void init() throws IOException {
this.write("uci");
String line;
String result = null;
do {
line = read();
if (line==null) {
break;
}
final String namePrefix = "id name ";
if (line.startsWith(namePrefix)) {
result = line.substring(namePrefix.length());
this.name = line.substring(namePrefix.length());
} else {
final Optional<Option<?>> ooption = parseOption(line);
if (ooption.isPresent()) {
Expand All @@ -73,10 +75,6 @@ private String init() throws IOException {
}
}
} while (!"uciok".equals(line));
if (result==null) {
throw new IOException("Engine has no name!");
}
return result;
}

private Optional<Option<?>> parseOption(String line) throws IOException {
Expand All @@ -102,7 +100,7 @@ private void write(String line) {
this.writer.write(line);
this.writer.newLine();
this.writer.flush();
log.info(">{}: {}", name, line);
log.info(">{}: {}", name==null?"?":name, line);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Collections;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
Expand All @@ -17,6 +18,7 @@
import com.fathzer.soft.ajlib.swing.widget.IntegerWidget;
import com.fathzer.soft.ajlib.swing.widget.TextWidget;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.Insets;
Expand All @@ -31,6 +33,7 @@ public class EnginePanel extends JPanel {
*/
public EnginePanel(List<Option<?>> options) {
setLayout(new GridBagLayout());
setBackground(Color.blue);
if (options==null) {
options = Collections.emptyList();
}
Expand All @@ -47,28 +50,34 @@ public EnginePanel(List<Option<?>> options) {

private void addComponent(Option<?> option, GridBagConstraints ct) {
if (option instanceof CheckOption check) {
ct.gridwidth = 2;
add(getCheck(check), ct);
} else if (option instanceof ButtonOption button) {
ct.gridwidth = 2;
add(getButton(button), ct);
} else {
add(new JLabel(option.getName()+": "),ct);
add(new JLabel(option.getName()+": "), ct);
ct.gridx++;
if (option instanceof ComboOption combo) {
add(getCombo(combo),ct);
add(getCombo(combo), ct);
} else if (option instanceof SpinOption spin) {
add(getSpin(spin),ct);
add(getSpin(spin), ct);
} else if (option instanceof StringOption string) {
add(getString(string),ct);
ct.weightx = 1;
add(getString(string), ct);
ct.weightx = 0;
} else {
throw new IllegalArgumentException("Type "+option.getType()+" is not supported");
}
ct.gridx--;
}
ct.gridwidth = 1;
}

private Component getButton(ButtonOption option) {
// TODO Auto-generated method stub
return new JLabel("TODO");
final JButton button = new JButton(option.getName());
button.addActionListener(e -> option.setValue(null));
return button;
}

private Component getCheck(CheckOption option) {
Expand All @@ -91,7 +100,12 @@ private Component getCombo(ComboOption option) {
private Component getSpin(SpinOption option) {
final IntegerWidget widget = new IntegerWidget(BigInteger.valueOf(option.getMin()), BigInteger.valueOf(option.getMax()));
widget.setValue(BigInteger.valueOf(option.getValue()));
widget.addActionListener(e -> option.setValue(widget.getValue().longValue()));
widget.addPropertyChangeListener(IntegerWidget.VALUE_PROPERTY, e -> {
final BigInteger value = widget.getValue();
if (value!=null) {
option.setValue(value.longValue());
}
});
widget.setColumns(getWidgetColumnCount(option));
return widget;
}
Expand All @@ -104,8 +118,7 @@ private int getWidgetColumnCount(SpinOption option) {
private Component getString(StringOption option) {
final TextWidget text = new TextWidget();
text.setText(option.getValue());
text.setColumns(10);
text.addActionListener(e -> System.out.println(text.getText()));
text.addPropertyChangeListener(TextWidget.TEXT_PROPERTY, e -> option.setValue(text.getText()));
return text;
}
}

0 comments on commit 0281a92

Please sign in to comment.