Skip to content

Commit

Permalink
Remove all nulls (#402)
Browse files Browse the repository at this point in the history
* Remove all nulls

Refactors the entire code base to remove all usages for null as a
values, and all null checks. When needed, data structures are updated to
use java.util.Optional, but in a lot of cases null are removed without
replacement.
Reimplement nextBranch and previousBranch
Minor graphical adjustment
Fix the Lizzie window not actually starting centered.
Remove the 0.98 factor on board size to remove the small region of
background above the board.
Fix an exception when creating a BufferedImage with width zero.
  • Loading branch information
OlivierBlanvillain authored and zsalch committed Oct 30, 2018
1 parent adf7cac commit a510502
Show file tree
Hide file tree
Showing 16 changed files with 845 additions and 919 deletions.
29 changes: 9 additions & 20 deletions src/main/java/featurecat/lizzie/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,8 @@ private JSONObject loadAndMergeConfig(

FileInputStream fp = new FileInputStream(file);

JSONObject mergedcfg = null;
boolean modified = false;
try {
mergedcfg = new JSONObject(new JSONTokener(fp));
modified = merge_defaults(mergedcfg, defaultCfg);
} catch (JSONException e) {
mergedcfg = null;
e.printStackTrace();
}
JSONObject mergedcfg = new JSONObject(new JSONTokener(fp));
boolean modified = mergeDefaults(mergedcfg, defaultCfg);

fp.close();

Expand All @@ -105,10 +98,6 @@ private JSONObject loadAndMergeConfig(
* @return if any correction has been made.
*/
private boolean validateAndCorrectSettings(JSONObject config) {
if (config == null) {
return false;
}

boolean madeCorrections = false;

// Check ui configs
Expand Down Expand Up @@ -185,22 +174,22 @@ public Config() throws IOException {

// Modifies config by adding in values from default_config that are missing.
// Returns whether it added anything.
public boolean merge_defaults(JSONObject config, JSONObject defaults_config) {
public boolean mergeDefaults(JSONObject config, JSONObject defaultsConfig) {
boolean modified = false;
Iterator<String> keys = defaults_config.keys();
Iterator<String> keys = defaultsConfig.keys();
while (keys.hasNext()) {
String key = keys.next();
Object new_val = defaults_config.get(key);
if (new_val instanceof JSONObject) {
Object newVal = defaultsConfig.get(key);
if (newVal instanceof JSONObject) {
if (!config.has(key)) {
config.put(key, new JSONObject());
modified = true;
}
Object old_val = config.get(key);
modified |= merge_defaults((JSONObject) old_val, (JSONObject) new_val);
Object oldVal = config.get(key);
modified |= mergeDefaults((JSONObject) oldVal, (JSONObject) newVal);
} else {
if (!config.has(key)) {
config.put(key, new_val);
config.put(key, newVal);
modified = true;
}
}
Expand Down
64 changes: 24 additions & 40 deletions src/main/java/featurecat/lizzie/Lizzie.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

/** Main class. */
public class Lizzie {
public static Config config;
public static LizzieFrame frame;
public static Leelaz leelaz;
public static Board board;
public static Config config;
public static Leelaz leelaz;
public static String lizzieVersion = "0.5";
private static String[] mainArgs;

Expand All @@ -24,25 +24,17 @@ public static void main(String[] args) throws IOException {
config = new Config();
board = new Board();
frame = new LizzieFrame();
new Thread(Lizzie::run).start();
}
leelaz = new Leelaz();

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

public static void setLookAndFeel() {
Expand All @@ -60,25 +52,23 @@ public static void setLookAndFeel() {
}

public static void shutdown() {
if (board != null && config.config.getJSONObject("ui").getBoolean("confirm-exit")) {
if (config.config.getJSONObject("ui").getBoolean("confirm-exit")) {
int ret =
JOptionPane.showConfirmDialog(
null, "Do you want to save this SGF?", "Save SGF?", JOptionPane.OK_CANCEL_OPTION);
if (ret == JOptionPane.OK_OPTION) {
LizzieFrame.saveFile();
}
}
if (board != null) {
board.autosaveToMemory();
}
board.autosaveToMemory();

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

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

Expand All @@ -88,33 +78,27 @@ public static void shutdown() {
* @param index engine index
*/
public static void switchEngine(int index) {

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

// Workaround for leelaz cannot exit when restarting
// Workaround for leelaz no exiting 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.togglePonder(); // Toggle twice for to restart pondering
Lizzie.leelaz.isThinking = false;
}
Lizzie.leelaz.togglePonder();
Expand Down
36 changes: 17 additions & 19 deletions src/main/java/featurecat/lizzie/analysis/Branch.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,39 @@
import featurecat.lizzie.rules.Board;
import featurecat.lizzie.rules.BoardData;
import featurecat.lizzie.rules.Stone;
import featurecat.lizzie.rules.Zobrist;
import java.util.List;
import java.util.Optional;

public class Branch {
public BoardData data;

public Branch(Board board, List<String> variation) {
int moveNumber = 0;
int[] lastMove = board.getLastMove();
int[] moveNumberList = new int[Board.boardSize * Board.boardSize];
boolean blackToPlay = board.getData().blackToPlay;

Stone lastMoveColor = board.getData().lastMoveColor;
Stone[] stones = board.getStones().clone();
Zobrist zobrist = board.getData().zobrist == null ? null : board.getData().zobrist.clone();
int moveNumber = 0;
double winrate = 0.0;
int playouts = 0;

// Dont care about winrate for branch
this.data =
new BoardData(
stones,
lastMove,
lastMoveColor,
blackToPlay,
zobrist,
board.getStones().clone(),
board.getLastMove(),
board.getData().lastMoveColor,
board.getData().blackToPlay,
board.getData().zobrist.clone(),
moveNumber,
moveNumberList,
board.getData().blackCaptures,
board.getData().whiteCaptures,
0.0,
0);
winrate,
playouts);

for (int i = 0; i < variation.size(); i++) {
int[] coord = Board.convertNameToCoordinates(variation.get(i));
if (coord == null) break;
data.lastMove = coord;
Optional<int[]> coordOpt = Board.asCoordinates(variation.get(i));
if (!coordOpt.isPresent()) {
break;
}
int[] coord = coordOpt.get();
data.lastMove = coordOpt;
data.stones[Board.getIndex(coord[0], coord[1])] =
data.blackToPlay ? Stone.BLACK_GHOST : Stone.WHITE_GHOST;
data.moveNumberList[Board.getIndex(coord[0], coord[1])] = i + 1;
Expand Down
Loading

0 comments on commit a510502

Please sign in to comment.