Packages
islands_client_player
0.1.39
0.1.45
0.1.43
0.1.42
0.1.41
0.1.40
0.1.39
0.1.38
0.1.37
0.1.36
0.1.35
0.1.34
0.1.33
0.1.32
0.1.31
0.1.30
0.1.29
0.1.28
0.1.27
0.1.26
0.1.25
0.1.24
0.1.23
0.1.22
0.1.21
0.1.20
0.1.19
0.1.18
0.1.17
0.1.16
0.1.15
0.1.14
0.1.13
0.1.12
0.1.11
0.1.10
0.1.9
0.1.8
0.1.7
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
Models a player in the Game of Islands.
Current section
Files
Jump to
Current section
Files
lib/islands/client/player.ex
# ┌─────────────────────────────────────────────────────────────────┐
# │ Inspired by the course "Elixir for Programmers" by Dave Thomas. │
# └─────────────────────────────────────────────────────────────────┘
defmodule Islands.Client.Player do
@moduledoc """
Models a player in the _Game of Islands_.
##### Inspired by the course [Elixir for Programmers](https://codestool.coding-gnome.com/courses/elixir-for-programmers) by Dave Thomas.
"""
alias Islands.Client.{GameOver, Input, Mover, React, State}
alias Islands.Tally
@doc """
Reacts to a game state, makes a move and repeats until the game is over.
Player1 reacts to game state:
- `:initialized` by waiting for game state `:players_set`
- `:players_set` by waiting for game state `:player1_turn`
- `:player2_turn` by waiting for game state `:player1_turn` or `:game_over`
- `:game_over` by exiting the game
Player2 reacts to game state:
- `:players_set` by waiting for game state `:player2_turn`
- `:player1_turn` by waiting for game state `:player2_turn` or `:game_over`
- `:game_over` by exiting the game
"""
# :initialized, :players_set, :player1_turn, :player2_turn, :game_over
@spec play(State.t()) :: no_return
def play(%State{tally: %Tally{game_state: game_state}} = state) do
React.react_to(state, game_state) |> continue()
end
def play(_error) do
self() |> Process.exit(:normal)
end
## Private functions
@spec continue(State.t()) :: no_return
defp continue(%State{tally: %Tally{game_state: :game_over}} = state) do
# This player was notified of the `:game_over` game state.
# The oppponent player caused the `:game_over` game state.
GameOver.exit(state, end_game: false)
end
defp continue(state) do
:ok = Tally.summary(state.tally, state.player_id)
state
|> Input.accept_move()
|> Mover.make_move()
|> play()
end
end