Swiftpack.co -  regexident/Strategist as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
regexident/Strategist
Algorithms for building strong immutable AIs for round-based games.
.package(url: "https://github.com/regexident/Strategist.git", from: "0.1.1")

Logo

Strategist

Strategist provides algorithms for building strong immutable AIs for round-based games.

Provided Algorithms:

  • Minimax Tree Search (with alpha-beta pruning)
  • Negamax Tree Search (with alpha-beta pruning)
  • Monte Carlo Tree Search

Example usage

Tic Tac Toe (Minimax Tree Search)

typealias Game = TicTacToeGame
typealias Player = TicTacToePlayer
typealias Policy = SimpleTreeSearchPolicy<Game>
typealias Strategy = MiniMaxTreeSearch<Game, Policy>

let players: [Player] = [.X, .O] // => [Human, Ai]
var game = Game(players: players)
let policy = Policy(maxMoves: 10, maxExplorationDepth: 10)
let strategy = Strategy(policy: policy)
while !game.evaluate().isFinal {
    let move: TicTacToeMove
    if game.currentPlayer == .White {
        move = askAndWaitForHumanPlayersMove(game.currentPlayer)
    } else {
        move = strategy.randomMaximizingMove(game)!
    }
    game = game.update(move) // moves turn and game state forward
}
print("Game ended with \(game.currentPlayer)'s \(game.evaluate()).")

Chess (Monte Carlo Tree Search)

typealias Game = ChessGame
typealias Heuristic = UpperConfidenceBoundHeuristic<Game>
typealias Policy = SimpleMonteCarloTreeSearchPolicy<Game, Heuristic>
typealias Strategy = MonteCarloTreeSearch<Game, Policy>

let players: [Player] = [.White, .Black] // => [Human, Ai]
var game = Game(players: players)
let heuristic = Heuristic(c: sqrt(2.0))
let policy = Policy(
    maxMoves: 100,
    maxExplorationDepth: 10,
    maxSimulationDepth: 10,
    simulations: 100,
    pruningThreshold: 1000,
    scoringHeuristic: heuristic
)
var strategy = Strategy(
    game: game,
    player: players[0],
    policy: policy
)
while !game.evaluate().isFinal {
    let move: ChessMove
    if game.currentPlayer == .White {
        move = askAndWaitForHumanPlayersMove(game.currentPlayer)
    } else {
        while stillPlentyOfTime() {
            strategy = strategy.refine()
        }
        move = strategy.randomMaximizingMove(game)!
    }
    strategy = strategy.update(move)
    game = game.update(move)
}    
print("Game ended with \(game.currentPlayer)'s \(game.evaluate()).")

Documentation

Online API documentation can be found here here.

Installation

Swift Package Manager

.Package(url: "https://github.com/regexident/Strategist.git")

Carthage (site)

github 'regexident/Strategist'

CocoaPods (site)

pod 'Strategist'

License

Strategist is available under the MPL-2.0 (tl;dr) license (see LICENSE file).

GitHub

link
Stars: 30
Last commit: 1 year ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API