|
| 1 | +package com.rahul.stockfish; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.io.OutputStreamWriter; |
| 7 | + |
| 8 | +/** |
| 9 | + * A simple and efficient client to run Stockfish from Java |
| 10 | + * |
| 11 | + * @author Rahul A R |
| 12 | + * |
| 13 | + */ |
| 14 | +public class Stockfish { |
| 15 | + |
| 16 | + private Process engineProcess; |
| 17 | + private BufferedReader processReader; |
| 18 | + private OutputStreamWriter processWriter; |
| 19 | + |
| 20 | + private static final String PATH = "./stockfish"; |
| 21 | + |
| 22 | + /** |
| 23 | + * Starts Stockfish engine as a process and initializes it |
| 24 | + * |
| 25 | + * @param None |
| 26 | + * @return True on success. False otherwise |
| 27 | + */ |
| 28 | + public boolean startEngine() { |
| 29 | + try { |
| 30 | + engineProcess = Runtime.getRuntime().exec(PATH); |
| 31 | + processReader = new BufferedReader(new InputStreamReader( |
| 32 | + engineProcess.getInputStream())); |
| 33 | + processWriter = new OutputStreamWriter( |
| 34 | + engineProcess.getOutputStream()); |
| 35 | + } catch (Exception e) { |
| 36 | + return false; |
| 37 | + } |
| 38 | + return true; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Takes in any valid UCI command and executes it |
| 43 | + * |
| 44 | + * @param command |
| 45 | + */ |
| 46 | + public void sendCommand(String command) { |
| 47 | + try { |
| 48 | + processWriter.write(command + "\n"); |
| 49 | + processWriter.flush(); |
| 50 | + } catch (IOException e) { |
| 51 | + e.printStackTrace(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * This is generally called right after 'sendCommand' for getting the raw |
| 57 | + * output from Stockfish |
| 58 | + * |
| 59 | + * @param waitTime |
| 60 | + * Time in milliseconds for which the function waits before |
| 61 | + * reading the output. Useful when a long running command is |
| 62 | + * executed |
| 63 | + * @return Raw output from Stockfish |
| 64 | + */ |
| 65 | + public String getOutput(int waitTime) { |
| 66 | + StringBuffer buffer = new StringBuffer(); |
| 67 | + try { |
| 68 | + Thread.sleep(waitTime); |
| 69 | + sendCommand("isready"); |
| 70 | + while (true) { |
| 71 | + String text = processReader.readLine(); |
| 72 | + if (text.equals("readyok")) |
| 73 | + break; |
| 74 | + else |
| 75 | + buffer.append(text + "\n"); |
| 76 | + } |
| 77 | + } catch (Exception e) { |
| 78 | + e.printStackTrace(); |
| 79 | + } |
| 80 | + return buffer.toString(); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * This function returns the best move for a given position after |
| 85 | + * calculating for 'waitTime' ms |
| 86 | + * |
| 87 | + * @param fen |
| 88 | + * Position string |
| 89 | + * @param waitTime |
| 90 | + * in milliseconds |
| 91 | + * @return Best Move in PGN format |
| 92 | + */ |
| 93 | + public String getBestMove(String fen, int waitTime) { |
| 94 | + sendCommand("position fen " + fen); |
| 95 | + sendCommand("go movetime " + waitTime); |
| 96 | + return getOutput(waitTime + 20).split("bestmove ")[1].split(" ")[0]; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Stops Stockfish and cleans up before closing it |
| 101 | + */ |
| 102 | + public void stopEngine() { |
| 103 | + try { |
| 104 | + sendCommand("quit"); |
| 105 | + processReader.close(); |
| 106 | + processWriter.close(); |
| 107 | + } catch (IOException e) { |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Get a list of all legal moves from the given position |
| 113 | + * |
| 114 | + * @param fen |
| 115 | + * Position string |
| 116 | + * @return String of moves |
| 117 | + */ |
| 118 | + public String getLegalMoves(String fen) { |
| 119 | + sendCommand("position fen " + fen); |
| 120 | + sendCommand("d"); |
| 121 | + return getOutput(0).split("Legal moves: ")[1]; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Draws the current state of the chess board |
| 126 | + * |
| 127 | + * @param fen |
| 128 | + * Position string |
| 129 | + */ |
| 130 | + public void drawBoard(String fen) { |
| 131 | + sendCommand("position fen " + fen); |
| 132 | + sendCommand("d"); |
| 133 | + |
| 134 | + String[] rows = getOutput(0).split("\n"); |
| 135 | + |
| 136 | + for (int i = 1; i < 18; i++) { |
| 137 | + System.out.println(rows[i]); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments