

These two classes serve as the models for the project. These are represented in the project by objects of type IBoardItem.Įnd Set End Property Private _piece As PieceTypeĮnd Set End Property Private _rank As Integer Public Property Rank As Integer Implements IBoardItem.RankĮnd Set End Property Private _file As Char Public Property File As Char Implements IBoardItem.FileĮnd Set End Property Public Property ItemType As ChessBoardItem Implements IBoardItem.ItemType The chess engine is included in the project.Ī chess board has 64 squares and in its initial setup, has 32 pieces. When the engine sends a message, the EngineMessage event is raised. Notice I'm starting the chess engine in the StartEngine method and making use of Rxs Observable.Timer to check, in one millisecond intervals, for any messages it sends. If message String.Empty Then RaiseEvent EngineMessage(message) StrmWriter.WriteLine(UciCommands.ucinewgame)Įnd If End Sub Private Sub ReadEngineMessages() StrmWriter.WriteLine(UciCommands.isready) TimeSpan.FromMilliseconds( 1)).Subscribe( Sub() ReadEngineMessages()) StrmReader = engineProcess.StandardOutputĮngineListener = Observable.Timer(TimeSpan.Zero, _ exe" ThenĮ = engine.FullNameĮ = FalseĮ = TrueĮ = TrueĮ = TrueĮ = True If engine.Exists AndAlso engine.Extension = ". (Environment.CurrentDirectory, " stockfish_8_圆4.exe")) If engineProcess IsNot Nothing And Not engineProcess.HasExited ThenĮnd If End Sub Public Sub StartEngine() Implements IEngineService.StartEngine If strmWriter IsNot Nothing AndAlso command UciCommands.uci ThenĮnd If End Sub Public Sub StopEngine() Implements IEngineService.StopEngine Public Sub SendCommand(command As String) Implements IEngineService.SendCommand Public Event EngineMessage(message As String) Implements IEngineService.EngineMessage StrmWriter.WriteLine(UciCommands.ucinewgame) TimeSpan.FromMilliseconds( 1)).Subscribe(s => ReadEngineMessages()) StrmReader = engineProcess.StandardOutput ĮngineListener = Observable.Timer(TimeSpan.Zero, StrmWriter = engineProcess.StandardInput exe")Į = engine.FullName Į = false Į = true Į = true Į = true Į = true If (engine.Exists & engine.Extension = ".

If (engineProcess != null & !engineProcess.HasExited)įileInfo engine = new FileInfo(Path.Combine If (strmWriter != null & command != UciCommands.uci) Public class StockfishService : IEngineService This is done by the StockfishService class: StockChess launches and communicates with Stockfish by making use of the Process class which is in the Systems.Diagnostics namespace. Tells the engine to start analysing the position, set up with the position command, in 5 sec. Tells the engine to set up its internal chess board and play the moves e4 e5.

Tells the engine that the search command that follows this command will be from a new game. This is the first command sent to the engine telling it to switch to UCI mode. The following table contains examples of commands a GUI can send to the engine, Command What are UCI commands you ask? These are text commands that are used to interact with an engine using the UCI (Universal Chess Interface) protocol. To enable the user to play against Stockfish, the application has to communicate with the chess engine. To run the attached project, you require VS2015 or higher and to better understand this article, knowledge of chess notation will be useful. Specifically the app makes use of Stockfish 8, the most recent release of the engine as at the time of writing. For the chess engine, I decided to use Stockfish, which is why the app is named StockChess. I had two goals in mind when developing this application: the first was to develop an app where the user can play against a chess engine the second was to create a WPF chess application that followed the MVVM pattern. The app is quite basic, with the user matching his/her wits against the chess engine as either black or white. StockChess combines the awesome features of WPF with the exceptional chess analysis of Stockfish – the strongest open-source chess engine in the world.
