Choosing a Hero
Build the functionality for the player to choose a hero.
We'll cover the following...
After the game lists the heroes, the player must type a number to choose one. Let’s build that functionality. First, we need to generate a question with the numbers that the player can choose, get the player’s input, parse it, and select the corresponding hero. Let’s improve the lib/dungeon_crawl/cli/hero_choice.ex
with the following code:
def start doShell.cmd("clear")Shell.info("Start by choosing your hero:")heroes = DungeonCrawl.Heroes.all()find_hero_by_index = &Enum.at(heroes, &1)heroes|> Enum.map(&(&1.name))|> display_options|> generate_question|> Shell.prompt|> parse_answer|> find_hero_by_index.()|> confirm_heroend
The pipeline of functions says to take the heroes’ names, display them, generate a question, ask the user for input, parse the user’s answer, find the corresponding hero, and confirm the player choice. The pipeline starts with a list of heroes and ends with the chosen hero. So, after listing the heroes, we must implement the generate_question/1
function: ...