Lightweight and fast chess framework written in Swift.
ChessKit is used as a base framework for Ladoga chess engine.
The ChessKit framework is avalable for installation via Swift Package Manager and CocoaPods.
Add a dependency via Xcode, linking to https://github.com/aperechnev/ChessKit, or directly in your Package.swift file:
import PackageDescription
let package = Package(
name: "MyPackage",
platforms: [
.macOS(.v10_12),
],
dependencies: [
.package(url: "https://github.com/aperechnev/ChessKit.git", from: "1.3.7"),
],
targets: [
.target(name: "MyPackage", dependencies: ["ChessKit"]),
]
)To render a ready-to-use SwiftUI board with drag-and-drop support, depend on the ChessKitUI product:
.target(name: "MyPackage", dependencies: ["ChessKitUI"])To install ChessKit via CocoaPods, just add a dependencie to your Podfile:
target 'MyApp' do
pod 'ChessKit'
endChessKit is well covered by documentation. But anyway here you can find an example describing how to start working with ChessKit faster.
import ChessKit
let italianGameFen = "r1bqk1nr/pppp1ppp/2n5/2b1p3/2B1P3/5N2/PPPP1PPP/RNBQK2R w KQkq - 4 4"
let italianGamePosition = FenSerialization.default.deserialize(fen: italianGameFen)
let game = Game(position: italianGamePosition)
print("Number of available moves: \(game.legalMoves.count)")
game.make(move: "b2b4")
let evansGambitFen = FenSerialization.default.serialize(position: game.position)
print("Evans gambit fen: \(evansGambitFen)")
game.make(move: Move(string: "c5b4"))
let evansGambitAcceptedFen = FenSerialization.default.serialize(position: game.position)
print("Evans gambit accepted fen: \(evansGambitAcceptedFen)")
print("List of moves in game: \(game.movesHistory)")
print("List of pieces on board: \(game.position.board.enumeratedPieces())")ChessKitUI ships a drop-in ChessBoardView that uses the same move generator under the hood. Highlights appear when selecting a piece; moves can be made by tapping or dragging a piece to its destination.
import SwiftUI
import ChessKitUI
struct ContentView: View {
@StateObject private var board = ChessBoardViewModel()
var body: some View {
VStack(spacing: 12) {
ChessBoardView(viewModel: board)
.frame(width: 360, height: 360)
Text(board.turn == .white ? "White to move" : "Black to move")
.font(.headline)
}
.padding()
}
}ChessBoardView loads piece assets by name from your app target. Add SVG (or PDF/PNG) assets titled wp, wn, wb, wr, wq, wk and their black counterparts bp, bn, bb, br, bq, bk to your asset catalog; the board will render those images automatically.
Please follow the git-flow notation and make sure that all tests are passing before contributing. Your questions and pull requests are welcome.
We are using semantic versioning.
ChessKit is released under the MIT license. See LICENSE for details.