Skip to content

Commit

Permalink
Fix the code by review
Browse files Browse the repository at this point in the history
  • Loading branch information
zsalch authored and zsalch committed Oct 2, 2018
1 parent 312b80d commit b7ae8dc
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/main/java/featurecat/lizzie/Lizzie.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public static void shutdown() {
}

/**
* Switch the Engine
* Switch the Engine by index number
* @param index engine index
*/
public static void switchEngine(int index) {
Expand All @@ -114,7 +114,7 @@ public static void switchEngine(int index) {
index = -1;
}
}
if (index < 0 || commandLine == null || commandLine.trim().length() == 0 || index == Lizzie.leelaz.currentEngineNo()) {
if (index < 0 || commandLine == null || commandLine.trim().isEmpty() || index == Lizzie.leelaz.currentEngineN()) {
return;
}

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/featurecat/lizzie/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,20 @@ public static void saveAsFile(URL url, File file) {
}

/**
* Fit the too long text by width
/**
* Truncate text that is too long for the given width
*
* @param line
* @param fm
* @param fitWidth
* @return fitted
*/
public static String fitStringByWidth(String line, FontMetrics fm, int fitWidth) {
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) {
if (width > fitWidth) {
int guess = line.length() * fitWidth / width;
String before = line.substring(0, guess).trim();
width = fm.stringWidth(before);
Expand Down
27 changes: 13 additions & 14 deletions src/main/java/featurecat/lizzie/analysis/Leelaz.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public class Leelaz {
private List<String> commands = null;
private JSONObject config = null;
private String currentWeight = null;
private String isSwitching = "";
private int currentEngineNo = -1;
private boolean switching = false;
private int currentEngineN = -1;
private ScheduledExecutorService executor = null;

// dynamic komi and opponent komi as reported by dynamic-komi version of leelaz
Expand Down Expand Up @@ -106,15 +106,15 @@ public Leelaz() throws IOException, JSONException {
// substitute in the weights file
engineCommand = engineCommand.replaceAll("%network-file", config.getString("network-file"));

// Init current engine no and start engine
currentEngineNo = 0;
// Initialize current engine number and start engine
currentEngineN = 0;
startEngine(engineCommand);
Lizzie.frame.refreshBackground();
}

public void startEngine(String engineCommand) throws IOException {
// Check engine command
if (engineCommand == null || "".equals(engineCommand.trim())) {
if (engineCommand == null || engineCommand.trim().isEmpty()) {
return;
}

Expand Down Expand Up @@ -168,20 +168,19 @@ public void startEngine(String engineCommand) throws IOException {
executor.execute(this::read);
}


public void restartEngine(String engineCommand, int index) throws IOException {
if (engineCommand == null || "".equals(engineCommand.trim())) {
if (engineCommand == null || engineCommand.trim().isEmpty()) {
return;
}
isSwitching = " Switching";
switching = true;
this.engineCommand = engineCommand;
// stop the ponder
if (Lizzie.leelaz.isPondering()) {
Lizzie.leelaz.togglePonder();
}
normalQuit();
startEngine(engineCommand);
currentEngineNo = index;
currentEngineN = index;
togglePonder();
}

Expand Down Expand Up @@ -285,7 +284,7 @@ else if (line.equals("\n")) {
} else if (line.startsWith("info")) {
isLoaded = true;
// Clear switching prompt
isSwitching = "";
switching = false;
// Display engine command in the title
if (Lizzie.frame != null) Lizzie.frame.setEngineTitle(this.engineCommand);
if (isResponseUpToDate()) {
Expand Down Expand Up @@ -651,12 +650,12 @@ public String currentWeight() {
return currentWeight;
}

public String isSwitching() {
return isSwitching;
public boolean switching() {
return switching;
}

public int currentEngineNo() {
return currentEngineNo;
public int currentEngineN() {
return currentEngineN;
}

public String engineCommand() {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/featurecat/lizzie/gui/LizzieFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,8 @@ public void paint(Graphics g0) {
if (Lizzie.config.showStatus) {
// Display switching prompt
drawPonderingState(g, resourceBundle.getString("LizzieFrame.display.pondering") +
(Lizzie.leelaz.isPondering()?resourceBundle.getString("LizzieFrame.display.on"):resourceBundle.getString("LizzieFrame.display.off")) + " " + Lizzie.leelaz.currentWeight() + Lizzie.leelaz.isSwitching(),
(Lizzie.leelaz.isPondering()?resourceBundle.getString("LizzieFrame.display.on"):resourceBundle.getString("LizzieFrame.display.off")) +
" " + Lizzie.leelaz.currentWeight() + (Lizzie.leelaz.switching() ? resourceBundle.getString("LizzieFrame.prompt.switching") : ""),
ponderingX, ponderingY, ponderingSize);
}

Expand Down Expand Up @@ -495,11 +496,11 @@ private void drawPonderingState(Graphics2D g, String text, int x, int y, double
Font font = new Font(systemDefaultFontName, Font.PLAIN, (int)(Math.max(getWidth(), getHeight()) * size));
FontMetrics fm = g.getFontMetrics(font);
int stringWidth = fm.stringWidth(text);
// Trim too long text when display switching prompt
// Truncate too long text when display switching prompt
if (Lizzie.leelaz.isLoaded()) {
int mainBoardX = (boardRenderer != null && boardRenderer.getLocation() != null) ? boardRenderer.getLocation().x : 0;
if ((mainBoardX > x) && stringWidth > (mainBoardX - x) ) {
text = Util.fitStringByWidth(text, fm, mainBoardX - x);
text = Util.truncateStringByWidth(text, fm, mainBoardX - x);
stringWidth = fm.stringWidth(text);
}
}
Expand Down
41 changes: 27 additions & 14 deletions src/main/java/featurecat/lizzie/rules/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,8 @@ public boolean nextMove() {

/**
* Goes to the next coordinate, thread safe
* @param fromBackChildren by back children branch
* @return true when has next variation
*/
public boolean nextMove(int fromBackChildren) {
synchronized (this) {
Expand All @@ -449,33 +451,44 @@ public boolean nextMove(int fromBackChildren) {
return nextVariation(fromBackChildren);
}
}
// Save the move number
public BoardHistoryNode saveMoveNumber() {

/**
* Save the move number for restore
* If in the branch, save the back routing from children
*/
public void saveMoveNumber() {
BoardHistoryNode curNode = history.getCurrentHistoryNode();
int curMoveNum = curNode.getData().moveNumber;
if (curMoveNum > 0) {
if (!BoardHistoryList.isMainTrunk(curNode)) {
// If in Branch, save the back routing
// If in branch, save the back routing from children
saveBackRouting(curNode);
}
goToMoveNumber(0);
}
saveNode = curNode;
return curNode;
}
// Save the back routing

/**
* Save the back routing from children
*/
public void saveBackRouting(BoardHistoryNode node) {
if (node != null && node.previous() != null) {
node.previous().setFromBackChildren(node.previous().getNexts().indexOf(node));
saveBackRouting(node.previous());
}
return;
}
// Restore move number

/**
* Restore move number by saved node
*/
public void restoreMoveNumber() {
restoreMoveNumber(saveNode);
}
// Restore move number by node

/**
* Restore move number by node
*/
public void restoreMoveNumber(BoardHistoryNode node) {
if (node == null) {
return;
Expand All @@ -486,14 +499,16 @@ public void restoreMoveNumber(BoardHistoryNode node) {
goToMoveNumber(moveNumber);
} else {
// If in Branch, restore by the back routing
goToMoveNumberByBack(moveNumber);
goToMoveNumberByBackChildren(moveNumber);
}
}
}
// Go to move number by back routing when in branch
public boolean goToMoveNumberByBack(int moveNumber) {

/**
* Go to move number by back routing from children when in branch
*/
public void goToMoveNumberByBackChildren(int moveNumber) {
int delta = moveNumber - history.getMoveNumber();
boolean moved = false;
for (int i = 0; i < Math.abs(delta); i++) {
BoardHistoryNode curNode = history.getCurrentHistoryNode();
if (curNode.numberOfChildren() > 1 && delta > 0) {
Expand All @@ -503,9 +518,7 @@ public boolean goToMoveNumberByBack(int moveNumber) {
break;
}
}
moved = true;
}
return moved;
}

public boolean goToMoveNumber(int moveNumber) {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/l10n/DisplayStrings.properties
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ LizzieFrame.prompt.failedToOpenFile=Failed to open file.
LizzieFrame.prompt.failedToSaveFile=Failed to save file.
LizzieFrame.prompt.sgfExists=The SGF file already exists, do you want to replace it?
LizzieFrame.prompt.showControlsHint=hold x = view controls
LizzieFrame.prompt.switching=switching...
LizzieFrame.display.lastMove=Last move
LizzieFrame.display.pondering=Pondering
LizzieFrame.display.on=on
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/l10n/DisplayStrings_RO.properties
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ LizzieFrame.prompt.failedToOpenSgf=Nu s-a reușit deschiderea fișierului SGF
LizzieFrame.prompt.failedToSaveSgf=Nu s-a reușit salvarea fișierului SGF
LizzieFrame.prompt.sgfExists=Fișierul SGF există deja, doriți să-l înlocuiți?
LizzieFrame.prompt.showControlsHint=x apăsat = afișează comenzi
LizzieFrame.prompt.switching=comutare...
LizzieFrame.display.lastMove=Ulima mutare
LizzieFrame.display.pondering=Analizeză
LizzieFrame.display.on=pornit
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/l10n/DisplayStrings_zh_CN.properties
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ LizzieFrame.prompt.failedToOpenFile=\u4E0D\u80FD\u6253\u5F00SGF\u6587\u4EF6.
LizzieFrame.prompt.failedToSaveFile=\u4E0D\u80FD\u4FDD\u5B58SGF\u6587\u4EF6.
LizzieFrame.prompt.sgfExists=SGF\u6587\u4EF6\u5DF2\u7ECF\u5B58\u5728, \u9700\u8981\u66FF\u6362\u5417?
LizzieFrame.prompt.showControlsHint=\u6309\u4F4FX\u4E0D\u653E\u67E5\u770B\u5FEB\u6377\u952E\u63D0\u793A
LizzieFrame.prompt.switching=\u5207\u6362\u4E2D...
LizzieFrame.display.lastMove=\u6700\u540E\u4E00\u624B
LizzieFrame.display.pondering=\u5206\u6790
LizzieFrame.display.on=\u5F00\u542F
Expand Down

0 comments on commit b7ae8dc

Please sign in to comment.