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
moves_and_result()
View Sourcemoves_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.
legal_moves(pid)
View Sourcelegal_moves(pid()) :: {:ok, ConnectFour.Game.moves()}
Get a list of legal moves for the current position.
Examples
iex> {:ok, pid} = GameServer.start_link()
iex> GameServer.legal_moves(pid)
{:ok, [0, 1, 2, 3, 4, 5, 6]}
iex> {:ok, pid} = GameServer.start_link()
iex> GameServer.move(pid, [3, 3, 3, 3, 3, 3])
{:ok, %{moves: [3, 3, 3, 3, 3, 3], result: nil}}
iex> GameServer.legal_moves(pid)
{:ok, [0, 1, 2, 4, 5, 6]}
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}}
move(pid, move_or_moves)
View Sourcemove(pid(), ConnectFour.Game.column() | ConnectFour.Game.moves()) :: {:ok, moves_and_result()} | {:error, String.t()}
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"}
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