Skip to content

Commit

Permalink
Start updates to BoardState for LiveView (#26)
Browse files Browse the repository at this point in the history
* Start updates to BoardState for LiveView

Begin work on utility functions for setting the status of a
BoardWord as being revealed when appropriate

* Remove scaffolded test

CI didn't like the scaffolded test for some reason

* Credo

Dammit Credo, nice catch
  • Loading branch information
adam-phillips authored Aug 5, 2020
1 parent cae419c commit 20b542b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/letter_lines_elixir.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule LetterLinesElixir do
alias LetterLinesElixir.BoardState
alias LetterLinesElixir.BoardWord

def generate_game() do
def generate_game do
words = [
BoardWord.new(5, 0, :v, "bunch"),
BoardWord.new(0, 1, :v, "chub"),
Expand Down
21 changes: 19 additions & 2 deletions lib/letter_lines_elixir/board_state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ defmodule LetterLinesElixir.BoardState do
def print_board(%BoardState{width: width, height: height} = board_state) do
for y <- 0..(height - 1) do
0..(width - 1)
|> Enum.map(&get_display_letter_at(board_state, &1, y))
|> Enum.map(&get_display_ascii_letter_at(board_state, &1, y))
|> Enum.join("")
|> IO.puts()
end
Expand All @@ -74,6 +74,23 @@ defmodule LetterLinesElixir.BoardState do
end
end

def get_display_letter_at(%BoardState{} = board_state, x, y) do
letter = BoardState.get_letter_at(board_state, x, y)

cond do
letter == :none -> :none
!letter_revealed?(board_state, x, y) -> :hidden
true -> letter
end
end

def reveal_word(%BoardState{words: words} = board_state, word) do
case put_in(words, [Access.filter(&match?(%{word: ^word}, &1)), :revealed?], true) do
^words -> {:error, :nothing_revealed}
new_words -> %BoardState{board_state | words: new_words}
end
end

defp letter_revealed?(%BoardState{words: words}, x, y) do
words
|> Enum.reject(&(BoardWord.get_letter_at(&1, x, y) == :none))
Expand All @@ -93,7 +110,7 @@ defmodule LetterLinesElixir.BoardState do
end
end

defp get_display_letter_at(%BoardState{} = board_state, x, y) do
defp get_display_ascii_letter_at(%BoardState{} = board_state, x, y) do
letter = BoardState.get_letter_at(board_state, x, y)

cond do
Expand Down
12 changes: 12 additions & 0 deletions test/letter_lines_elixir/board_state_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ defmodule LetterLinesElixir.BoardStateTest do
assert %BoardState{words: ^words} = BoardState.new(@board_words)
end

@tag :skip
test "rejects duplicate board words" do
end

test "will raise when multiple letters are found at the same location" do
assert_raise RuntimeError, "Multiple letters found at 5, 0: [\"b\", \"c\"]", fn ->
BoardState.new(@colliding_board_words)
Expand Down Expand Up @@ -150,4 +154,12 @@ defmodule LetterLinesElixir.BoardStateTest do

assert ["b", "r", "u", "n", "c", "h"] = BoardState.get_usable_letter_list(board_state)
end

describe "reveal_word/2" do
test "given a BoardState and one of its words, the word is set to revealed" do
end

test "given a BoardState and a word it does not contain an error is returned" do
end
end
end
4 changes: 0 additions & 4 deletions test/letter_lines_elixir_test.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
defmodule LetterLinesElixirTest do
use ExUnit.Case
doctest LetterLinesElixir

test "greets the world" do
assert LetterLinesElixir.hello() == :world
end
end

0 comments on commit 20b542b

Please sign in to comment.