forked from alecho/chex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchex.ex
62 lines (48 loc) · 1.01 KB
/
chex.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
defmodule Chex do
@moduledoc """
Public API for Chex.
"""
import DynamicSupervisor, only: [start_child: 2, terminate_child: 2]
@server_mod Chex.DynamicGameServerSupervisor
@doc """
Creates a new game.
## Examples
iex> {:ok, pid} = Chex.new_game()
iex> pid |> is_pid()
true
"""
def new_game(fen) when is_binary(fen) do
start_child(
@server_mod,
{Chex.Server, [fen: fen]}
)
end
def new_game() do
start_child(
@server_mod,
Chex.Server
)
end
def move(pid, move) when is_pid(pid) and byte_size(move) == 4 do
GenServer.call(pid, {:move, move})
end
def engine_move(pid) when is_pid(pid) do
GenServer.call(pid, :engine_move)
end
def state(pid) when is_pid(pid) do
GenServer.call(pid, :state)
end
@doc """
End a game process.
## Examples
iex> {:ok, pid} = Chex.new_game()
iex> pid|> Chex.end_game()
:ok
"""
def end_game(pid) do
terminate_child(
@server_mod,
pid
)
end
end