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

Remove all nulls #402

Merged
merged 4 commits into from
Oct 30, 2018
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
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);
zsalch marked this conversation as resolved.
Show resolved Hide resolved
}
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