com.phasmidsoftware.gambit.examples.tictactoe

Members list

Type members

Classlikes

sealed trait BeadResult

The result of a game from one player's perspective.

The result of a game from one player's perspective.

Attributes

Supertypes
class Object
trait Matchable
class Any
Known subtypes
object Draw
object Loss
object Win
case class Board(sequence: Int, value: Int)

This class represents 9 x 2 bits, at the high end of the 32-bit word.

This class represents 9 x 2 bits, at the high end of the 32-bit word.

NOTE: this used to extend AnyVal before we added the sequence parameter.

Value parameters

value

the bit value of this board. NOTE: that the low 14 bits of this value should always be zero.

Attributes

Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
case object Draw extends BeadResult

Attributes

Supertypes
trait Singleton
trait Product
trait Mirror
trait Serializable
trait Product
trait Equals
trait BeadResult
class Object
trait Matchable
class Any
Show all
Self type
Draw.type
class HeuristicPlayer(implicit state: State[Board, TicTacToe]) extends Player[TicTacToe, Int, Boolean]

A player that uses the existing heuristic (TicTacToeState$.heuristic) to greedily pick the best available move. Useful as a strong baseline.

A player that uses the existing heuristic (TicTacToeState$.heuristic) to greedily pick the best available move. Useful as a strong baseline.

Attributes

Supertypes
trait Player[TicTacToe, Int, Boolean]
class Object
trait Matchable
class Any
case object Loss extends BeadResult

Attributes

Supertypes
trait Singleton
trait Product
trait Mirror
trait Serializable
trait Product
trait Equals
trait BeadResult
class Object
trait Matchable
class Any
Show all
Self type
Loss.type
case class Matchbox(beads: Map[Int, Int])

A Matchbox represents one position in the MENACE machine. It holds a bead count for each legal move (open cell index 0..8). Weighted random selection picks a move proportional to bead counts. After a game, beads are added (win) or removed (loss), with a floor of 1.

A Matchbox represents one position in the MENACE machine. It holds a bead count for each legal move (open cell index 0..8). Weighted random selection picks a move proportional to bead counts. After a game, beads are added (win) or removed (loss), with a floor of 1.

Value parameters

beads

a Map from cell index (0..8, row-major) to bead count.

Attributes

Companion
object
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
object Matchbox

Attributes

Companion
class
Supertypes
trait Product
trait Mirror
class Object
trait Matchable
class Any
Self type
Matchbox.type
class Matchboxes

Matchboxes is the MENACE registry: a mutable map from canonical board value to Matchbox. Canonicalization uses the 8-element dihedral group D4 (4 rotations × 2 for reflection via transpose), so symmetrically equivalent positions share one matchbox and learn together.

Matchboxes is the MENACE registry: a mutable map from canonical board value to Matchbox. Canonicalization uses the 8-element dihedral group D4 (4 rotations × 2 for reflection via transpose), so symmetrically equivalent positions share one matchbox and learn together.

The canonical form of a board is the minimum value over all 8 D4 transforms. (Minimum is arbitrary but deterministic — any fixed choice from the orbit works.)

Note: exchange (swapping X and O bits) is NOT included in canonicalization here. X always moves first so X-positions and O-positions are structurally distinct in the learning signal. We can revisit this if we want to halve the matchbox count.

Attributes

Companion
object
Supertypes
class Object
trait Matchable
class Any
object Matchboxes

Attributes

Companion
class
Supertypes
class Object
trait Matchable
class Any
Self type
Matchboxes.type
class MenacePlayer(val matchboxes: Matchboxes) extends Player[TicTacToe, Int, Boolean]

A MENACE player backed by a shared Matchboxes registry. Records the sequence of (position, move) pairs during a game so it can back-propagate the result afterwards.

A MENACE player backed by a shared Matchboxes registry. Records the sequence of (position, move) pairs during a game so it can back-propagate the result afterwards.

All cell indices in history are in original board orientation, matching what selectMove returns and what update expects.

Pl = Boolean: true = X (first player), false = O (second player).

Value parameters

matchboxes

the shared registry.

Attributes

Supertypes
trait Player[TicTacToe, Int, Boolean]
class Object
trait Matchable
class Any
implicit class Outcome(x: Option[Int])

Implicit class Outcome which allows for two Option[Int] values to be combined.

Implicit class Outcome which allows for two Option[Int] values to be combined.

NOTE: currently this class is not used.

Value parameters

x

an Option[Int] on the left of the &

Attributes

Supertypes
class Object
trait Matchable
class Any
class PerfectPlayer(implicit state: State[Board, TicTacToe]) extends Player[TicTacToe, Int, Boolean]

A perfect TicTacToe player built by running a full minimax evaluation over the game DAG using Visitor's post-order DFS.

A perfect TicTacToe player built by running a full minimax evaluation over the game DAG using Visitor's post-order DFS.

