Search⌘ K

Listing the Heroes

Explore how to build and display a list of heroes in an Elixir game application. Learn to separate CLI interactions using modules, manage terminal input and output with Mix.Shell.IO, and maintain clear boundaries for scalable code. This lesson helps you understand struct usage, aliasing, and user interface design in Elixir.

We'll cover the following...

Displaying

The next step is to make the game display a list of heroes to the player. The actions of displaying an interface and displaying the heroes list are two different contexts. We’ll learn how to separate them. First, let’s create the heroes list by creating the heroes.ex file in the lib/dungeon_crawl folder. Our directory structure should look like this:

Let’s build the module in heroes.ex:

C++
defmodule DungeonCrawl.Heroes do
alias DungeonCrawl.Character
def all, do: [
%Character{
name: "Knight",
description: "Knight has strong defense and consistent damage.",
hit_points: 18,
max_hit_points: 18,
damage_range: 4..5,
attack_description: "a sword"
},
%Character{
name: "Wizard",
description: "Wizard has strong attack, but low health.",
hit_points: 8,
max_hit_points: 8,
damage_range: 6..10,
attack_description: "a fireball"
},
%Character{
name: "Rogue",
description: "Rogue has high variability of attack damage.", hit_points: 12,
max_hit_points: 12,
damage_range: 1..12,
attack_description: "a dagger"
},
]
end

We’ve made a list of heroes using the DungeonCrawl.Character struct. The first line of our module uses the alias directive. This alias creates a shortcut, ...