1
+ using ChessChallenge . API ;
2
+ using System ;
3
+ using System . Diagnostics ;
4
+ using System . IO ;
5
+ using System . Linq . Expressions ;
6
+
7
+ public class Stockfish : IChessBot
8
+ {
9
+
10
+ // The name of the stockfish binaries that you downloaded and placed in ...\Chess-Challenge\bin\Debug\net6.0
11
+ const string STOCKFISH_BINARIES = "stockfish" ;
12
+
13
+ // Adjust this so the time it takes to think is adequate
14
+ // Adjusting this also have an effect on how strong it will play
15
+ const string STOCKFISH_DEPTH = "10" ;
16
+
17
+ // Min: 0, Max: 20
18
+ const string STOCKFISH_SKILL_LEVEL = "20" ;
19
+
20
+ // For best performance, set this equal to the number of CPU cores available.
21
+ const string STOCKFISH_THREADS = "6" ;
22
+
23
+ public Move Think ( Board board , Timer timer )
24
+ {
25
+ Move bestMove = Move . NullMove ;
26
+
27
+ Process stockfish = new Process ( ) ;
28
+ stockfish . StartInfo . UseShellExecute = false ;
29
+ stockfish . StartInfo . RedirectStandardOutput = true ;
30
+ stockfish . StartInfo . RedirectStandardInput = true ;
31
+ stockfish . StartInfo . FileName = STOCKFISH_BINARIES ;
32
+ stockfish . OutputDataReceived += ( sender , args ) =>
33
+ {
34
+ /* Example output:
35
+ Stockfish 16 by the Stockfish developers (see AUTHORS file)
36
+ info string NNUE evaluation using nn-5af11540bbfe.nnue enabled
37
+ info depth 1 seldepth 1 multipv 1 score cp 2 nodes 20 nps 20000 hashfull 0 tbhits 0 time 1 pv g1f3
38
+ info depth 2 seldepth 2 multipv 1 score cp 2 nodes 40 nps 40000 hashfull 0 tbhits 0 time 1 pv g1f3
39
+ info depth 3 seldepth 2 multipv 1 score cp 16 nodes 70 nps 70000 hashfull 0 tbhits 0 time 1 pv c2c3
40
+ info depth 4 seldepth 2 multipv 1 score cp 29 nodes 101 nps 101000 hashfull 0 tbhits 0 time 1 pv e2e4
41
+ info depth 5 seldepth 3 multipv 1 score cp 42 nodes 131 nps 131000 hashfull 0 tbhits 0 time 1 pv e2e4 g8f6
42
+ info depth 6 seldepth 4 multipv 1 score cp 59 nodes 489 nps 244500 hashfull 0 tbhits 0 time 2 pv g1f3 d7d5 d2d4
43
+ info depth 7 seldepth 6 multipv 1 score cp 31 nodes 1560 nps 520000 hashfull 1 tbhits 0 time 3 pv e2e4 d7d5 e4d5 d8d5 g1f3
44
+ info depth 8 seldepth 6 multipv 1 score cp 40 nodes 2105 nps 701666 hashfull 1 tbhits 0 time 3 pv e2e4 d7d5 e4d5 d8d5
45
+ info depth 9 seldepth 8 multipv 1 score cp 48 nodes 4500 nps 900000 hashfull 1 tbhits 0 time 5 pv e2e4 e7e5 g1f3 g8f6 f3e5 f6e4 d2d4 b8c6
46
+ info depth 10 seldepth 10 multipv 1 score cp 50 nodes 7548 nps 943500 hashfull 2 tbhits 0 time 8 pv e2e4 e7e5 g1f3 g8f6 b1c3 d7d6 d2d4
47
+ bestmove e2e4 ponder e7e5
48
+
49
+ */
50
+ if ( args . Data . StartsWith ( "bestmove" ) ) {
51
+ bestMove = new Move ( args . Data . Split ( ' ' ) [ 1 ] , board ) ;
52
+ stockfish . StandardInput . WriteLine ( "quit" ) ;
53
+ stockfish . Close ( ) ;
54
+ }
55
+ } ;
56
+
57
+ try
58
+ {
59
+ stockfish . Start ( ) ;
60
+ }
61
+ catch ( System . ComponentModel . Win32Exception )
62
+ {
63
+ Console . WriteLine (
64
+ "Unable to find stockfish binaries, expecting a binary file named "
65
+ + STOCKFISH_BINARIES + " inside of: \n " + Directory . GetCurrentDirectory ( )
66
+ + "\n \n Download your stockfish binaries at https://stockfishchess.org/download/"
67
+ ) ;
68
+ Environment . Exit ( 0 ) ;
69
+ }
70
+
71
+ stockfish . BeginOutputReadLine ( ) ;
72
+
73
+ stockfish . StandardInput . WriteLine ( "setoption name Threads value " + STOCKFISH_THREADS ) ;
74
+ stockfish . StandardInput . WriteLine ( "setoption name Skill Level value " + STOCKFISH_SKILL_LEVEL ) ;
75
+ stockfish . StandardInput . WriteLine ( "position fen " + board . GetFenString ( ) ) ;
76
+ stockfish . StandardInput . WriteLine ( "go depth " + STOCKFISH_DEPTH ) ;
77
+
78
+ stockfish . WaitForExit ( ) ;
79
+
80
+ return bestMove ;
81
+ }
82
+ }
0 commit comments