Skip to content

Commit

Permalink
Merge branch 'next' into next_sgfprops
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierBlanvillain committed Oct 7, 2018
2 parents 99c369d + 10875ca commit 60a86f4
Show file tree
Hide file tree
Showing 23 changed files with 737 additions and 559 deletions.
115 changes: 0 additions & 115 deletions doc/plugin.md

This file was deleted.

19 changes: 0 additions & 19 deletions doc/theme.md

This file was deleted.

19 changes: 7 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,6 @@
<useFile>false</useFile>
</configuration>
</plugin>
<plugin>
<groupId>com.coveo</groupId>
<artifactId>fmt-maven-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<goals>
<goal>format</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand All @@ -94,5 +82,12 @@
<version>4.11</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/com.jhlabs/filters -->
<dependency>
<groupId>com.jhlabs</groupId>
<artifactId>filters</artifactId>
<version>2.0.235</version>
</dependency>
</dependencies>
</project>
9 changes: 8 additions & 1 deletion src/main/java/featurecat/lizzie/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class Config {
public boolean showMoveNumber = false;
public boolean showWinrate = true;
public boolean showVariationGraph = true;
public boolean showComment = false;
public int commentFontSize = 0;
public boolean showRawBoard = false;
public boolean showCaptured = true;
public boolean handicapInsteadOfWinrate = false;
Expand Down Expand Up @@ -130,6 +132,8 @@ public Config() throws IOException {
showBranch = uiConfig.getBoolean("show-leelaz-variation");
showWinrate = uiConfig.getBoolean("show-winrate");
showVariationGraph = uiConfig.getBoolean("show-variation-graph");
showComment = uiConfig.optBoolean("show-comment", false);
commentFontSize = uiConfig.optInt("comment-font-size", 0);
showCaptured = uiConfig.getBoolean("show-captured");
showBestMoves = uiConfig.getBoolean("show-best-moves");
showNextMoves = uiConfig.getBoolean("show-next-moves");
Expand Down Expand Up @@ -181,6 +185,10 @@ public void toggleShowVariationGraph() {
this.showVariationGraph = !this.showVariationGraph;
}

public void toggleShowComment() {
this.showComment = !this.showComment;
}

public void toggleShowBestMoves() {
this.showBestMoves = !this.showBestMoves;
}
Expand Down Expand Up @@ -249,7 +257,6 @@ private JSONObject createDefaultConfig() {
JSONObject ui = new JSONObject();

ui.put("board-color", new JSONArray("[217, 152, 77]"));
ui.put("theme", "DefaultTheme");
ui.put("shadows-enabled", true);
ui.put("fancy-stones", true);
ui.put("fancy-board", true);
Expand Down
114 changes: 86 additions & 28 deletions src/main/java/featurecat/lizzie/Lizzie.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import featurecat.lizzie.analysis.Leelaz;
import featurecat.lizzie.gui.LizzieFrame;
import featurecat.lizzie.plugin.PluginManager;
import featurecat.lizzie.rules.Board;
import java.io.File;
import java.io.IOException;
import javax.swing.*;
import org.json.JSONArray;
import org.json.JSONException;

/** Main class. */
Expand All @@ -16,40 +16,51 @@ public class Lizzie {
public static Board board;
public static Config config;
public static String lizzieVersion = "0.5";
private static String[] args;

/** Launches the game window, and runs the game. */
public static void main(String[] args)
throws IOException, JSONException, ClassNotFoundException, UnsupportedLookAndFeelException,
InstantiationException, IllegalAccessException, InterruptedException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
public static void main(String[] args) throws IOException {
setLookAndFeel();
args = args;
config = new Config();
PluginManager.loadPlugins();
board = new Board();
frame = new LizzieFrame();
new Thread(Lizzie::run).start();
}

new Thread(
() -> {
try {
leelaz = new Leelaz();
if (config.handicapInsteadOfWinrate) {
leelaz.estimatePassWinrate();
}
if (args.length == 1) {
frame.loadFile(new File(args[0]));
} else if (config.config.getJSONObject("ui").getBoolean("resume-previous-game")) {
board.resumePreviousGame();
}
leelaz.togglePonder();
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
})
.start();
public static void run() {
try {
leelaz = new Leelaz();
if (config.handicapInsteadOfWinrate) {
leelaz.estimatePassWinrate();
}
if (args.length == 1) {
frame.loadFile(new File(args[0]));
} else if (config.config.getJSONObject("ui").getBoolean("resume-previous-game")) {
board.resumePreviousGame();
}
leelaz.togglePonder();
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
}

public static void setLookAndFeel() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}

public static void shutdown() {
PluginManager.onShutdown();
if (board != null && config.config.getJSONObject("ui").getBoolean("confirm-exit")) {
int ret =
JOptionPane.showConfirmDialog(
Expand All @@ -64,11 +75,58 @@ public static void shutdown() {

try {
config.persist();
} catch (IOException err) {
// Failed to save config
} catch (IOException e) {
e.printStackTrace(); // Failed to save config
}

if (leelaz != null) leelaz.shutdown();
System.exit(0);
}

/**
* Switch the Engine by index number
*
* @param index engine index
*/
public static void switchEngine(int index) {

String commandLine = null;
if (index == 0) {
commandLine = Lizzie.config.leelazConfig.getString("engine-command");
commandLine =
commandLine.replaceAll(
"%network-file", Lizzie.config.leelazConfig.getString("network-file"));
} else {
JSONArray commandList = Lizzie.config.leelazConfig.getJSONArray("engine-command-list");
if (commandList != null && commandList.length() >= index) {
commandLine = commandList.getString(index - 1);
} else {
index = -1;
}
}
if (index < 0
|| commandLine == null
|| commandLine.trim().isEmpty()
|| index == Lizzie.leelaz.currentEngineN()) {
return;
}

// Workaround for leelaz cannot exit when restarting
if (leelaz.isThinking) {
if (Lizzie.frame.isPlayingAgainstLeelaz) {
Lizzie.frame.isPlayingAgainstLeelaz = false;
Lizzie.leelaz.togglePonder(); // we must toggle twice for it to restart pondering
Lizzie.leelaz.isThinking = false;
}
Lizzie.leelaz.togglePonder();
}

board.saveMoveNumber();
try {
leelaz.restartEngine(commandLine, index);
board.restoreMoveNumber();
} catch (IOException e) {
e.printStackTrace();
}
}
}
33 changes: 33 additions & 0 deletions src/main/java/featurecat/lizzie/Util.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package featurecat.lizzie;

import java.awt.FontMetrics;
import java.io.*;
import java.net.URL;
import java.nio.channels.Channels;
Expand Down Expand Up @@ -74,4 +75,36 @@ public static void saveAsFile(URL url, File file) {
e.printStackTrace();
}
}

/**
* Truncate text that is too long for the given width
*
* @param line
* @param fm
* @param fitWidth
* @return fitted
*/
public static String truncateStringByWidth(String line, FontMetrics fm, int fitWidth) {
if (line == null || line.length() == 0) {
return "";
}
int width = fm.stringWidth(line);
if (width > fitWidth) {
int guess = line.length() * fitWidth / width;
String before = line.substring(0, guess).trim();
width = fm.stringWidth(before);
if (width > fitWidth) {
int diff = width - fitWidth;
int i = 0;
for (; (diff > 0 && i < 5); i++) {
diff = diff - fm.stringWidth(line.substring(guess - i - 1, guess - i));
}
return line.substring(0, guess - i).trim();
} else {
return before;
}
} else {
return line;
}
}
}
Loading

0 comments on commit 60a86f4

Please sign in to comment.