Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,30 @@
defmodule Maze do
def generate(w, h) do
:random.seed(:os.timestamp)
(for i <- 1..w, j <- 1..h, do: {i,j}) |>
Enum.each(fn{i,j} -> Process.put({:vis, i, j}, true) end)
walk(:random.uniform(w), :random.uniform(h))
print(w, h)
end
defp walk(x, y) do
Process.put({:vis, x, y}, false)
Enum.each(Enum.shuffle([[x-1,y], [x,y+1], [x+1,y], [x,y-1]]), fn [i,j] ->
if Process.get({:vis, i, j}) do
if i == x, do: Process.put({:hor, x, max(y, j)}, "+ "),
else: Process.put({:ver, max(x, i), y}, " ")
walk(i, j)
end
end)
end
defp print(w, h) do
Enum.each(1..h, fn j ->
IO.puts (Enum.map(1..w, fn i -> Process.get({:hor, i, j}, "+---") end) |> Enum.join) <> "+"
IO.puts (Enum.map(1..w, fn i -> Process.get({:ver, i, j}, "| ") end) |> Enum.join) <> "|"
end)
IO.puts String.duplicate("+---", w) <> "+"
end
end
Maze.generate(20, 10)