-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathStartChessEngine.java
More file actions
57 lines (51 loc) · 1.98 KB
/
StartChessEngine.java
File metadata and controls
57 lines (51 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.chess;
import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import com.chess.command.StopCommand;
import com.chess.process.EngineProcess;
import com.chess.utilities.ChessEngineUtilities;
import com.chess.utilities.RobotUtilities;
public class StartChessEngine implements Runnable {
public static long requestTime = 0L;
public static boolean hasReturn = false;
private static Logger log ;
static {
PropertyConfigurator.configure("log4j.properties");
log= Logger.getLogger(RobotUtilities.class);
}
@Override
public void run() {
try {
while (true) {
// 处理准备开始,悔棋,求和。保证当前局面已到我方开始走棋,返回当前棋盘局面。
String fen = RobotUtilities.prepareToPlay();
// 通过fen获取最佳下法
String bestmove = ChessEngineUtilities.getBastMove(fen);
// 通过最佳下法操作鼠标点击
RobotUtilities.playByBestmove(bestmove);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
// 使引擎在空闲状态
final OutputStream outputStream = EngineProcess.getOutputStream();
outputStream.write("ucci\r\n".getBytes());
outputStream.write("setoption Hash 256\r\n".getBytes());
outputStream.flush();
// 开始检测定时器,要注意根据限时设置时间,要不会影响引擎的棋力
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(new StopCommand(), 0, 2000, TimeUnit.MILLISECONDS);
// 开始挂机线程
ExecutorService exec = Executors.newSingleThreadExecutor();
exec.execute(new StartChessEngine());
log.debug("engine starting success!!");
}
}