Instantiate with arguments for CarloGamePlayer and a limit for the number turns to rollout in a single search iteration:
let computer = Computer(for: .two, maxRolloutDepth: 5)
Call the .iterate() method to perform one search iteration in the tree, .bestMove to get the best move (so far) as found by search algorithm, and .uproot(to:) to recycle the tree and update the internal game state:
var game = ConnectThreeGame(length: 10, currentPlayer: .one)
/// 0000000000
game = game.update(4)
/// 0000100000
game = game.update(0)
/// 2000100000
game = game.update(7)
/// 2000100000
game = game.update(2)
/// 2020100000
game = game.update(9)
/// 2020100001 ... player 2 can win if move => 1
computer.uproot(to: game)
for _ in 0..<50 {
computer.iterate()
}
let move = computer.bestMove!
game = game.update(move)
/// 2220100001 ... game over
Install
If you use Swift Package Manager adding Carlo as a dependency is as easy as adding it to the dependencies of your Package.swift:
About
A Monte Carlo Tree Search (MCTS) library for turn-based games built with Swift
Import
Import Carlo by adding the following line to any
.swiftfile:Implement
Implement Carlo by designing player, move, and game structs that conform to the
CarloGamePlayer,CarloGameMove, andCarloGameprotocols.While the first two protocols don’t explicitly require anything, “conforming” to them might look like this:
Conforming to
CarloGamerequires the following:Properly implemented it might look like this:
Use
Use Carlo by scaffolding a
CarloTacticianon aCarloGame:Instantiate with arguments for
CarloGamePlayerand a limit for the number turns to rollout in a single search iteration:Call the
.iterate()method to perform one search iteration in the tree,.bestMoveto get the best move (so far) as found by search algorithm, and.uproot(to:)to recycle the tree and update the internal game state:Install
If you use Swift Package Manager adding Carlo as a dependency is as easy as adding it to the
dependenciesof yourPackage.swift: