A Swift package for the following chess engines:
chesskit-engine
implements the Universal Chess Interface protocol for communication between chess engines and user interfaces built with Swift.
For a related Swift package that manages chess logic, see chesskit-swift.
-
Add
chesskit-engine
as a dependency -
Next, import
ChessKitEngine
to use it in Swift code:
import ChessKitEngine
// ...
- Initialize an engine and set response handler
// create Stockfish engine
let engine = Engine(type: .stockfish)
// set response handler, called when engine issues responses
engine.receiveResponse = { response in
print(response)
}
// start listening for engine responses
engine.start {
// engine is ready to go!
}
- Send UCI protocol commands
// check that engine is running before sending commands
guard engine.isRunning else { return }
// stop any current engine processing
engine.send(command: .stop)
// set engine position to standard starting chess position
engine.send(command: .position(.startpos))
// start engine analysis with maximum depth of 15
engine.send(command: .go(depth: 15))
- Update engine position after a move is made
// FEN after 1. e4
let newPosition = "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1"
engine.send(command: .stop)
engine.send(command: .position(.fen(newPosition)))
engine.send(command: .go(depth: 15))
- Receive engine's analysis of current position
// receiveResponse is called whenever the engine publishes a response
engine.receiveResponse = { response in
switch response {
case let .info(info):
print(info.score) // engine evaluation score in centipawns
print(info.pv) // array of move strings representing best line
default:
break
}
}
- Terminate engine communication
// stop listening for engine responses
engine.stop()
- Enable engine response logging
// log engine commands and responses to the console
engine.loggingEnabled = true
// Logging is off by default since engines can be very
// verbose while analyzing positions and returning evaluations.
Both Stockfish 17
and LeelaChessZero 0.31.1
require neural network files to be provided to the engine for computation.
In order to keep the package size small and allow for the greatest level of flexibility, these neural network files are not bundled with the package. Therefore they must be added to the app (either in the bundle or manually by a user) and then provided to the engine at runtime.
They can be provided to the engine using the .setoption(id:value:)
UCI commands included in chesskit-engine
.
For example:
// Stockfish
engine.send(command: .setoption(id: "EvalFile", value: fileURL))
engine.send(command: .setoption(id: "EvalFileSmall", value: smallFileURL))
// Lc0
engine.send(command: .setoption(id: "WeightsFile", value: fileURL))
The following details the recommended files for each engine and where to obtain them.
"EvalFile"
:nn-1111cefa1111.nnue
(download here)"EvalFileSmall"
:nn-37f18f62d772.nnue
(download here)- Other files from https://tests.stockfishchess.org can be used if desired.
chesskit-engine
(PR's are welcome!).
"WeightsFile"
:192x15_network
(download here)- Other files can be obtained here or here.
The following engines are currently supported:
Engine | Version | License | Options Reference | |
---|---|---|---|---|
Stockfish | 17 | GPL v3 | 🔗 | |
lc0 | 0.31.1 | GPL v3 | 🔗 |
ChessKitEngine
is distributed under the MIT License.