Choosing a Hero
Explore how to implement hero selection in your Elixir game application by generating choices, parsing user input, and confirming selections. Understand how to use functional programming constructs and pipelines to interact with users and manage state within your application.
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:
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: ...