SGFKit

SGFKit 是一个用于在 Swift 中操作 SGF FF[4] 文件的库。你可以以类型安全的方式操作 SGF 文件。

请参考文档

安装

SGFKit 仅支持 Swift Package Manager。

.package(url: "https://github.com/mtj0928/SGFKit", .upToNextMinor(from: "0.4.0"))

用法

解析特定对局的 SGF 文本。

当指定对局时,你可以以类型安全的方式从 SGF 文本中提取属性。

let go = try Go(input: "(;FF[4]C[root](;B[ab]))")

let rootNode = go.tree.nodes[0] // ;FF[4]C[root]
let fileFormatVersion: Int? = rootNode.propertyValue(of: .fileFormat) // 4
let comment: String? = rootNode.propertyValue(of: .comment) // "root"

let nodeA = rootNode.children[0] // ;B[ab]
let pointA: Go.Point? = nodeA.propertyValue(of: .black) // (1, 2)

更新属性

你可以更新属性值并从节点中移除属性。

注意

由于树形结构,节点是引用类型。

let go = try Go(input: "(;FF[4];B[ab]C[a];B[ba])")
let rootNode = go.tree.nodes[0]
let node = rootNode.children[0] // ;B[ab]C[a]

let point = GoPoint(column: 4, row: 3)
node.addProperty(point, to: .black) // dc
node.removeProperty(of: .comment)

print(go.tree.convertToSGF()) // "(;FF[4];B[dc];B[ba])"

基于 EBNF 定义解析 SGF 文本。

你可以基于官方 EBNF 解析 SGF。返回的值是 EBNF 的非终结符。

let input = "(;FF[4]C[root](;C[a];C[b](;C[c])(;C[d];C[e]))(;C[f](;C[g];C[h];C[i])(;C[j])))"

let collection = try Parser.parse(input: input)
let firstNode = collection.gameTrees[0].sequence.nodes[0] // ;FF[4]C[root]
let firstProperty = firstNode.properties[0] // FF[4]

print(firstProperty.identifier.letters) // "FF"
print(firstProperty.values[0].type.first.value ?? "none") // "4"