chesskit-app / chesskit-swift

♟️ Swift package for implementing chess logic.
MIT License
7 stars 6 forks source link
chess swift swift-package-manager

♟️ ChessKit

checks codecov

A Swift package for efficiently implementing chess logic.

For a related Swift package that contains chess engines such as Stockfish, see chesskit-engine.

Usage

  1. Add chesskit-swift as a dependency

  2. Next, import ChessKit to use it in Swift code:

    
    import ChessKit

// ...


## Features

* Representation of chess elements
    * `Piece`
    * `Square`
    * `Move`
    * `Position`
    * `Board`
    * `Clock`
    * `Game`
    * Special moves (castling, en passant)
* Move validation
    * Implemented using highly performant `UInt64` [bitboards](https://www.chessprogramming.org/Bitboards).
* Pawn promotion handling
* Game states (check, stalemate, checkmate, etc.)
* Chess notation string parsing
    * PGN
    * FEN
    * SAN

## Examples

* Create a board with the standard starting position:
``` swift
let board = 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
``` swift
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

// convert Position to FEN string let fenString = position.fen


* Similarly, parse [PGN](https://en.wikipedia.org/wiki/Portable_Game_Notation) (into `Game`) or [SAN](https://en.wikipedia.org/wiki/Algebraic_notation_(chess)) (into `Move`).
``` swift
// 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

License

ChessKit is distributed under the MIT License.