Hi all,
I am trying to come up with a Room struct which is able to have paths in between them. This is what i have so far:
defmodule Room do
defstruct name: "", description: "", paths: %{}
def go(room, direction) do
Map.get(room.paths, direction, nil)
end
def add_paths(room, paths) do
new_paths = Map.merge(room.paths, paths)
%{room | paths: new_paths}
end
end
Test
defmodule RoomTest do
use ExUnit.Case
test "map" do
start = %Room{name: "Start", description: "You can go west and down a hole."}
west = %Room{name: "Trees", description: "There are trees here, you can go east."}
down = %Room{name: "Dungeon", description: "It's dark down here, you can go up"}
start = Room.add_paths(start, %{'west' => west, 'down' => down})
west = Room.add_paths(west, %{'east' => start})
down = Room.add_paths(down, %{'up' => start})
assert Room.go(start, 'west') == west
assert Room.go(start, 'west').go('east') == start
assert Room.go(start, 'down').go('up') == start
end
end
Test fails because the method call start = Room.add_paths(start, %{'west' => west, 'down' => down})
would have recorded an āold versionā of west and down rooms by the end of the test execution.
Any suggestion how this kinda work is done in Elixir?