Connect4

com.phasmidsoftware.gambit.examples.connect4.Connect4
See theConnect4 companion object
case class Connect4(xBits: Long, oBits: Long, heights: Vector[Int])

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
Graph
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all

Members list

Value members

Concrete methods

def play(col: Int, isX: Boolean): Connect4

Drop a piece into the given column. The piece lands on the lowest open row. Does not validate that the column is open — callers should check open.

Drop a piece into the given column. The piece lands on the lowest open row. Does not validate that the column is open — callers should check open.

Value parameters

col

the column index (0..6).

isX

true if X is playing, false if O.

Attributes

Returns

the new Connect4 state.

def render: String

Render the board as a String, top row first. X = X, O = 0, empty = .

Render the board as a String, top row first. X = X, O = 0, empty = .

Attributes

override def toString: String

Returns a string representation of the object.

Returns a string representation of the object.

The default representation is platform dependent.

Attributes

Returns

a string representation of the object.

Definition Classes
Any

Inherited methods

def productElementNames: Iterator[String]

Attributes

Inherited from:
Product
def productIterator: Iterator[Any]

Attributes

Inherited from:
Product

Concrete fields

lazy val isFull: Boolean

Whether the board is full (draw condition, assuming no winner).

Whether the board is full (draw condition, assuming no winner).

Attributes

lazy val open: Seq[Int]

The open (non-full) column indices, in order 0..6.

The open (non-full) column indices, in order 0..6.

Attributes

lazy val player: Boolean

The player who just moved to create this state. true = X just moved (odd number of total pieces). false = O just moved or empty board (even number).

The player who just moved to create this state. true = X just moved (odd number of total pieces). false = O just moved or empty board (even number).

Attributes

lazy val winner: Option[Boolean]

The winner of this position, if any. Some(true) = X wins, Some(false) = O wins, None = no winner yet.

The winner of this position, if any. Some(true) = X wins, Some(false) = O wins, None = no winner yet.

Attributes