Improved stockfish
This commit is contained in:
parent
2bc9b79ad8
commit
a8adf6d827
|
@ -41,7 +41,7 @@ public class Player {
|
|||
}, 1000, 1000);
|
||||
}
|
||||
|
||||
public void randomMove() {
|
||||
public boolean randomMove() {
|
||||
ArrayList<APiece> myPieces = getPieces();
|
||||
while(myPieces.size() != 0) {
|
||||
Random r = new Random();
|
||||
|
@ -66,23 +66,25 @@ public class Player {
|
|||
PiecePosition randomMove = possibleMoves.get(r.nextInt(moveCount));
|
||||
selectedPiece.move(randomMove, true);
|
||||
chessboard.changeActivePlayer();
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
System.out.println("No possible move!");
|
||||
return false;
|
||||
}
|
||||
|
||||
public void smartMove() {
|
||||
public boolean smartMove() {
|
||||
if(stockfish == null) {
|
||||
stockfish = new Stockfish();
|
||||
stockfish.startEngine();
|
||||
}
|
||||
String fen = chessboard.toFEN();
|
||||
String bestMove = stockfish.getBestMove(fen, 500);
|
||||
String bestMove = stockfish.getBestMove(fen, 0, 500);
|
||||
PiecePosition fromPos = PiecePosition.fromString(bestMove.substring(0, 2));
|
||||
PiecePosition toPos = PiecePosition.fromString(bestMove.substring(2, 4));
|
||||
if(fromPos == null || toPos == null) return false;
|
||||
APiece piece = chessboard.getPiece(fromPos.x, fromPos.y);
|
||||
piece.move(toPos);
|
||||
chessboard.changeActivePlayer();
|
||||
return true;
|
||||
}
|
||||
|
||||
public Chessboard getChessboard() {
|
||||
|
|
|
@ -56,10 +56,15 @@ public class Stockfish {
|
|||
return buffer.toString();
|
||||
}
|
||||
|
||||
public String getBestMove(String fen, int waitTime) {
|
||||
public String getBestMove(String fen, int skill, int waitTime) {
|
||||
sendCommand("position fen " + fen);
|
||||
sendCommand("setoption name Skill Level value " + skill);
|
||||
sendCommand("go movetime " + waitTime);
|
||||
return getOutput(waitTime + 100).split("bestmove ")[1].split(" ")[0];
|
||||
String[] parts = getOutput(waitTime+150).split("bestmove ");
|
||||
while(parts.length != 2) {
|
||||
parts = getOutput(150).split("bestmove ");
|
||||
}
|
||||
return parts[1].split(" ")[0];
|
||||
}
|
||||
|
||||
public void stopEngine() {
|
||||
|
|
Loading…
Reference in a new issue