mirror of
https://github.com/chesskit-app/chesskit-swift.git
synced 2026-05-16 11:50:35 +00:00
851083620a9ab495946736d483bec38bc9f330ba
♟️ ChessKit
A Swift package for efficiently implementing chess logic.
For a related Swift package that contains chess engines such as Stockfish, see chesskit-engine.
Usage
-
Add
chesskit-swiftas a dependency -
Next, import
ChessKitto use it in Swift code:
import ChessKit
// ...
Features
- Representation of chess elements
Piece: represents a single piece on the board, given by its color, type, and location on the boardSquare: represents a square on the boardMove: represents a piece move, along with other metadata such as captures, annotations, and disambiguationsPosition: represents a single position on the chess boardBoard: validate and make moves in accordance with chess rulesGame: manage positions throughout a game and PGN tags- Special moves (castling, en passant): handled automatically by
PositionandBoard
- Move validation
- Implemented using highly performant
UInt64bitboards.
- Implemented using highly performant
- Move branching and variations
- Implemented using a performant tree-like data structure
MoveTree.
- Implemented using a performant tree-like data structure
- Pawn promotion handling
- Game states (check, stalemate, checkmate, draws)
- Chess notation string parsing
PGNParserFENParserSANParserEngineLANParser(for use with UCI engines)
Examples
- Create a board with the standard starting position:
let board = Board()
- Create a board with a custom starting position using FEN:
if let position = Position(fen: "rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2") {
let board = Board(position: position)
print(board)
}
// 8 ♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜
// 7 ♟ ♟ · ♟ ♟ ♟ ♟ ♟
// 6 · · · · · · · ·
// 5 · · ♟ · · · · ·
// 4 · · · · ♙ · · ·
// 3 · · · · · ♘ · ·
// 2 ♙ ♙ ♙ ♙ · ♙ ♙ ♙
// 1 ♖ ♘ ♗ ♕ ♔ ♗ · ♖
// a b c d e f g h
//
// (see `ChessKitConfiguration` for printing options)
- Move pieces on the board
let board = Board()
// 8 ♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜
// 7 ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟
// 6 · · · · · · · ·
// 5 · · · · · · · ·
// 4 · · · · · · · ·
// 3 · · · · · · · ·
// 2 ♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙
// 1 ♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖
// a b c d e f g h
// move pawn at e2 to e4
board.move(pieceAt: .e2, to: .e4)
// 8 ♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜
// 7 ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟
// 6 · · · · · · · ·
// 5 · · · · · · · ·
// 4 · · · · ♙ · · ·
// 3 · · · · · · · ·
// 2 ♙ ♙ ♙ ♙ · ♙ ♙ ♙
// 1 ♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖
// a b c d e f g h
- Check move legality
let board = Board()
print(board.canMove(pieceAt: .a1, to: .a8)) // false
- Check legal moves
let board = Board()
print(board.legalMoves(forPieceAt: .e2)) // [.e3, .e4]
// parse FEN using Position initializer
let fen = "rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2"
let position = Position(fen: fen)
// convert Position to FEN string
let fenString = position.fen
// parse PGN using Game initializer
let game = Game(pgn: "1. e4 e5 2. Nf3")
// convert Game to PGN string
let pgnString = game.pgn
// parse the move text "e4" from the starting position
let move = Move(san: "e4", in: .standard)
// convert Move to SAN string
let sanString = move.san
Command Line
The ChessKit command line interface (chesskit-cli) can be used to explore and test ChessKit features.
The interface can be accessed by running swift run chesskit-cli from the project directory. The available commands will be displayed on screen and can be used to manipulate the board.
License
ChessKit is distributed under the MIT License.
Description
Languages
Swift
100%