Connect Four v1.0.0 ConnectFour.GameServer View Source

A Connect Four game, stored in a GenServer.

Link to this section Summary

Functions

Returns a specification to start this module under a supervisor.

Get a list of legal moves for the current position.

Look at the state of the game.

Make a move. If any of the moves are invalid, none (including any valid ones preceding the invalid one) will be played.

Restart the game. The game does not need to have a result to be restarted.

Start a GenServer process for a Connect Four game.

Link to this section Types

Link to this type

moves_and_result()

View Source
moves_and_result() :: %{
  moves: ConnectFour.Game.moves(),
  result: ConnectFour.Game.result()
}

Link to this section Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Look at the state of the game.

Examples

iex> {:ok, pid} = GameServer.start_link()
iex> GameServer.move(pid, [4, 5, 4])
{:ok, %{moves: [4, 5, 4], result: nil}}
iex> GameServer.look(pid)
{:ok, %{moves: [4, 5, 4], result: nil}}

Works for finished games, too.

Examples

iex> {:ok, pid} = GameServer.start_link()
iex> GameServer.move(pid, [4, 5, 4, 5, 4, 5])
{:ok, %{moves: [4, 5, 4, 5, 4, 5], result: nil}}
iex> GameServer.move(pid, 4)
{:ok, %{moves: [4, 5, 4, 5, 4, 5, 4], result: :yellow_wins}}
iex> GameServer.look(pid)
{:ok, %{moves: [4, 5, 4, 5, 4, 5, 4], result: :yellow_wins}}

Make a move. If any of the moves are invalid, none (including any valid ones preceding the invalid one) will be played.

Examples

iex> {:ok, pid} = GameServer.start_link()
iex> GameServer.move(pid, 4)
{:ok, %{moves: [4], result: nil}}
iex> GameServer.move(pid, 5)
{:ok, %{moves: [4, 5], result: nil}}

iex> {:ok, pid} = GameServer.start_link()
iex> GameServer.move(pid, [4, 5])
{:ok, %{moves: [4, 5], result: nil}}

iex> {:ok, pid} = GameServer.start_link()
iex> GameServer.move(pid, [4, 7])
{:error, "One or more invalid moves"}

iex> {:ok, pid} = GameServer.start_link()
iex> GameServer.move(pid, [1, 1, 2, 2, 3, 3, 4])
{:ok, %{moves: [1, 1, 2, 2, 3, 3, 4], result: :yellow_wins}}
iex> GameServer.move(pid, 4)
{:error, "Game is over"}
Link to this function

restart(pid)

View Source
restart(pid()) :: :ok

Restart the game. The game does not need to have a result to be restarted.

Examples

iex> {:ok, pid} = GameServer.start_link()
iex> GameServer.move(pid, 4)
{:ok, %{moves: [4], result: nil}}
iex> GameServer.restart(pid)
:ok
iex> GameServer.move(pid, 4)
{:ok, %{moves: [4], result: nil}}

Start a GenServer process for a Connect Four game.

Examples

iex> {:ok, pid} = GameServer.start_link()
iex> Process.alive?(pid)
true