Skip to content

Commit dc0b6a5

Browse files
committed
added drawBoard, legalMoves
1 parent 7dddfcd commit dc0b6a5

File tree

3 files changed

+188
-6
lines changed

3 files changed

+188
-6
lines changed

JavaStockfish/src/com/rahul/stockfish/Stockfish.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class Stockfish {
1616
private Process engineProcess;
1717
private BufferedReader processReader;
1818
private OutputStreamWriter processWriter;
19-
19+
2020
private static final String PATH = "../engine/stockfish";
2121

2222
/**
@@ -84,8 +84,8 @@ public String getOutput(int waitTime) {
8484
* This function returns the best move for a given position after
8585
* calculating for 'waitTime' ms
8686
*
87-
* @param Position
88-
* in FEN format
87+
* @param fen
88+
* Position string
8989
* @param waitTime
9090
* in milliseconds
9191
* @return Best Move in PGN format
@@ -107,4 +107,34 @@ public void stopEngine() {
107107
} catch (IOException e) {
108108
}
109109
}
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+
}
110140
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
}
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,35 @@
11
package com.rahul.stockfish;
22

3-
43
public class StockfishTest {
54
public static void main(String[] args) {
65
Stockfish client = new Stockfish();
6+
String FEN = "8/6pk/8/1R5p/3K3P/8/6r1/8 b - - 0 42";
7+
78
// initialize and connect to engine
89
if (client.startEngine()) {
910
System.out.println("Engine has started..");
1011
} else {
1112
System.out.println("Oops! Something went wrong..");
1213
}
14+
1315
// send commands manually
1416
client.sendCommand("uci");
17+
1518
// receive output dump
1619
System.out.println(client.getOutput(0));
20+
1721
// get the best move for a position with a given think time
18-
System.out.println(client.getBestMove(
19-
"8/6pk/8/1R5p/3K3P/8/6r1/8 b - - 0 42", 1000));
22+
System.out.println("Best move : " + client.getBestMove(FEN, 100));
23+
24+
// get all the legal moves from a given position
25+
System.out.println("Legal moves : " + client.getLegalMoves(FEN));
26+
27+
// draw board from a given position
28+
System.out.println("Board state :");
29+
client.drawBoard(FEN);
30+
2031
// stop the engine
32+
System.out.println("Stopping engine..");
2133
client.stopEngine();
2234
}
2335
}

0 commit comments

Comments
 (0)