Skip to content

Instantly share code, notes, and snippets.

@not-much-io
Created October 15, 2016 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save not-much-io/67b229f7f78890362ce42f9498109194 to your computer and use it in GitHub Desktop.
Save not-much-io/67b229f7f78890362ce42f9498109194 to your computer and use it in GitHub Desktop.
@spec new_grid(pos_int, pos_int) :: grid
def new_grid(height, width), do: new_grid(height, width, [])
@spec new_grid(pos_int, pos_int, [cell_state]) :: grid
defp new_grid(h, w, acc)
defp new_grid(0, _w, acc), do: acc
defp new_grid(h, w, acc), do: new_grid(h - 1, w, [new_grid_line(w) | acc])
@spec new_grid_line(pos_int, [cell_state]) :: [cell_state]
defp new_grid_line(width, acc \\ [])
defp new_grid_line(0, acc), do: acc
defp new_grid_line(width, acc) do
rand_state = Enum.random([:alive, :dead])
new_grid_line(width - 1, [rand_state | acc])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment