-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_node.js
executable file
·83 lines (73 loc) · 2.07 KB
/
simple_node.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env node
var stockfish = require("./stockfish.js");
var engine = stockfish();
var position = "startpos";
var got_uci;
var started_thinking;
function send(str)
{
console.log("Sending: " + str)
engine.postMessage(str);
}
if (process.argv[2] === "--help") {
console.log("Usage: node simple_node.js [FEN OR move1 move2 ...moveN]");
console.log("");
console.log("Examples:");
console.log(" node simple_node.js");
console.log(" node simple_node.js \"rnbqkbnr/ppp1pppp/8/3p4/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2\"");
console.log(" node simple_node.js g1f3 e7e5");
process.exit();
}
engine.onmessage = function (line)
{
var match;
console.log("Line: " + line)
if (typeof line !== "string") {
console.log("Got line:");
console.log(typeof line);
console.log(line);
return;
}
if (!got_uci && line === "uciok") {
got_uci = true;
if (position) {
send("position " + position);
send("eval");
send("d");
}
send("go ponder");
} else if (!started_thinking && line.indexOf("info depth") > -1) {
console.log("Thinking...");
started_thinking = true;
setTimeout(function ()
{
send("stop");
}, 1000 * 10);
} else if (line.indexOf("bestmove") > -1) {
match = line.match(/bestmove\s+(\S+)/);
if (match) {
console.log("Best move: " + match[1]);
process.exit();
}
}
};
(function get_position()
{
var i,
len,
temp_arr;
if (process.argv.length > 2) {
/// Does it look like FEN?
if (process.argv.length === 3 && process.argv[2].indexOf("/") > -1) {
position = "fen " + process.argv[2];
} else {
temp_arr = [];
len = process.argv.length;
for (i = 2; i <= len; i += 1) {
temp_arr[temp_arr.length] = process.argv[i];
}
position = "startpos moves " + temp_arr.join(" ");
}
}
}());
send("uci");