Skip to content

Commit f2174f4

Browse files
authored
Add support for strict swift concurrency (#21)
2 parents 8975c9f + fe297f4 commit f2174f4

14 files changed

Lines changed: 382 additions & 188 deletions

File tree

Package.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version: 5.9
1+
// swift-tools-version: 6.0
22

33
import PackageDescription
44

@@ -10,7 +10,8 @@ let package = Package(
1010
.iOS(.v16),
1111
.watchOS(.v9),
1212
.macOS(.v13),
13-
.tvOS(.v16)
13+
.tvOS(.v16),
14+
.visionOS(.v1)
1415
],
1516
products: [
1617
.library(
@@ -21,7 +22,9 @@ let package = Package(
2122
targets: [
2223
.target(
2324
name: "ChessKitEngine",
24-
dependencies: ["ChessKitEngineCore"]
25+
dependencies: [
26+
"ChessKitEngineCore",
27+
]
2528
),
2629
.target(
2730
name: "ChessKitEngineCore",

README.md

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ For a related Swift package that manages chess logic, see [chesskit-swift](https
2222
## Usage
2323

2424
1. 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

2828
2. 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
4343
let 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
7472
let 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
116115
For 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

126125
The following details the recommended files for each engine and where to obtain them.

0 commit comments

Comments
 (0)