Created
April 17, 2017 18:54
-
-
Save markmeeus/0a4bde0dac56f704a6ab7bac35fd77d4 to your computer and use it in GitHub Desktop.
Super naive maze generator
This file contains hidden or 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
defmodule Maze do | |
def get_maze maze_size do | |
maze = | |
for y <- (maze_size..1), | |
do: build_row(y, maze_size) | |
end | |
defp build_row(y, maze_size), do: Enum.into(%{}, build_row_tuples(y, maze_size)) | |
defp build_row_tuples(y, maze_size)do | |
for x <- (1..maze_size), do: {x, get_direction(x, y, maze_size)} #tuple: {x, direction} | |
end | |
defp get_direction(maze_size, maze_size, maze_size), do: :end #end of the maze | |
defp get_direction(_x, maze_size, maze_size), do: :right #Top row, always to the right | |
defp get_direction(maze_size, _y, maze_size), do: :up #rightmost column, always up | |
defp get_direction(_x, _y, maze_size), do: Enum.random([:up, :right]) | |
end | |
defmodule MazePrinter do | |
#printing stuff | |
def print_maze maze do | |
Enum.reduce(maze, "", &print_maze_row/2) |> IO.puts | |
end | |
def print_maze_row row, acc do | |
Enum.reduce(row, acc, fn({x, direction}, acc)-> print_cell(acc, direction) end) | |
<> "\n" | |
end | |
defp print_cell(acc, :up), do: acc <> " |" | |
defp print_cell(acc, :right), do: acc <> "‾‾" | |
defp print_cell(acc, :end), do: acc <> " O" | |
end | |
Maze.get_maze(40) | |
|> MazePrinter.print_maze |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment