DroidFish: Code cleanup.

This commit is contained in:
Peter Osterlund
2011-12-28 15:53:27 +00:00
parent 99580477bd
commit 0ca0a40324
2 changed files with 42 additions and 40 deletions

View File

@@ -66,6 +66,8 @@ public final class DroidBook {
private BookOptions options = null; private BookOptions options = null;
private static final DroidBook INSTANCE = new DroidBook(); private static final DroidBook INSTANCE = new DroidBook();
/** Get singleton instance. */
public static DroidBook getInstance() { public static DroidBook getInstance() {
return INSTANCE; return INSTANCE;
} }
@@ -75,7 +77,7 @@ public final class DroidBook {
} }
/** Set opening book options. */ /** Set opening book options. */
public final void setOptions(BookOptions options) { public final synchronized void setOptions(BookOptions options) {
this.options = options; this.options = options;
if (CtgBook.canHandle(options)) if (CtgBook.canHandle(options))
externalBook = new CtgBook(); externalBook = new CtgBook();
@@ -88,7 +90,7 @@ public final class DroidBook {
} }
/** Return a random book move for a position, or null if out of book. */ /** Return a random book move for a position, or null if out of book. */
public final Move getBookMove(Position pos) { public final synchronized Move getBookMove(Position pos) {
if ((options != null) && (pos.fullMoveCounter > options.maxLength)) if ((options != null) && (pos.fullMoveCounter > options.maxLength))
return null; return null;
List<BookEntry> bookMoves = getBook().getBookEntries(pos); List<BookEntry> bookMoves = getBook().getBookEntries(pos);
@@ -121,24 +123,8 @@ public final class DroidBook {
return bookMoves.get(nMoves-1).move; return bookMoves.get(nMoves-1).move;
} }
private final double scaleWeight(double w) {
if (w <= 0)
return 0;
if (options == null)
return w;
return Math.pow(w, Math.exp(-options.random));
}
final private IOpeningBook getBook() {
if (externalBook.enabled()) {
return externalBook;
} else {
return internalBook;
}
}
/** Return all book moves, both as a formatted string and as a list of moves. */ /** Return all book moves, both as a formatted string and as a list of moves. */
public final Pair<String,ArrayList<Move>> getAllBookMoves(Position pos) { public final synchronized Pair<String,ArrayList<Move>> getAllBookMoves(Position pos) {
StringBuilder ret = new StringBuilder(); StringBuilder ret = new StringBuilder();
ArrayList<Move> bookMoveList = new ArrayList<Move>(); ArrayList<Move> bookMoveList = new ArrayList<Move>();
List<BookEntry> bookMoves = getBook().getBookEntries(pos); List<BookEntry> bookMoves = getBook().getBookEntries(pos);
@@ -184,6 +170,22 @@ public final class DroidBook {
return new Pair<String, ArrayList<Move>>(ret.toString(), bookMoveList); return new Pair<String, ArrayList<Move>>(ret.toString(), bookMoveList);
} }
private final double scaleWeight(double w) {
if (w <= 0)
return 0;
if (options == null)
return w;
return Math.pow(w, Math.exp(-options.random));
}
private final IOpeningBook getBook() {
if (externalBook.enabled()) {
return externalBook;
} else {
return internalBook;
}
}
/** Creates the book.bin file. */ /** Creates the book.bin file. */
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
List<Byte> binBook = createBinBook(); List<Byte> binBook = createBinBook();
@@ -196,7 +198,7 @@ public final class DroidBook {
out.close(); out.close();
} }
public static List<Byte> createBinBook() { private static final List<Byte> createBinBook() {
List<Byte> binBook = new ArrayList<Byte>(0); List<Byte> binBook = new ArrayList<Byte>(0);
try { try {
InputStream inStream = new Object().getClass().getResourceAsStream("/book.txt"); InputStream inStream = new Object().getClass().getResourceAsStream("/book.txt");
@@ -225,7 +227,7 @@ public final class DroidBook {
} }
/** Add a sequence of moves, starting from the initial position, to the binary opening book. */ /** Add a sequence of moves, starting from the initial position, to the binary opening book. */
private static boolean addBookLine(String line, List<Byte> binBook) throws ChessParseError { private static final boolean addBookLine(String line, List<Byte> binBook) throws ChessParseError {
Position pos = TextIO.readFEN(TextIO.startPosFEN); Position pos = TextIO.readFEN(TextIO.startPosFEN);
UndoInfo ui = new UndoInfo(); UndoInfo ui = new UndoInfo();
String[] strMoves = line.split(" "); String[] strMoves = line.split(" ");
@@ -251,7 +253,7 @@ public final class DroidBook {
return true; return true;
} }
private static int pieceToProm(int p) { private static final int pieceToProm(int p) {
switch (p) { switch (p) {
case Piece.WQUEEN: case Piece.BQUEEN: case Piece.WQUEEN: case Piece.BQUEEN:
return 1; return 1;

View File

@@ -101,29 +101,18 @@ public class DroidComputerPlayer {
} }
/** Get engine reported name, including strength setting. */ /** Get engine reported name, including strength setting. */
public synchronized String getEngineName() { public final synchronized String getEngineName() {
String ret = engineName; String ret = engineName;
if (strength < 1000) if (strength < 1000)
ret += String.format(" (%.1f%%)", strength * 0.1); ret += String.format(" (%.1f%%)", strength * 0.1);
return ret; return ret;
} }
/** Clear transposition table. */ /** Clear transposition table. Takes effect when next search started. */
public final void clearTT() { public final synchronized void clearTT() {
newGame = true; newGame = true;
} }
/** Sends "ucinewgame" to engine if clearTT() has previously been called. */
public final void maybeNewGame() {
if (newGame) {
newGame = false;
if (uciEngine != null) {
uciEngine.writeLineToEngine("ucinewgame");
syncReady();
}
}
}
/** Sends "ponderhit" command to engine. */ /** Sends "ponderhit" command to engine. */
public final synchronized void ponderHit(Position pos, Move ponderMove) { public final synchronized void ponderHit(Position pos, Move ponderMove) {
havePonderHit = true; havePonderHit = true;
@@ -320,7 +309,18 @@ public class DroidComputerPlayer {
} }
} }
private static int getNumCPUs() { /** Sends "ucinewgame" to engine if clearTT() has previously been called. */
private final void maybeNewGame() {
if (newGame) {
newGame = false;
if (uciEngine != null) {
uciEngine.writeLineToEngine("ucinewgame");
syncReady();
}
}
}
private static final int getNumCPUs() {
int nCPUsFromProc = 1; int nCPUsFromProc = 1;
try { try {
FileReader fr = new FileReader("/proc/stat"); FileReader fr = new FileReader("/proc/stat");
@@ -428,7 +428,7 @@ public class DroidComputerPlayer {
* @param move The move that may have to be made before claiming draw. * @param move The move that may have to be made before claiming draw.
* @return The draw string that claims the draw, or empty string if draw claim not valid. * @return The draw string that claims the draw, or empty string if draw claim not valid.
*/ */
private String canClaimDraw(Position pos, long[] posHashList, int posHashListSize, Move move) { private final String canClaimDraw(Position pos, long[] posHashList, int posHashListSize, Move move) {
String drawStr = ""; String drawStr = "";
if (canClaimDraw50(pos)) { if (canClaimDraw50(pos)) {
drawStr = "draw 50"; drawStr = "draw 50";