Skip to content

Commit 918d6af

Browse files
author
rahul
committed
added JStockfish
1 parent 86ada4d commit 918d6af

File tree

15 files changed

+346
-3
lines changed

15 files changed

+346
-3
lines changed

JavaStockfish/.classpath

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

JavaStockfish/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Stockfish</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.7
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.7
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.ltk.core.refactoring.enable.project.refactoring.history=false
2.91 KB
Binary file not shown.
1.07 KB
Binary file not shown.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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 = "../engine/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 Position
88+
* in FEN format
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+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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 Position
88+
* in FEN format
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+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.rahul.stockfish;
2+
3+
4+
public class StockfishTest {
5+
public static void main(String[] args) {
6+
Stockfish client = new Stockfish();
7+
// initialize and connect to engine
8+
if (client.startEngine()) {
9+
System.out.println("Engine has started..");
10+
} else {
11+
System.out.println("Oops! Something went wrong..");
12+
}
13+
// send commands manually
14+
client.sendCommand("uci");
15+
// receive output dump
16+
System.out.println(client.getOutput(0));
17+
// 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));
20+
// stop the engine
21+
client.stopEngine();
22+
}
23+
}

JavaStockfish/stockfish

328 KB
Binary file not shown.

0 commit comments

Comments
 (0)