Construction (once, on first use):

  1. A mutable score map Map[TicTacToe, Int] is allocated.
  2. A given Evaluable[TicTacToe, Int] closes over the map; for terminal positions it scores directly; for internal positions it reads children's scores from the map (already populated in post-order).
  3. Traversal.dfs is called with DfsOrder.Post from TicTacToe.start. The default given VisitedSet[TicTacToe] ensures each position is evaluated exactly once (DAG, not tree).
  4. The completed score map drives chooseMove.

Minimax convention:

  • X maximises (score +1 = X wins, 0 = draw, -1 = O wins).
  • O minimises.
  • TicTacToe.player is true when it was X's turn to produce this board (i.e., X just moved), so when choosing the next move, we look at whose turn it is, which is !ttt.player for the node just settled.

Scores in the map are from X's perspective throughout.

Attributes

Supertypes
trait Player[TicTacToe, Int, Boolean]
class Object
trait Matchable
class Any
class RandomPlayer extends Player[TicTacToe, Int, Boolean]

A player that selects moves uniformly at random. Useful as a baseline opponent for MENACE to learn against.

A player that selects moves uniformly at random. Useful as a baseline opponent for MENACE to learn against.

Attributes

Supertypes
trait Player[TicTacToe, Int, Boolean]
class Object
trait Matchable
class Any
case class TicTacToe(board: Board, maybePrior: Option[TicTacToe] = ...)

Case class to represent a TicTacToe state (layout).

Case class to represent a TicTacToe state (layout).

Value parameters

board

the current state.

maybePrior

the prior state.

Attributes

Companion
object
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
object TicTacToe

Attributes

Companion
class
Supertypes
trait Product
trait Mirror
class Object
trait Matchable
class Any
Self type
TicTacToe.type
final class TicTacToeDemo

Attributes

Supertypes
class Object
trait Matchable
class Any

Factory for a TicTacToe GameRunner. Requires an implicit State[Board, TicTacToe] in scope.

Factory for a TicTacToe GameRunner. Requires an implicit State[Board, TicTacToe] in scope.

Attributes

Supertypes
class Object
trait Matchable
class Any
Self type

Shared utility methods for TicTacToe cell operations.

Shared utility methods for TicTacToe cell operations.

Attributes

Supertypes
class Object
trait Matchable
class Any
Self type
case object Win extends BeadResult

Attributes

Supertypes
trait Singleton
trait Product
trait Mirror
trait Serializable
trait Product
trait Equals
trait BeadResult
class Object
trait Matchable
class Any
Show all
Self type
Win.type

Types

type Cell = Option[Boolean]

If there is a "cell" (a matching square in the 3x3 grid), then: Some(true) for player X, and Some(false) for player 0. else None (no match).

If there is a "cell" (a matching square in the 3x3 grid), then: Some(true) for player X, and Some(false) for player 0. else None (no match).

Attributes

type Row = Int

Representation of a row (or column or diagonal) of a TicTacToe state. Only the six low bits are significant.

Representation of a row (or column or diagonal) of a TicTacToe state. Only the six low bits are significant.

CONSIDER using Row(x: Int) extends AnyVal

Attributes

type RowWithMask = (Row, Int)

Representation of a Row with a bit mask representing the difference between two states.

Representation of a Row with a bit mask representing the difference between two states.

Attributes

Value members

Concrete methods

def TicTacToeDemo(): Unit

Demonstrates TicTacToe games between all four player types. Each distinct pair plays twice — home (X) and away (O) — to account for the first-mover advantage X enjoys in TicTacToe.

Demonstrates TicTacToe games between all four player types. Each distinct pair plays twice — home (X) and away (O) — to account for the first-mover advantage X enjoys in TicTacToe.

Attributes

def playTicTacToeDemo(xPlayer: Player[TicTacToe, Int, Boolean], xName: String, oPlayer: Player[TicTacToe, Int, Boolean], oName: String, rng: Random): Option[Boolean]

Play a single TicTacToe game, printing each move with board and heuristic.

Play a single TicTacToe game, printing each move with board and heuristic.

Attributes

def renderTTTWithHeuristic(ttt: TicTacToe): String

Render a TicTacToe board with its heuristic score.

Render a TicTacToe board with its heuristic score.

Attributes

Givens

Givens

given tictactoeGame(using state: State[Board, TicTacToe]): tictactoeGame

The Game typeclass instance for TicTacToe.

The Game typeclass instance for TicTacToe.

S = TicTacToe M = Int (flat cell index, 0..8 row-major) Pl = Boolean (true = X, moves first; false = O, moves second)

Attributes

Implicits

Implicits

final implicit def Outcome(x: Option[Int]): Outcome

Implicit class Outcome which allows for two Option[Int] values to be combined.

Implicit class Outcome which allows for two Option[Int] values to be combined.

NOTE: currently this class is not used.

Value parameters

x

an Option[Int] on the left of the &

Attributes