@@ -22,8 +22,8 @@ For a related Swift package that manages chess logic, see [chesskit-swift](https
2222## Usage
2323
24241 . Add ` chesskit-engine ` as a dependency
25- * in an [app built in Xcode](https://developer.apple.com/documentation/xcode/adding-package-dependencies-to-your-app),
26- * or [as a dependency to another Swift Package](https://www.swift.org/documentation/package-manager/#importing-dependencies).
25+ * in an [ app built in Xcode] ( https://developer.apple.com/documentation/xcode/adding-package-dependencies-to-your-app ) ,
26+ * or [ as a dependency to another Swift Package] ( https://www.swift.org/documentation/package-manager/#importing-dependencies ) .
2727
28282 . Next, import ` ChessKitEngine ` to use it in Swift code:
2929``` swift
@@ -37,51 +37,49 @@ import ChessKitEngine
3737
3838## Features
3939
40- * Initialize an engine and set response handler
40+ * Initialize an engine and set response stream
4141``` swift
4242// create Stockfish engine
4343let engine = Engine (type : .stockfish )
4444
45- // set response handler , called when engine issues responses
46- engine. receiveResponse = { response in
45+ // set response stream , called when engine issues responses
46+ for await response in await engine.responseStream ! {
4747 print (response)
4848}
4949
5050// start listening for engine responses
51- engine.start {
52- // engine is ready to go!
53- }
51+ engine.start ()
5452```
5553
5654* Send [ UCI protocol] ( https://backscattering.de/chess/uci/2006-04.txt ) commands
5755``` swift
5856// check that engine is running before sending commands
59- guard engine.isRunning else { return }
57+ guard await engine.isRunning else { return }
6058
6159// stop any current engine processing
62- engine.send (command : .stop )
60+ await engine.send (command : .stop )
6361
6462// set engine position to standard starting chess position
65- engine.send (command : .position (.startpos ))
63+ await engine.send (command : .position (.startpos ))
6664
6765// start engine analysis with maximum depth of 15
68- engine.send (command : .go (depth : 15 ))
66+ await engine.send (command : .go (depth : 15 ))
6967```
7068
7169* Update engine position after a move is made
7270``` swift
7371// FEN after 1. e4
7472let newPosition = " rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1"
7573
76- engine.send (command : .stop )
77- engine.send (command : .position (.fen (newPosition)))
78- engine.send (command : .go (depth : 15 ))
74+ await engine.send (command : .stop )
75+ await engine.send (command : .position (.fen (newPosition)))
76+ await engine.send (command : .go (depth : 15 ))
7977```
8078
8179* Receive engine's analysis of current position
8280``` swift
83- // receiveResponse is called whenever the engine publishes a response
84- engine. receiveResponse = { response in
81+ // responseStream is called whenever the engine publishes a response
82+ for await response in await engine.responseStream ! {
8583 switch response {
8684 case let .info (info):
8785 print (info.score ) // engine evaluation score in centipawns
@@ -90,18 +88,19 @@ engine.receiveResponse = { response in
9088 break
9189 }
9290}
91+
9392```
9493
9594* Terminate engine communication
9695``` swift
9796// stop listening for engine responses
98- engine.stop ()
97+ await engine.stop ()
9998```
10099
101100* Enable engine response logging
102101``` swift
103102// log engine commands and responses to the console
104- engine.loggingEnabled = true
103+ engine.setLoggingEnabled ( true )
105104
106105// Logging is off by default since engines can be very
107106// verbose while analyzing positions and returning evaluations.
@@ -116,11 +115,11 @@ They can be provided to the engine using the `.setoption(id:value:)` UCI command
116115For example:
117116``` swift
118117// Stockfish
119- engine.send (command : .setoption (id : " EvalFile" , value : fileURL))
120- engine.send (command : .setoption (id : " EvalFileSmall" , value : smallFileURL))
118+ await engine.send (command : .setoption (id : " EvalFile" , value : fileURL))
119+ await engine.send (command : .setoption (id : " EvalFileSmall" , value : smallFileURL))
121120
122121// Lc0
123- engine.send (command : .setoption (id : " WeightsFile" , value : fileURL))
122+ await engine.send (command : .setoption (id : " WeightsFile" , value : fileURL))
124123```
125124
126125The following details the recommended files for each engine and where to obtain them.
0 commit comments