Skip to content

♟️ A Swift package for implementing chess logic.

License

Notifications You must be signed in to change notification settings

Dscee/chesskit-swift

 
 

Repository files navigation

♟️ ChessKit

checks codecov

A Swift package for efficiently implementing chess logic.

Usage

  • Add a package dependency to your Xcode project or Swift Package:
.package(url: "https://github.com/chesskit-app/chesskit-swift", from: "0.5.0")
  • Next you can import ChessKit to use it in your Swift code:
import ChessKit

// ...

Features

  • Representation of chess elements
    • Piece
    • Square
    • Move
    • Position
    • Board
    • Clock
    • Game
    • Special moves (castling, en passant)
  • Move validation
  • Chess notation string parsing
    • PGN
    • FEN
    • SAN

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()
board.move(pieceAt: .e2, to: .e4)           // move pawn at e2 to e4
  • Check move legality
let board = Board()
print(board.canMove(pieceAt: .a1, to: .a8)) // returns false
  • Check legal moves
let board = Board()
print(board.legalMoves(forPieceAt: .e2))    // returns [.e3, .e4]
  • Parse FEN into a Position object
// 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
  • Similarly, parse PGN (into Game) or SAN (into Move).
// 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

Author

@pdil

License

ChessKit is distributed under the MIT License.

About

♟️ A Swift package for implementing chess logic.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Swift 100.0%