com.phasmidsoftware.gambit.examples.connect4

Members list

Type members

Classlikes

class AlphaBetaPlayerConnect4(me: Boolean, depth: Int = ...)(using state: State[Connect4, Connect4], game: Game[Connect4, Int, Boolean], ttCache: TTCache[(Long, Long)]) extends AlphaBetaPlayer[Connect4, Connect4, Int, Boolean, (Long, Long)]

A Connect Four AI player using the alpha-beta pruning optimization technique for minimax search. The player aims to determine the optimal moves for winning or forcing a draw in the game.

A Connect Four AI player using the alpha-beta pruning optimization technique for minimax search. The player aims to determine the optimal moves for winning or forcing a draw in the game.

Value parameters

depth

The maximum depth of the search tree used in alpha-beta pruning. Defaults to 6.

keyFn

An optional function that generates a unique key for the Transposition Table (TT), to identify equivalent game states. Defaults to None. Uses contextual abstractions: - state for managing the transformation of game states. - game for game-specific logic, including rules and evaluation utilities. - ttCache as a caching mechanism for storing precomputed game states during alpha-beta expansion for performance optimization. Extends: - AlphaBetaPlayer, a generic implementation of alpha-beta search AI for turn-based games, parameterized by the game state, result, move, player type, and Transposition Table key type.

me

Specifies the player's identity in the game. True indicates the player is X, false indicates the player is O.

Attributes

Supertypes
class AlphaBetaPlayer[Connect4, Connect4, Int, Boolean, (Long, Long)]
trait Player[Connect4, Int, Boolean]
class Object
trait Matchable
class Any
case class Connect4(xBits: Long, oBits: Long, heights: Vector[Int])

Connect Four board state using a column-major bitboard representation.

Connect Four board state using a column-major bitboard representation.

Two Longs (xBits, oBits) represent the pieces of each player. Bit index for cell (row, col) = col * 7 + row, where row 0 is the bottom. Sentinel bits at positions 6, 13, 20, 27, 34, 41, 48 are never set during play; they prevent horizontal win detection from wrapping across columns.

heights tracks the next available row index per column (0..5). A column is full when heights(col) == Connect4.rows.

You can read more about the game here: https://en.wikipedia.org/wiki/Connect_Four

Value parameters

heights

next available row per column

oBits

bit positions of O's pieces

xBits

bit positions of X's pieces

Attributes

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

Attributes

Companion
class
Supertypes
trait Product
trait Mirror
class Object
trait Matchable
class Any
Self type
Connect4.type
final class Connect4Demo

Attributes

Supertypes
class Object
trait Matchable
class Any

Factory for a Connect Four GameRunner.

Factory for a Connect Four GameRunner.

Attributes

Supertypes
class Object
trait Matchable
class Any
Self type

State[Connect4, Connect4] typeclass instance for Connect4.

State[Connect4, Connect4] typeclass instance for Connect4.

P = S = Connect4: the proto-state and state are the same type. construct takes (newState, previousState) and returns newState.

Attributes

Supertypes
trait Ordering[Connect4]
trait PartialOrdering[Connect4]
trait Equiv[Connect4]
trait Serializable
trait Comparator[Connect4]
class Object
trait Matchable
class Any
Show all
Self type

Runs a round-robin tournament between all Connect Four player types and prints the league table to stdout.

Runs a round-robin tournament between all Connect Four player types and prints the league table to stdout.

Player parameters (depths, iteration counts, exploration constant) are read from application.conf via com.phasmidsoftware.gambit.game.GambitConfig. Edit the config file to tune contestants without recompiling.

Players entered:

  • RandomPlayer
  • HeuristicPlayer (one-ply greedy)
  • AlphaBeta(d=d1) (minimax, depth from config)
  • AlphaBeta(d=d2) (minimax, deeper depth from config)
  • MCTS(i=i1) (Monte Carlo, iteration count from config)
  • MCTS(i=i2) (Monte Carlo, higher iteration count from config)

Attributes

Supertypes
class Object
trait Matchable
class Any
Self type
class HeuristicPlayer extends Player[Connect4, Int, Boolean]

A player that greedily selects the highest-heuristic successor.

A player that greedily selects the highest-heuristic successor.

Attributes

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

A player that selects moves uniformly at random.

A player that selects moves uniformly at random.

Attributes

Supertypes
trait Player[Connect4, Int, Boolean]
class Object
trait Matchable
class Any

Value members

Concrete methods

def Connect4Demo(): Unit

Demonstrates Connect4 games between player types, printing the board state and heuristic score after each move.

Demonstrates Connect4 games between player types, printing the board state and heuristic score after each move.

Each matchup is played twice — home (X) and away (O) — to account for the first-mover advantage X enjoys in Connect Four.

Matchups: MCTS vs Random, MCTS vs Heuristic, Heuristic vs Random.

Attributes

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

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

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

Attributes

Render a Connect4 board with its heuristic score. Adds column index header for readability.

Render a Connect4 board with its heuristic score. Adds column index header for readability.

Attributes

Givens

Givens

given connect4Game(using x$1: State[Connect4, Connect4]): connect4Game

The Game typeclass instance for Connect Four.

The Game typeclass instance for Connect Four.

S = Connect4 M = Int (column index, 0..6) Pl = Boolean (true = X, moves first; false = O, moves second)

Attributes