iT邦幫忙

2022 iThome 鐵人賽

DAY 14
0

You and your friends love to play pen-and-paper role-playing games, but you noticed that it's difficult to get new people to join your group. They often struggle with character creation.

They don't know where to start. To help new players out, you decided to write a small program that will guide them through the process.

1. Welcome the new player

Implement the RPG.CharacterSheet.welcome/0 function. It should print a welcome message, and return :ok.

2. Ask for the character's name

Implement the RPG.CharacterSheet.ask_name/0 function. It should print a question, wait for an answer, and return the answer without leading and trailing whitespace.

3. Ask for the character's class

Implement the RPG.CharacterSheet.ask_class/0 function. It should print a question, wait for an answer, and return the answer without leading and trailing whitespace.

4. Ask for the character's level

Implement the RPG.CharacterSheet.ask_level/0 function. It should print a question, wait for an answer, and return the answer as an integer.

5. Combine previous steps into one

Implement the RPG.CharacterSheet.run/0 function. It should welcome the new player, ask for the character's name, class, and level, and return the character sheet as a map. It should also print the map with the label "Your character".


https://exercism.org/tracks/elixir/exercises/rpg-character-sheet

defmodule RPG.CharacterSheet do
  @moduledoc """
    IO related
  """

  @doc """
  practice with IO module
  """
  @spec welcome() :: :ok
  def welcome(), do: IO.puts("Welcome! Let's fill out your character sheet together.")

  @doc """
  IO.gets practice
  """
  @spec ask_name() :: IO.chardata()
  def ask_name(), do: IO.gets("What is your character's name?\n") |> String.trim()

  @spec ask_class() :: IO.chardata()
  def ask_class(), do: IO.gets("What is your character's class?\n") |> String.trim()

  def ask_level(),
    do: IO.gets("What is your character's level?\n") |> String.trim() |> String.to_integer()

  @spec run() :: IO.chardata()
  def run() do
    welcome()
    name = ask_name()
    class = ask_class()
    level = ask_level()

    IO.inspect(%{class: class, level: level, name: name}, label: "Your character")
  end
end

上一篇
十三. Case & Charlist
下一篇
十五. If Statement
系列文
戀戀 Elixir30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言