Game

com.phasmidsoftware.gambit.game.Game
trait Game[S, M, Pl]

Typeclass: describes the rules of a game in terms of state S, move M, and player identity Pl.

A Game instance captures everything the GameRunner needs to know about the mechanics of a specific game — how moves are applied, whose turn it is, where the game starts, and how many players are involved.

This separates game rules (Game[S, M, Pl]) from game strategy (Player[S, M, Pl]) and game execution (GameRunner[P, S, M, Pl]).

Type parameters

M

the move type.

Pl

the player identity type (e.g., Boolean for two-player, an enum for bridge's four seats).

S

the state type.

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Abstract methods

def applyMove(s: S, m: M, pl: Pl): S

Apply a move made by player pl to state s, returning the new state.

Apply a move made by player pl to state s, returning the new state.

Value parameters

m

the move to apply.

pl

the player making the move.

s

the current state.

Attributes

Returns

the new state after the move.

def moves(s: S): Seq[M]

The legal moves available from state s. Used by MCTS and other players that need raw move values rather than State transitions.

The legal moves available from state s. Used by MCTS and other players that need raw move values rather than State transitions.

Value parameters

s

the current state.

Attributes

Returns

a sequence of legal moves.

def nextPlayer(s: S, current: Pl): Pl

Given the current state and the player who just moved, return the identity of the next player to move.

Given the current state and the player who just moved, return the identity of the next player to move.

Value parameters

current

the player who just moved.

s

the state after the move was applied.

Attributes

Returns

the next player.

def players: Seq[Pl]

The ordered sequence of all player identities. Used by GameRunner to initialise the players map and notify all players of the game result.

The ordered sequence of all player identities. Used by GameRunner to initialise the players map and notify all players of the game result.

Attributes

def start: S

The starting state of the game.

The starting state of the game.

Attributes

def startingPlayer: Pl

The player who moves first.

The player who moves first.

Attributes

Concrete methods

def currentPlayer[P](s: S)(using state: State[P, S]): Pl

The player whose turn it is to make the NEXT move from state s.

The player whose turn it is to make the NEXT move from state s.

To be completely unambiguous: if the game is in state s, and we are about to call applyMove, the player returned by currentPlayer(s) is the one who will make that move. It is NOT the player who made the last move to reach s — that player has already moved and is waiting.

Example (TicTacToe, Pl = Boolean): empty board → currentPlayer = true (X moves first) after X plays → currentPlayer = false (O moves next) after O plays → currentPlayer = true (X moves next)

Default implementation for two-player games: returns startingPlayer when state.isFirstPlayerToMove(s) is true, otherwise the other player. Override for games with more than two players.

Value parameters

s

the current state.

state

the implicit State[P, S] used to determine move parity.

Attributes

Returns

the identity of the player who is about to move.

def winner(s: S, current: Pl): GameResult[Pl]

Determine the winner from a terminal state. Called by GameRunner when State.isGoal returns Some(true). The current parameter is the player who is about to move — i.e., the player who did NOT make the winning move.

Determine the winner from a terminal state. Called by GameRunner when State.isGoal returns Some(true). The current parameter is the player who is about to move — i.e., the player who did NOT make the winning move.

Default implementation for zero-sum two-player games: the player who is NOT current wins (+1), current loses (-1). Override for multiplayer or non-zero-sum games.

Value parameters

current

the player whose turn it would have been next.

s

the terminal state.

Attributes

Returns

a GameResult mapping each player to their score.