Giter Site home page Giter Site logo

swiftchess's Introduction

SwiftChess

Version License Platform CI Status Twitter

SwiftChess is a chess engine written in Swift.

swiftchess

Features

  • Move validation
  • AI with three difficulty levels
  • Callbacks for check, checkmate and stalemate
  • Supports castling
  • Supports En Passent
  • Supports pawn promotion
  • Asyncronous AI move calculation

SwiftChess doesn't provide any UI, just all of the logic required to create a chess game. The example project contains a complete UIKit UI with touch handling that you start from if you like.

Example

The example application contains a complete implementation of SwiftChess.

Run Example/Example.xcodeproj

Basic Use

Start a game
// Make a human player
let whitePlayer = Human(color: .white)

// ... or an AI Player
let blackPlayer = AIPlayer(color: .black, configuration: AIConfiguration(difficulty: .hard))

// Create a game       
let game = Game(firstPlayer: whitePlayer, secondPlayer: blackPlayer)
Make a move
if let player = game.currentPlayer as? Human {

	let currentLocation = BoardLocation(x: 4, y: 1)
	let newLocation = BoardLocation(x: 4, y: 2)

	try! player.movePiece(from: currentLocation,
                        to: newLocation)
}
Tell the AI to make a move
if let player =  game.currentPlayer as? AIPlayer {
	player.makeMoveAsync()
}
Then just wait for the callbacks!
extension GameViewController: GameDelegate {

	func gameDidMovePiece(game: Game, piece: Piece, toLocation: BoardLocation) {
        // Move piece on board
    }
    
    func gameDidRemovePiece(game: Game, piece: Piece, location: BoardLocation) {
        // Remove piece from board 
    }
    
    func gameDidTransformPiece(game: Game, piece: Piece, location: BoardLocation) {
    	// A pawn was promoted!
    }
    
    func gameWonByPlayer(game: Game, player: Player) {
    	ShowAlert("Checkmate!")
    }
    
    func gameEndedInStaleMate(game: Game) {
     	ShowAlert("Stalemate!")
    }
    
    func gameDidChangeCurrentPlayer(game: Game) {
    	// Make another move        
    }
}

Persistence

The entire state of a SwiftChess game can be converted to and initialised from a Dictionary.

Get a snapshot of the current state:

let dictionary: [String: Any] = game.dictionaryRepresentation

Initialise a game with a previous snapshot:

let game = Game(dictionary: dictionary)

The returned dictionary stores all the information required to create a 'save game' feature. The player colors, AI difficulty, piece positions etc.

You can serialize this to JSON, save it to disk, send it over the network etc.

Other stuff

Make a castling move
if game.board.canColorCastle(color: .white, side: .kingSide) {
	player.performCastleMove(side: .kingSide)
}
Support pawn promotion
func promotedTypeForPawn(location: BoardLocation, 
                         player: Human, 
                         possiblePromotions: [Piece.PieceType], 
                         callback: @escaping (Piece.PieceType) -> Void) {

	// Show UI for the user to select one of the possible promotion types
	// then call the handler
	
	// ...or some games just promote to a queen
	callback(.queen)
}

Author

Follow me on twitter @SteveBarnegren

License

SwiftChess is available under the MIT license. See the LICENSE file for more info.

swiftchess's People

Contributors

sochalewski avatar stevebarnegren avatar yixiang avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.