Improved stockfish

This commit is contained in:
Filip Znachor 2023-05-02 20:46:29 +02:00
parent 2bc9b79ad8
commit a8adf6d827
2 changed files with 14 additions and 7 deletions

View file

@ -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() {

View file

@ -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() {