Character Generator
Mix.install([
{:kino, github: "livebook-dev/kino", override: true},
{:kino_lab, "~> 0.1.0-dev", github: "jonatanklosko/kino_lab"},
{:vega_lite, "~> 0.1.4"},
{:kino_vega_lite, "~> 0.1.1"},
{:benchee, "~> 0.1"},
{:ecto, "~> 3.7"},
{:math, "~> 0.7.0"},
{:faker, "~> 0.17.0"},
{:utils, path: "#{__DIR__}/../utils"},
{:tested_cell, git: "https://github.com/BrooklinJazz/tested_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
Character
struct -
Character
s should havehealth
,class
,description
,name
,level
, andattack
fields. -
health
should be a float. -
class
should be an atom of either:wizard
,:warrior
, or:ranger
. -
name
should be a string. -
description
should be a string. -
level
should be an integer defaulted to1
-
attack
should be an integer defaulted to5
defmodule Character do
end
Character Validation
-
Create a
Character.new/1
function which validates the parameters given to character. -
level
should not be used to create a character, all characters start at1
-
attack
should not be used to create a character, all characters start at5
-
health
should not be used to create a character, wizards start with10
health, rangers15
, and warriors20
. -
enforce that
class
one of the three correct atoms -
enforce that
name
is a string -
enforce that
description
is a string
Character Randomizer
-
Create a
Character.randomize/1
function which accepts an existingCharacter
struct and randomizes theclass
,name
, anddescription
. -
a random
class
should be one of:wizard
,:warrior
, and:ranger
-
a random
name
should be a random7
to14
letter string of random alphabetical characters. -
a random
description
should be a random100
to200
letter string of random alphabetical characters. -
attack
should be updated to match the randomclass
. -
level
andattack
should 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
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/1
Create a description
input using Kino.Input.text/1
Create a class
input using Kino.Input.select/1
. 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}
.
Commit Your Progress
Run the following in your command line from the project folder to track and save your progress in a Git commit.
$ git add .
$ git commit -m "finish character generator exercise"