From 0281a92840d24674351918234b4e413dc01eddfd Mon Sep 17 00:00:00 2001 From: Fathzer Date: Tue, 13 Feb 2024 20:09:42 +0100 Subject: [PATCH] Fixes bugs --- .../com/fathzer/jchess/bot/uci/UCIEngine.java | 18 +++++------ .../swing/settings/engine/EnginePanel.java | 31 +++++++++++++------ 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/fathzer/jchess/bot/uci/UCIEngine.java b/src/main/java/com/fathzer/jchess/bot/uci/UCIEngine.java index af1a366..2d23a32 100644 --- a/src/main/java/com/fathzer/jchess/bot/uci/UCIEngine.java +++ b/src/main/java/com/fathzer/jchess/bot/uci/UCIEngine.java @@ -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; @@ -44,13 +44,15 @@ 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) { @@ -58,7 +60,7 @@ private String init() throws IOException { } final String namePrefix = "id name "; if (line.startsWith(namePrefix)) { - result = line.substring(namePrefix.length()); + this.name = line.substring(namePrefix.length()); } else { final Optional> ooption = parseOption(line); if (ooption.isPresent()) { @@ -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> parseOption(String line) throws IOException { @@ -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); } diff --git a/src/main/java/com/fathzer/jchess/swing/settings/engine/EnginePanel.java b/src/main/java/com/fathzer/jchess/swing/settings/engine/EnginePanel.java index 8c86187..33aa1c7 100644 --- a/src/main/java/com/fathzer/jchess/swing/settings/engine/EnginePanel.java +++ b/src/main/java/com/fathzer/jchess/swing/settings/engine/EnginePanel.java @@ -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; @@ -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; @@ -31,6 +33,7 @@ public class EnginePanel extends JPanel { */ public EnginePanel(List> options) { setLayout(new GridBagLayout()); + setBackground(Color.blue); if (options==null) { options = Collections.emptyList(); } @@ -47,28 +50,34 @@ public EnginePanel(List> 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) { @@ -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; } @@ -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; } }