Search⌘ K
AI Features

Dungeon Rooms and Actions

Explore how to create interconnected structs for dungeon rooms and actions in Elixir. Learn to refactor and reuse code for handling player choices, enabling interactive game design and better application structure.

Building structs that use structs

When the hero is in a room, the player can choose an action and face the consequences. We have two new structs to build, and one will reference the other. The Room struct will have many Action structs.

Let’s define the room action module in lib/dungeon_crawl/room. Then we’ll add the following module to the file action.ex:

C++
defmodule DungeonCrawl.Room.Action do
alias DungeonCrawl.Room.Action
defstruct label: nil, id: nil
def forward, do: %Action{id: :forward, label: "Move forward."}
def rest, do: %Action{id: :rest, label: "Take a better look and rest."}
def search, do: %Action{id: :search, label: "Search the room."}
end

The DungeonCrawl.Room.Action struct has id and label attributes. We also created helper functions to build common actions that we’ll need to build the rooms. The next step is to create the room module. Create ...