Character Generator
Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.9", override: true},
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"}
])
Navigation
Setup
Ensure you type the ea keyboard shortcut to evaluate all Elixir cells before starting. Alternatively you can evaluate the Elixir cells as you read.
Character Generator
You are building a text RPG game where players can create a character. In the Elixir cell below,
-
Create a
Characterstruct -
Characters should havehealth,class,description,name,level, andattackfields. -
healthshould be a float. -
classshould be an atom of either:wizard,:warrior, or:ranger. -
nameshould be a string. -
descriptionshould be a string. -
levelshould be an integer defaulted to1 -
attackshould be an integer defaulted to5
defmodule Character do
end
Character Validation
-
Create a
Character.new/1function which validates the parameters given to character. -
levelshould not be used to create a character, all characters start at1 -
attackshould not be used to create a character, all characters start at5 -
healthshould not be used to create a character, wizards start with10health, rangers15, and warriors20. -
enforce that
classone of the three correct atoms -
enforce that
nameis a string -
enforce that
descriptionis a string
Character Randomizer
-
Create a
Character.randomize/1function which accepts an existingCharacterstruct and randomizes theclass,name, anddescription. -
a random
classshould be one of:wizard,:warrior, and:ranger -
a random
nameshould be a random7to14letter string of random alphabetical characters. -
a random
descriptionshould be a random100to200letter string of random alphabetical characters. -
attackshould be updated to match the randomclass. -
levelandattackshould be unchanged.
Character Form
Kino Inputs
In order to complete this exercise, you’re going to use Kino. Kino is a library specifically for Livebook - the program you’re using to run this course.
text_input = Kino.Input.text("the label of your input")
Then you can access the value inside of the text input with Kino.Input.read/1
text_input_value = Kino.Input.read(text_input)
You can also create select inputs with Kino.Input.select/3
Kino.Input.select("Language", [{"key1", :value1}, {"key2", :value2}])
Create A Character Form
In the Elixir cell below, Create a name input using Kino.Input.text/2
Create a description input using Kino.Input.text/2
Create a class input using Kino.Input.select/3. It should have :wizard, :warrior, and :ranger as options.
In the Elixir cell below, use the inputs you defined above and Kino.Input.read/1 to create
an instance of a Character struct i.e. %Character{params}.
Mark As Completed
file_name = Path.basename(Regex.replace(~r/#.+/, __ENV__.file, ""), ".livemd")
progress_path = __DIR__ <> "/../progress.json"
existing_progress = File.read!(progress_path) |> Jason.decode!()
default = Map.get(existing_progress, file_name, false)
form =
Kino.Control.form(
[
completed: input = Kino.Input.checkbox("Mark As Completed", default: default)
],
report_changes: true
)
Task.async(fn ->
for %{data: %{completed: completed}} <- Kino.Control.stream(form) do
File.write!(progress_path, Jason.encode!(Map.put(existing_progress, file_name, completed)))
end
end)
form
Commit Your Progress
Run the following in your command line from the curriculum folder to track and save your progress in a Git commit.
Ensure that you do not already have undesired or unrelated changes by running git status or by checking the source control tab in Visual Studio Code.
$ git checkout solutions
$ git checkout -b character-generator-exercise
$ git add .
$ git commit -m "finish character generator exercise"
$ git push origin character-generator-exercise
Create a pull request from your character-generator-exercise branch to your solutions branch.
Please do not create a pull request to the DockYard Academy repository as this will spam our PR tracker.
DockYard Academy Students Only:
Notify your instructor by including @BrooklinJazz in your PR description to get feedback.
You (or your instructor) may merge your PR into your solutions branch after review.
If you are interested in joining the next academy cohort, sign up here to receive more news when it is available.