Skip to content

Commit 4a8f22b

Browse files
committed
merge {deluf:main}: Fix for SebLague#347
1 parent 099cea3 commit 4a8f22b

File tree

5 files changed

+96
-4
lines changed

5 files changed

+96
-4
lines changed

Chess-Challenge/Chess-Challenge.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@
4141
<Compile Update="src\Framework\Application\UI\BotBrainCapacityUI.cs">
4242
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
4343
</Compile>
44-
<Compile Update="src\Evil Bot\EvilBot.cs">
44+
<Compile Update="src\EvilBot\EvilBot.cs">
45+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
46+
</Compile>
47+
<Compile Update="src\Stockfish\Stockfish.cs">
4548
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
4649
</Compile>
4750
</ItemGroup>

Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public enum PlayerType
2020
Human,
2121
MyBot,
2222
EvilBot,
23-
LastBot
23+
LastBot,
24+
Stockfish
2425
}
2526

2627
// Game state
@@ -57,6 +58,7 @@ public enum PlayerType
5758
readonly int debugTokenCount;
5859
readonly StringBuilder pgns;
5960
long maxMemoryUsed = 0;
61+
public bool fastForward;
6062

6163
public ChallengeController()
6264
{
@@ -213,6 +215,7 @@ ChessPlayer CreatePlayer(PlayerType type)
213215
PlayerType.MyBot => new ChessPlayer(new MyBot(), type, GameDurationMilliseconds),
214216
PlayerType.EvilBot => new ChessPlayer(new EvilBot(), type, GameDurationMilliseconds),
215217
PlayerType.LastBot => new ChessPlayer(LastBot.GetLastBot(), type, GameDurationMilliseconds),
218+
PlayerType.Stockfish => new ChessPlayer(new Stockfish(), type, GameDurationMilliseconds),
216219
_ => new ChessPlayer(new HumanPlayer(boardUI), type)
217220
};
218221
}

Chess-Challenge/src/Framework/Application/Core/Settings.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ namespace ChessChallenge.Application
44
{
55
public static class Settings
66
{
7-
public const string Version = "1.20";
7+
public const string Version = "1.20.x";
88

99
// Game settings
1010
public const int GameDurationMilliseconds = 60 * 1000;
1111
public const int IncrementMilliseconds = 0 * 1000;
12-
public const float MinMoveDelay = 0;
12+
public const float MinMoveDelay = 0.01f;
1313
public static readonly bool RunBotsOnSeparateThread = true;
1414

1515
// Display settings

Chess-Challenge/src/Framework/Application/UI/MenuUI.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public static void DrawButtons(ChallengeController controller)
3333
{
3434
controller.StartNewBotMatch(ChallengeController.PlayerType.MyBot, ChallengeController.PlayerType.LastBot);
3535
}
36+
if (NextButtonInRow("MyBot vs Stockfish", ref buttonPos, spacing, buttonSize))
37+
{
38+
controller.StartNewBotMatch(ChallengeController.PlayerType.MyBot, ChallengeController.PlayerType.Stockfish);
39+
}
3640

3741
// Page buttons
3842
buttonPos.Y += breakSpacing;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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\nDownload 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

Comments
 (0)