Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Map generator - Rabbits and Lettuce

examples/rabbits/map_generator.livemd

Map generator - Rabbits and Lettuce

Code

generate_row = fn rabbit_chance, lettuce_chance, width ->
  1..width
  |> Enum.map(fn _index ->
    selected = :rand.uniform()

    cond do
      selected <= rabbit_chance -> "r"
      selected <= rabbit_chance + lettuce_chance -> "l"
      :otherwise -> "-"
    end
  end)
  |> Enum.join()
end

generate_map = fn ->
  [rabbit_chance, lettuce_chance] =
    ["rabbit chance", "lettuce chance"]
    |> Enum.map(&amp;IO.gets/1)
    |> Enum.map(&amp;String.trim/1)
    |> Enum.map(&amp;String.to_float/1)

  [width, height] =
    ["width", "height"]
    |> Enum.map(&amp;IO.gets/1)
    |> Enum.map(&amp;String.trim/1)
    |> Enum.map(&amp;String.to_integer/1)

  map =
    1..height
    |> Enum.map(fn _row -> generate_row.(rabbit_chance, lettuce_chance, width) end)
    |> Enum.join("\n")

  output_directory = IO.gets("output directory") |> String.trim()
  file_name = IO.gets("file name") |> String.trim()

  output_file = "#{output_directory}/#{file_name}"

  File.write(output_file, map)
end

Inputs

Running

generate_map.()