zChessKit 1.0.0 Release
I’ve been working on a little side project lately that I’m really excited to share, zChessKit. A Swift package that provides the core building blocks for working with chess in Swift.
If you’ve ever wanted to build a chess app, analyze games, or just tinker with FENs and PGNs in your own Swift projects, this library is for you!
What’s Inside
zChessKit includes:
- Full move generation and validation
- FEN parsing and serialization
- PGN parsing with support for tags, moves, and comments
- Core types like
BoardState
,Game
, andMove
- Support for any platform that uses Swift Package Manager
Here’s what using it looks like:
1
2
3
4
5
6
7
8
import zChessKit
let fen = "2r4k/5prp/2b1P3/4Qp2/1PpR1P2/2q5/P4K1P/4R3 w - - 2 33"
// This way also works
if let state = BoardState.fromFEN(fen) {
print("white has \(state.generateAllLegalMoves().count) valid moves")
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
let pgn = """
[Event "Paris"]
[Site "Paris FRA"]
[Date "1858.??.??"]
[Round "?"]
[White "Paul Morphy"]
[Black "Duke Karl / Count Isouard"]
[Result "1-0"]
1. e4 e5 2. Nf3 d6 3. d4 Bg4 4. dxe5 Bxf3 5. Qxf3 dxe5 6. Bc4 Nf6 7. Qb3 Qe7
8. Nc3 c6 9. Bg5 b5 10. Nxb5 cxb5 11. Bxb5+ Nbd7 12. O-O-O Rd8 13. Rxd7 Rxd7
14. Rd1 Qe6 15. Bxd7+ Nxd7 16. Qb8+ $3 Nxb8 17. Rd8# 1-0
"""
if let tokens = try? Lexer.getPGNLexer().run(input: pgn),
let games = try? Parser.parsePGN(from: tokens),
let game = games.first {
print("Parsed \(game.moves.count) moves")
}
Why I Built It
Like many others navigating COVID-19 lockdowns, I got into ches via the Queen’s Gambit show. I had played chess earlier with friends and family, but after discovering online chess I found a bigger and better love for the game.
Since then I’ve experimented with various aspects of chess programming from reading algorithm books and specifications for protocols from the early days of the internet, but never really took the time to compile it all somewhere usable for other projects, until now.
What’s Next
zChessKit will be a foundation for a few other projects I want to play around with, notably zChess.app, which should be available on the App Store soon. After that I plan to try my hand at beating Stockfish and expanding what I can do with PGN parsing.
If you’re into Swift, chess, or both, I’d love your feedback. Feel free to open issues or suggestions on GitHub, shoot me an email, or connect on LinkedIn.