Skip to content

Commit

Permalink
Refactor tests and make Dialyzer happy
Browse files Browse the repository at this point in the history
  • Loading branch information
denvaar committed Nov 26, 2020
1 parent 323af1c commit 9347466
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 73 deletions.
76 changes: 49 additions & 27 deletions lib/sternhalma/board.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ defmodule Sternhalma.Board do
def path(board, start, finish) do
neighbors = Hex.neighbors(start.position)

simple_path_exists? =
non_jump_possible? =
finish.marble == nil and
Enum.find(neighbors, fn {_direction, hex} -> hex == finish.position end)

if simple_path_exists? do
if non_jump_possible? do
[start, finish]
else
paths =
path_helper(
jump_move(
board,
nil,
start,
Expand Down Expand Up @@ -104,35 +104,24 @@ defmodule Sternhalma.Board do
backtrack(paths, current, [finish | path])
end

@type jump_direction :: Hex.direction() | nil
@type jump_direction :: nil | Hex.direction()

@spec path_helper(t(), jump_direction(), Cell.t(), Cell.t(), path_guide(), list(Cell.t())) ::
@spec jump_move(t(), jump_direction(), Cell.t(), Cell.t(), path_guide(), list(Cell.t())) ::
path_guide()
defp path_helper(_board, _jump_direction, _start, _finish, came_from, []), do: came_from
defp jump_move(_board, _jump_direction, _start, _finish, came_from, []), do: came_from

defp path_helper(_board, _jump_direction, _start, finish, came_from, [current | _cells])
defp jump_move(_board, _jump_direction, _start, finish, came_from, [current | _cells])
when finish.position == current.position do
came_from
end

defp path_helper(board, jump_direction, start, finish, came_from, [current | cells]) do
defp jump_move(board, jump_direction, start, finish, came_from, [current | cells]) do
cells_to_visit =
if jump_direction == nil do
Hex.neighbors(current.position)
else
[{nil, Hex.neighbor(current.position, jump_direction)}]
end
current
|> neighborz(jump_direction)
|> remove_invalid_cells(board)
|> Enum.map(fn {direction, hex} ->
{direction, Enum.find(board, &(&1.position == hex))}
end)
|> Enum.filter(fn {_direction, cell} ->
if jump_direction == nil do
cell.marble != nil
else
cell.marble == nil
end
end)
|> convert_hex_positions_to_cells(board)
|> filter_occupied_cells(jump_direction)
|> remove_visited_cells(came_from)

came_from =
Expand All @@ -149,21 +138,54 @@ defmodule Sternhalma.Board do
|> Enum.map(fn {_direction, cell} -> cell end)
|> Kernel.++(cells)

path_helper(board, jump_direction, start, finish, came_from, next_cells)
jump_move(board, jump_direction, start, finish, came_from, next_cells)
end

@spec remove_invalid_cells(list({Hex.direction(), Hex.t()}), t()) ::
list({Hex.direction(), Hex.t()})
@spec convert_hex_positions_to_cells(list({jump_direction(), Hex.t()}), t()) ::
list({jump_direction(), Cell.t()})
defp convert_hex_positions_to_cells(neighbors, board) do
Enum.reduce(neighbors, [], fn {direction, position}, acc ->
cell = Enum.find(board, &(&1.position == position))

if cell do
[{direction, cell} | acc]
else
acc
end
end)
end

@spec neighborz(Cell.t(), jump_direction()) :: list({jump_direction(), Hex.t()})
defp neighborz(cell, nil), do: Hex.neighbors(cell.position)
defp neighborz(cell, jump_direction), do: [{nil, Hex.neighbor(cell.position, jump_direction)}]

@spec remove_invalid_cells(list({jump_direction(), Hex.t()}), t()) ::
list({jump_direction(), Hex.t()})
defp remove_invalid_cells(neighbors, board) do
neighbors
|> Enum.filter(fn {_direction, neighbor} ->
Enum.any?(board, fn cell -> neighbor == cell.position end)
end)
end

@spec remove_visited_cells(list({Hex.direction(), Cell.t()}), path_guide()) :: list(Cell.t())
@spec remove_visited_cells(list({jump_direction(), Cell.t()}), path_guide()) ::
list({jump_direction(), Cell.t()})
defp remove_visited_cells(cells, came_from) do
cells
|> Enum.reject(fn {_direction, cell} -> Map.get(came_from, cell) end)
end

@spec filter_occupied_cells(list({jump_direction(), Cell.t()}), jump_direction()) ::
list({jump_direction(), Cell.t()})
defp filter_occupied_cells(cells, nil) do
Enum.reject(cells, fn {_direction, cell} ->
cell.marble == nil
end)
end

defp filter_occupied_cells(cells, _jump_direction) do
Enum.filter(cells, fn {_direction, cell} ->
cell.marble == nil
end)
end
end
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ defmodule Sternhalma.MixProject do
[
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
{:dialyxir, "~> 1.0", only: [:dev], runtime: false}
]
end
end
4 changes: 4 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
%{
"dialyxir": {:hex, :dialyxir, "1.0.0", "6a1fa629f7881a9f5aaf3a78f094b2a51a0357c843871b8bc98824e7342d00a5", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "aeb06588145fac14ca08d8061a142d52753dbc2cf7f0d00fc1013f53f8654654"},
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
}
Loading

0 comments on commit 9347466

Please sign in to comment.