Game

public protocol Game: Equatable

A representation of a game’s state.

Requires

Must be immutable & have value semantics.

Note

Depending on the game’s nature it may be appropriate to model the game’s state as a history of moves, in addition to a simple snapshot.

Snapshot Only:

enum ChessPlayer: Strategist.Player {
    case White
    case Black
}
enum ChessPiece {
    case King, Queen, Rook, Bishop, Knight, Pawn
}
struct ChessGame: Strategist.State {
    let board: [ChessPiece?]
    let player: ChessPlayer?
}

Snapshot + History:

enum GoPlayer: Strategist.Player {
    case White
    case Black
}
enum GoStone {
    case White, Black
}
struct GoGame: Strategist.Game {
    let board: [GoStone?]
    let moves: [GoMove]
    let player: GoPlayer?
}

  • The player who the turn was handed over by last player’s move or the game’s initial player if no move has been made yet.

    Declaration

    Swift

    var currentPlayer: Player { get }
  • A representation of a game’s moves.

    Declaration

    Swift

    associatedtype Move: Strategist.Move
  • A representation of a game’s evaluation.

    Declaration

    Swift

    associatedtype Score: Strategist.Score
  • A representation of a game’s state.

    Declaration

    Swift

    associatedtype Player: Strategist.Player
  • Advance the game by applying a move to the game’s current state and advancing the players’ turn.

    Declaration

    Swift

    func update(move: Move) -> Self

    Return Value

    An updated game at the newly calculated state.

  • Checks whether two players are expected to cooperate.

    Declaration

    Swift

    func playersAreAllied(players: (Player, Player)) -> Bool
  • All available moves for the next turn’s player given the game’s current state.

    • recommended: Generate the moves lazily to reduce the memory overhead.

    Declaration

    Swift

    func availableMoves() -> AnyGenerator<Move>
  • Evaluate the game at its current state for the current player.

    Declaration

    Swift

    func evaluate(forPlayer player: Player) -> Evaluation<Score>

    Return Value

    Evaluation of game’s current state from the perspective of the game’s current player.

  • evaluate() Extension method

    Evaluate the game at its current state for the current player. had - returns: Evaluation of game’s current state from the perspective of the game’s current player.

    Declaration

    Swift

    public func evaluate() -> Evaluation<Score>

    Return Value

    Evaluation of game’s current state from the perspective of the game’s current player.

  • isFinished Extension method

    Checks whether the game has reached a final state.

    Declaration

    Swift

    public var isFinished: Bool

    Return Value

    false iff self.evaluate() would return .Ongoing(_), otherwise true.