I am creating a 2d map and want to start by pre-filling it with empty values.
I know the following will not work in Elixir, but this is what I am trying to do.
def empty_map(size_x, size_y) do
map = %{}
for x <- 1..size_x do
for y <- 1..size_y do
map = Map.put(map, {x, y}, " ")
end
end
end
Then I will be drawing shapes onto that map like
def create_room(map, {from_x, from_y}, {width, height}) do
for x in from_x..(from_x + width) do
for y in from_x..(from_x + width) do
if # first line, or last line, or first col, or last col
map = Map.replace(map, x, y, '#')
else
map = Map.replace(map, x, y, '.')
end
end
end
end
I have tried doing it as a 2D array, but I think flat map with coordinate touples as keys will be easier to work with.
I know I am supposed to use recursions, but I don't really have a good idea of how to do it elegantly and this scenario keeps coming up and I haven't seen a simple/universal way to do this.