-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d392e7f
commit c0c033c
Showing
3 changed files
with
208 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
defmodule TwentyFortyEight.Test.Helpers do | ||
@moduledoc false | ||
|
||
alias TwentyFortyEight.Game.Board | ||
|
||
def ascii_to_board(ascii) do | ||
rows = | ||
ascii | ||
|> String.trim() | ||
|> String.split("\n") | ||
|> Enum.map(&String.split/1) | ||
|
||
num_rows = Enum.count(rows) | ||
if num_rows == 0, do: raise("Need non-zero number of rows.") | ||
|
||
[num_cols | _] = row_sizes = Enum.map(rows, &Enum.count/1) | ||
consistent_rows? = Enum.all?(row_sizes, fn row_size -> row_size == num_cols end) | ||
if not consistent_rows?, do: raise("All rows must have equal length.") | ||
|
||
cells = | ||
for {row, row_idx} <- Enum.with_index(rows), | ||
{character, col_idx} <- Enum.with_index(row), | ||
into: %{} do | ||
{{1 + row_idx, 1 + col_idx}, ascii_to_cell_value(character)} | ||
end | ||
|
||
%Board{num_rows: num_rows, num_cols: num_cols, cells: cells} | ||
end | ||
|
||
defp ascii_to_cell_value("-"), do: nil | ||
|
||
defp ascii_to_cell_value("*"), do: :obstacle | ||
|
||
defp ascii_to_cell_value(ascii), do: String.to_integer(ascii) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.