Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed and support more for the KataGo #554

Merged
merged 2 commits into from
Jul 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions src/main/java/featurecat/lizzie/analysis/Leelaz.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package featurecat.lizzie.analysis;

import featurecat.lizzie.Lizzie;
import featurecat.lizzie.gui.MainFrame;
import featurecat.lizzie.rules.Board;
import featurecat.lizzie.rules.BoardData;
import featurecat.lizzie.rules.Stone;
Expand Down Expand Up @@ -29,8 +30,7 @@
* github.com/gcp/leela-zero
*/
public class Leelaz {
private static final ResourceBundle resourceBundle =
ResourceBundle.getBundle("l10n.DisplayStrings");
private static final ResourceBundle resourceBundle = MainFrame.resourceBundle;

private static final long MINUTE = 60 * 1000; // number of milliseconds in a minute

Expand Down Expand Up @@ -134,7 +134,7 @@ public void startEngine() throws IOException {
commands = splitCommand(engineCommand);

// Get weight name
Pattern wPattern = Pattern.compile("(?s).*?(--weights |-w )([^'\" ]+)(?s).*");
Pattern wPattern = Pattern.compile("(?s).*?(--weights |-w |-model )([^'\" ]+)(?s).*");
Matcher wMatcher = wPattern.matcher(engineCommand);
if (wMatcher.matches() && wMatcher.groupCount() == 2) {
currentWeightFile = wMatcher.group(2);
Expand Down Expand Up @@ -590,6 +590,19 @@ public void boardSize(int width, int height) {
sendCommand("boardsize " + width + (width != height ? " " + height : ""));
}

public void komi(double komi) {
synchronized (this) {
sendCommand("komi " + (komi == 0.0 ? "0" : komi));
bestMoves = new ArrayList<>();
Lizzie.board.getData().tryToClearBestMoves();
if (isPondering) ponder();
}
}

public void handicap(int num) {
Lizzie.leelaz.sendCommand((isKataGo ? "place_free_handicap " : "fixed_handicap ") + num);
}

public void undo() {
synchronized (this) {
sendCommand("undo");
Expand All @@ -598,6 +611,24 @@ public void undo() {
}
}

public void analyzeAvoid(String type, String color, String coordList, int untilMove) {
analyzeAvoid(
String.format("%s %s %s %d", type, color, coordList, untilMove <= 0 ? 1 : untilMove));
}

public void analyzeAvoid(String parameters) {
bestMoves = new ArrayList<>();
if (!isPondering) {
isPondering = true;
startPonderTime = System.currentTimeMillis();
}
sendCommand(
String.format(
"lz-analyze %d %s",
Lizzie.config.config.getJSONObject("leelaz").getInt("analyze-update-interval-centisec"),
parameters));
}

/** This initializes leelaz's pondering mode at its current position */
public void ponder() {
isPondering = true;
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/featurecat/lizzie/gui/BoardPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@

/** The window used to display the game. */
public class BoardPane extends LizziePane {
private static final ResourceBundle resourceBundle =
ResourceBundle.getBundle("l10n.DisplayStrings");
private static final ResourceBundle resourceBundle = MainFrame.resourceBundle;

private static final String[] commands = {
resourceBundle.getString("LizzieFrame.commands.keyN"),
Expand Down Expand Up @@ -112,6 +111,11 @@ public void mousePressed(MouseEvent e) {
Input.undo();
}
}

@Override
public void mouseExited(MouseEvent e) {
onMouseExited(e.getX(), e.getY());
}
});
addMouseMotionListener(
new MouseMotionListener() {
Expand Down Expand Up @@ -342,6 +346,10 @@ public void onDoubleClicked(int x, int y) {
}
}

public void onMouseExited(int x, int y) {
mouseOverCoordinate = outOfBoundCoordinate;
}

public void onMouseMoved(int x, int y) {
mouseOverCoordinate = outOfBoundCoordinate;
Optional<int[]> coords = boardRenderer.convertScreenToCoordinates(x, y);
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/featurecat/lizzie/gui/BoardRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ private void drawLeelazSuggestions(Graphics2D g) {
drawString(
g,
suggestionX,
suggestionY - stoneRadius * 6 / 16,
suggestionY - stoneRadius * 5 / 16,
LizzieFrame.winrateFont,
Font.PLAIN,
text,
Expand All @@ -816,10 +816,10 @@ private void drawLeelazSuggestions(Graphics2D g) {
drawString(
g,
suggestionX,
suggestionY + stoneRadius * 1 / 16,
suggestionY + stoneRadius * 2 / 16,
MainFrame.uiFont,
Utils.getPlayoutsString(move.playouts),
(float) (stoneRadius * 0.8),
(float) (stoneRadius * 0.7),
stoneRadius * 1.4);
double score = move.scoreMean;
if (Lizzie.board.getHistory().isBlacksTurn()) {
Expand All @@ -837,7 +837,7 @@ private void drawLeelazSuggestions(Graphics2D g) {
drawString(
g,
suggestionX,
suggestionY + stoneRadius * 12 / 16,
suggestionY + stoneRadius * 11 / 16,
LizzieFrame.uiFont,
String.format("%.1f", score),
(float) (stoneRadius * 0.75),
Expand Down
25 changes: 2 additions & 23 deletions src/main/java/featurecat/lizzie/gui/ChangeMoveDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import featurecat.lizzie.Lizzie;
import featurecat.lizzie.rules.Board;
import featurecat.lizzie.util.DigitOnlyFilter;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
Expand All @@ -22,13 +23,11 @@
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.InternationalFormatter;

public class ChangeMoveDialog extends JDialog {
public final ResourceBundle resourceBundle = ResourceBundle.getBundle("l10n.DisplayStrings");
public final ResourceBundle resourceBundle = MainFrame.resourceBundle;
private JRadioButton rdoChangeCoord;
private JRadioButton rdoPass;
private JRadioButton rdoSwap;
Expand Down Expand Up @@ -159,26 +158,6 @@ private Integer txtFieldValue(JTextField txt) {
}
}

private class DigitOnlyFilter extends DocumentFilter {
@Override
public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr)
throws BadLocationException {
String newStr = string != null ? string.replaceAll("\\D++", "") : "";
if (!newStr.isEmpty()) {
fb.insertString(offset, newStr, attr);
}
}

@Override
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
throws BadLocationException {
String newStr = text != null ? text.replaceAll("\\D++", "") : "";
if (!newStr.isEmpty()) {
fb.replace(offset, length, newStr, attrs);
}
}
}

private String getChangeToType() {
if (rdoPass.isSelected()) {
return "pass";
Expand Down
58 changes: 5 additions & 53 deletions src/main/java/featurecat/lizzie/gui/ConfigDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import featurecat.lizzie.Lizzie;
import featurecat.lizzie.rules.Board;
import featurecat.lizzie.theme.Theme;
import featurecat.lizzie.util.DigitOnlyFilter;
import featurecat.lizzie.util.Utils;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
Expand Down Expand Up @@ -89,15 +91,13 @@
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.InternationalFormatter;
import org.json.JSONArray;
import org.json.JSONObject;

public class ConfigDialog extends JDialog {
public final ResourceBundle resourceBundle = ResourceBundle.getBundle("l10n.DisplayStrings");
public final ResourceBundle resourceBundle = MainFrame.resourceBundle;

public String enginePath = "";
public String weightPath = "";
Expand Down Expand Up @@ -868,7 +868,7 @@ protected DocumentFilter getDocumentFilter() {
return filter;
}

private DocumentFilter filter = new NumericFilter();
private DocumentFilter filter = new DigitOnlyFilter("[^0-9\\.]++");
});
txtMinPlayoutRatioForStats.setColumns(10);
txtMinPlayoutRatioForStats.setBounds(171, 60, 57, 26);
Expand Down Expand Up @@ -1716,54 +1716,6 @@ private Integer txtFieldIntValue(JTextField txt) {
}
}

private Double txtFieldDoubleValue(JTextField txt) {
if (txt.getText().trim().isEmpty()) {
return 0.0;
} else {
return new Double(txt.getText().trim());
}
}

private class DigitOnlyFilter extends DocumentFilter {
@Override
public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr)
throws BadLocationException {
String newStr = string != null ? string.replaceAll("\\D++", "") : "";
if (!newStr.isEmpty()) {
fb.insertString(offset, newStr, attr);
}
}

@Override
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
throws BadLocationException {
String newStr = text != null ? text.replaceAll("\\D++", "") : "";
if (!newStr.isEmpty()) {
fb.replace(offset, length, newStr, attrs);
}
}
}

private class NumericFilter extends DocumentFilter {
@Override
public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr)
throws BadLocationException {
String newStr = string != null ? string.replaceAll("[^0-9\\.]++", "") : "";
if (!newStr.isEmpty()) {
fb.insertString(offset, newStr, attr);
}
}

@Override
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
throws BadLocationException {
String newStr = text != null ? text.replaceAll("[^0-9\\.]++", "") : "";
if (!newStr.isEmpty()) {
fb.replace(offset, length, newStr, attrs);
}
}
}

private class FontComboBoxRenderer<E> extends JLabel implements ListCellRenderer<E> {
@Override
public Component getListCellRendererComponent(
Expand Down Expand Up @@ -2289,7 +2241,7 @@ private void saveConfig() {
Lizzie.config.uiConfig.put("board-width", size[0]);
Lizzie.config.uiConfig.put("board-height", size[1]);
Lizzie.config.uiConfig.putOpt("panel-ui", chkPanelUI.isSelected());
Lizzie.config.minPlayoutRatioForStats = txtFieldDoubleValue(txtMinPlayoutRatioForStats);
Lizzie.config.minPlayoutRatioForStats = Utils.txtFieldDoubleValue(txtMinPlayoutRatioForStats);
Lizzie.config.uiConfig.put(
"min-playout-ratio-for-stats", Lizzie.config.minPlayoutRatioForStats);
Lizzie.config.uiConfig.putOpt("show-coordinates", chkShowCoordinates.isSelected());
Expand Down
23 changes: 19 additions & 4 deletions src/main/java/featurecat/lizzie/gui/GameInfoDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,24 @@

package featurecat.lizzie.gui;

import featurecat.lizzie.Lizzie;
import featurecat.lizzie.analysis.GameInfo;
import java.awt.*;
import featurecat.lizzie.util.Utils;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.text.DecimalFormat;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

/** @author unknown */
public class GameInfoDialog extends JDialog {
Expand Down Expand Up @@ -72,7 +85,7 @@ private void initContentPanel() {
// read-only
textFieldKomi = new JFormattedTextField(FORMAT_KOMI);
textFieldHandicap = new JFormattedTextField(FORMAT_HANDICAP);
textFieldKomi.setEditable(false);
textFieldKomi.setEditable(true);
textFieldHandicap.setEditable(false);

contentPanel.add(new JLabel("Black"));
Expand Down Expand Up @@ -132,6 +145,8 @@ public void apply() {
// apply new values
gameInfo.setPlayerBlack(playerBlack);
gameInfo.setPlayerWhite(playerWhite);
gameInfo.setKomi(Utils.txtFieldDoubleValue(textFieldKomi));
if (Lizzie.leelaz != null) Lizzie.leelaz.komi(gameInfo.getKomi());

// close window
setVisible(false);
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/featurecat/lizzie/gui/GtpConsolePane.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
import org.json.JSONArray;

public class GtpConsolePane extends JDialog {
private static final ResourceBundle resourceBundle =
ResourceBundle.getBundle("l10n.DisplayStrings");
private static final ResourceBundle resourceBundle = MainFrame.resourceBundle;

// Display Comment
private HTMLDocument htmlDoc;
Expand Down Expand Up @@ -188,6 +187,15 @@ private void postCommand(ActionEvent e) {
}
Lizzie.board.reopen(width, height);
}
} else if (command.startsWith("komi")) {
String cmdParams[] = command.split(" ");
if (cmdParams.length >= 2) {
try {
double komi = Double.parseDouble(cmdParams[1]);
Lizzie.leelaz.komi(komi);
} catch (Exception ex) {
}
}
} else {
Lizzie.leelaz.sendCommand(command);
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/featurecat/lizzie/gui/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,10 @@ public void keyPressed(KeyEvent e) {

case VK_I:
// stop the ponder
if (Lizzie.leelaz.isPondering()) Lizzie.leelaz.togglePonder();
boolean isPondering = Lizzie.leelaz.isPondering();
if (isPondering) Lizzie.leelaz.togglePonder();
Lizzie.frame.editGameInfo();
if (isPondering) Lizzie.leelaz.togglePonder();
break;
case VK_S:
// stop the ponder
Expand Down
Loading