mirror of
https://github.com/peterosterlund2/droidfish.git
synced 2025-01-31 01:20:46 +01:00
DroidFish: Code cleanup.
This commit is contained in:
parent
99580477bd
commit
0ca0a40324
|
@ -66,6 +66,8 @@ public final class DroidBook {
|
|||
private BookOptions options = null;
|
||||
|
||||
private static final DroidBook INSTANCE = new DroidBook();
|
||||
|
||||
/** Get singleton instance. */
|
||||
public static DroidBook getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
@ -75,7 +77,7 @@ public final class DroidBook {
|
|||
}
|
||||
|
||||
/** Set opening book options. */
|
||||
public final void setOptions(BookOptions options) {
|
||||
public final synchronized void setOptions(BookOptions options) {
|
||||
this.options = options;
|
||||
if (CtgBook.canHandle(options))
|
||||
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. */
|
||||
public final Move getBookMove(Position pos) {
|
||||
public final synchronized Move getBookMove(Position pos) {
|
||||
if ((options != null) && (pos.fullMoveCounter > options.maxLength))
|
||||
return null;
|
||||
List<BookEntry> bookMoves = getBook().getBookEntries(pos);
|
||||
|
@ -121,28 +123,12 @@ public final class DroidBook {
|
|||
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. */
|
||||
public final Pair<String,ArrayList<Move>> getAllBookMoves(Position pos) {
|
||||
public final synchronized Pair<String,ArrayList<Move>> getAllBookMoves(Position pos) {
|
||||
StringBuilder ret = new StringBuilder();
|
||||
ArrayList<Move> bookMoveList = new ArrayList<Move>();
|
||||
List<BookEntry> bookMoves = getBook().getBookEntries(pos);
|
||||
|
||||
|
||||
// Check legality
|
||||
if (bookMoves != null) {
|
||||
ArrayList<Move> legalMoves = new MoveGen().pseudoLegalMoves(pos);
|
||||
|
@ -155,7 +141,7 @@ public final class DroidBook {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (bookMoves != null) {
|
||||
Collections.sort(bookMoves, new Comparator<BookEntry>() {
|
||||
public int compare(BookEntry arg0, BookEntry arg1) {
|
||||
|
@ -184,6 +170,22 @@ public final class DroidBook {
|
|||
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. */
|
||||
public static void main(String[] args) throws IOException {
|
||||
List<Byte> binBook = createBinBook();
|
||||
|
@ -196,7 +198,7 @@ public final class DroidBook {
|
|||
out.close();
|
||||
}
|
||||
|
||||
public static List<Byte> createBinBook() {
|
||||
private static final List<Byte> createBinBook() {
|
||||
List<Byte> binBook = new ArrayList<Byte>(0);
|
||||
try {
|
||||
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. */
|
||||
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);
|
||||
UndoInfo ui = new UndoInfo();
|
||||
String[] strMoves = line.split(" ");
|
||||
|
@ -251,7 +253,7 @@ public final class DroidBook {
|
|||
return true;
|
||||
}
|
||||
|
||||
private static int pieceToProm(int p) {
|
||||
private static final int pieceToProm(int p) {
|
||||
switch (p) {
|
||||
case Piece.WQUEEN: case Piece.BQUEEN:
|
||||
return 1;
|
||||
|
|
|
@ -101,29 +101,18 @@ public class DroidComputerPlayer {
|
|||
}
|
||||
|
||||
/** Get engine reported name, including strength setting. */
|
||||
public synchronized String getEngineName() {
|
||||
public final synchronized String getEngineName() {
|
||||
String ret = engineName;
|
||||
if (strength < 1000)
|
||||
ret += String.format(" (%.1f%%)", strength * 0.1);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/** Clear transposition table. */
|
||||
public final void clearTT() {
|
||||
/** Clear transposition table. Takes effect when next search started. */
|
||||
public final synchronized void clearTT() {
|
||||
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. */
|
||||
public final synchronized void ponderHit(Position pos, Move ponderMove) {
|
||||
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;
|
||||
try {
|
||||
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.
|
||||
* @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 = "";
|
||||
if (canClaimDraw50(pos)) {
|
||||
drawStr = "draw 50";
|
||||
|
|
Loading…
Reference in New Issue
Block a user