Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mtj0928/sgfkit
A library for operating SGF FF[4] file in Swift.
https://github.com/mtj0928/sgfkit
Last synced: about 2 months ago
JSON representation
A library for operating SGF FF[4] file in Swift.
- Host: GitHub
- URL: https://github.com/mtj0928/sgfkit
- Owner: mtj0928
- License: mit
- Created: 2024-04-29T22:56:44.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-05-05T01:59:41.000Z (8 months ago)
- Last Synced: 2024-10-20T08:39:04.934Z (2 months ago)
- Language: Swift
- Homepage: https://mtj0928.github.io/SGFKit/documentation/sgfkit
- Size: 68.4 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SGFKit
SGFKit is a library for manipulating a [SGF FF[4]](https://www.red-bean.com/sgf/index.html) file in Swift.
You can manipulate a SGF in a type-safe manner.Please refer [documents](https://mtj0928.github.io/SGFKit/documentation/sgfkit).
## Install
SGFKit supports only Swift Package Manager.
```swift
.package(url: "https://github.com/mtj0928/SGFKit", .upToNextMinor(from: "0.4.0"))
```## Usage
### Parse a SGF text for a specific game.
You can extract a property from the SGF text in the type-safe manner, when the game is specified,
```swift
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)
```### Update a property
You can update a property value and remove a property from a node.> [!NOTE]
> A node is a reference type due to a tree structure.```swift
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])"
```### Parse a SGF text based on the EBNF definition.
You can parse the SGF based on [the official EBNF](https://www.red-bean.com/sgf/sgf4.html#ebnf-def).
The returned value is non terminal symbols of the EBNF.```swift
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"
```