RPG Dialogue
Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.9", override: true},
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"}
])
Navigation
Home Report An Issue Rock Paper Scissors Lizard SpockPokemon BattleOverview
In this exercise, you will create dialogue for an RPG (Role Playing Game).
To generate this dialogue define a Character
struct with a :name
, :class
, and :weapon
keys. Enforce that the :name
key must have a value.
classDiagram
class Character {
name*: string
class: string
weapon: string
}
Example solution
defmodule Character do
@enforce_keys [:name]
defstruct @enforce_keys ++ [:class, :weapon]
def introduce(character) do
"My name is #{character.name}."
end
def attack(character) do
"I attack with my #{character.weapon}!"
end
def class(character) do
"I am a #{character.class}"
end
def war_cry(character) do
"My name is #{character.name} and I am a #{character.class}!"
end
def defeat(character1, character2) do
"My name is #{character1.name} and I have defeated the #{character2.class} #{character2.name}!"
end
end
Then implement the Character
functions to generate dialogue according to the documented examples below.
defmodule Character do
@moduledoc """
Character
Defines a character struct, and functions for creating character dialogue.
iex> %Character{name: "Frodo"}
%Character{name: "Frodo", weapon: nil, class: nil}
The :name key is required.
iex> %Character{name: "Frodo"}
** (ArgumentError) the following keys must also be given when building struct Character: [:name]
"""
@enforce_keys [:name]
defstruct @enforce_keys ++ [:class, :weapon]
@doc """
Introduce the character by name.
## Examples
iex> Character.introduce(%Character{name: "Gimli"})
"My name is Gimli."
iex> Character.introduce(%Character{name: "Aragorn"})
"My name is Aragorn."
"""
def introduce(character) do
"My name is #{character.name}."
end
@doc """
Declare a character attack.
## Examples
iex> Character.attack(%Character{name: "Gimli", weapon: "axe"})
"I attack with my axe!"
iex> Character.attack(%Character{name: "Aragorn", weapon: "sword"})
"I attack with my sword!"
"""
def attack(character) do
"I attack with my #{character.weapon}!"
end
@doc """
Declare a character's class.
## Examples
iex> Character.class(%Character{name: "Gimli", class: "warrior"})
"I am a warrior."
iex> Character.class(%Character{name: "Aragorn", class: "ranger"})
"I am a ranger."
"""
def class(character) do
"I am a #{character.class}."
end
@doc """
Declare a character's name and class in a war cry.
## Examples
iex> Character.war_cry(%Character{name: "Gimli", class: "warrior"})
"My name is Gimli and I am a warrior!"
iex> Character.war_cry(%Character{name: "Aragorn", class: "ranger"})
"My name is Aragorn and I am a ranger!"
"""
def war_cry(character) do
"My name is #{character.name} and I am a #{character.class}!"
end
@doc """
Declare that one character has defeated another.
## Examples
iex> Character.defeat(%Character{name: "Gimli"}, %Character{name: "Aragorn", class: "ranger"})
"My name is Gimli and I have defeated the ranger Aragorn!"
iex> Character.defeat(%Character{name: "Aragorn"}, %Character{name: "Gimli", class: "warrior"})
"My name is Aragorn and I have defeated the warrior Gimli!"
"""
def defeat(character1, character2) do
"My name is #{character1.name} and I have defeated the #{character2.class} #{character2.name}!"
end
end
Bonus: Character Instances
Bind three variables aragorn
, gandalf
, and gimli
to an instance of a Character
struct with the following information.
classDiagram
class aragorn {
name: "Aragorn"
class: "ranger"
weapon: "sword"
}
class gandalf {
name: "Gandalf"
class: "wizard"
weapon: "staff"
}
class gimli {
name: "Gimli"
class: "warrior"
weapon: "axe"
}
You may use these three structs to test your Character
functions.
Example solution
aragorn = %Character{name: "Aragorn", class: "ranger", weapon: "sword"}
gandalf = %Character{name: "Gandalf", class: "wizard", weapon: "staff"}
gimli = %Character{name: "Gimli", class: "warrior", weapon: "axe"}
# Example Testing
Character.introduce(aragorn)
Enter your solution below.
aragon = %Character{name: "Aragorn", class: "ranger", weapon: "sword"}
gandalf = %Character{name: "Gandalf", class: "wizard", weapon: "staff"}
gimli = %Character{name: "Gimli", class: "warrior", weapon: "axe"}
Commit Your Progress
DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.
Run git status
to ensure there are no undesirable changes.
Then run the following in your command line from the curriculum
folder to commit your progress.
$ git add .
$ git commit -m "finish RPG Dialogue exercise"
$ git push
We’re proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.
We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.