Connect Four v1.0.0 ConnectFour.Game View Source

A Connect Four game.

Players are distinguished by game piece color (yellow and red). Moves are represented by the columns in which they are made.

To create a new game, create an empty ConnectFour.Game.t() struct (%ConnectFour.Game{}).

Yellow moves first.

Link to this section Summary

Types

One of seven Connect Four game columns, zero-indexed.

A list of Connect Four moves, describing a game. Yellow always moves first, so the first move in the list will always be yellow's.

A Connect Four player (yellow always moves first).

A ply is one move completed by one player. Plies are thus the total number of moves completed in the game so far.

A Connect Four game result. nil means that the game has not yet ended. :draws occur when all columns are full and no player has connected four.

t()

The representation of a Connect Four game. The struct contains five fields, three of which should be considered read-only, and two which should be considered private. None should ever be modified manually.

Functions

Get a list of all the legal moves for a game. Returns an empty list if the game is over.

Submit a move for whomever's turn it currently is by specifying a column (0 through 6).

Link to this section Types

Link to this type

column()

View Source
column() :: 0..6

One of seven Connect Four game columns, zero-indexed.

A list of Connect Four moves, describing a game. Yellow always moves first, so the first move in the list will always be yellow's.

Link to this type

player()

View Source
player() :: :yellow | :red

A Connect Four player (yellow always moves first).

A ply is one move completed by one player. Plies are thus the total number of moves completed in the game so far.

For example, if a game starts and yellow takes their turn (by "dropping" a game piece into a column) and then red does the same, that game has two plies.

Link to this type

result()

View Source
result() :: :yellow_wins | :red_wins | :draw | nil

A Connect Four game result. nil means that the game has not yet ended. :draws occur when all columns are full and no player has connected four.

Link to this type

t()

View Source
t() :: %ConnectFour.Game{
  bitboards: %{yellow: bitboard(), red: bitboard()},
  column_heights: column_heights(),
  moves: moves(),
  plies: non_neg_integer(),
  result: result()
}

The representation of a Connect Four game. The struct contains five fields, three of which should be considered read-only, and two which should be considered private. None should ever be modified manually.

The read-only fields are:

  • moves
  • plies
  • result

And the private fields are:

  • bitbords
  • column_heights

Link to this section Functions

Link to this function

move(game, column)

View Source
move(ConnectFour.Game.t(), column() | moves()) ::
  {:ok, ConnectFour.Game.t()} | {:error, String.t()}

Submit a move for whomever's turn it currently is by specifying a column (0 through 6).

Examples

iex> alias ConnectFour.Game
iex> {:ok, updated_game} = %Game{} |> Game.move(0)
iex> updated_game.moves
[0]

Make multiple moves at once by passing a list of moves.

Examples

iex> alias ConnectFour.Game
iex> {:ok, updated_game} = %Game{} |> Game.move([0, 1, 0])
iex> updated_game.moves
[0, 1, 0]