rpg-character-sheet
This commit is contained in:
parent
a4a5085d00
commit
b69dc97356
|
@ -0,0 +1,22 @@
|
||||||
|
{
|
||||||
|
"authors": [
|
||||||
|
"angelikatyborska"
|
||||||
|
],
|
||||||
|
"contributors": [
|
||||||
|
"neenjaw"
|
||||||
|
],
|
||||||
|
"files": {
|
||||||
|
"solution": [
|
||||||
|
"lib/rpg/character_sheet.ex"
|
||||||
|
],
|
||||||
|
"test": [
|
||||||
|
"test/rpg/character_sheet_test.exs"
|
||||||
|
],
|
||||||
|
"exemplar": [
|
||||||
|
".meta/exemplar.ex"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"language_versions": ">=1.10",
|
||||||
|
"icon": "wizards-and-warriors",
|
||||||
|
"blurb": "Learn about IO (input/output) by guiding your friends through the character creation process for your pen-and-paper role-playing game."
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
{"track":"elixir","exercise":"rpg-character-sheet","id":"2040b6af89dd425595b79c2a676bfdeb","url":"https://exercism.org/tracks/elixir/exercises/rpg-character-sheet","handle":"negrienko","is_requester":true,"auto_approve":false}
|
|
@ -0,0 +1,4 @@
|
||||||
|
# Used by "mix format"
|
||||||
|
[
|
||||||
|
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
|
||||||
|
]
|
|
@ -0,0 +1,24 @@
|
||||||
|
# The directory Mix will write compiled artifacts to.
|
||||||
|
/_build/
|
||||||
|
|
||||||
|
# If you run "mix test --cover", coverage assets end up here.
|
||||||
|
/cover/
|
||||||
|
|
||||||
|
# The directory Mix downloads your dependencies sources to.
|
||||||
|
/deps/
|
||||||
|
|
||||||
|
# Where third-party dependencies like ExDoc output generated docs.
|
||||||
|
/doc/
|
||||||
|
|
||||||
|
# Ignore .fetch files in case you like to edit your project deps locally.
|
||||||
|
/.fetch
|
||||||
|
|
||||||
|
# If the VM crashes, it generates a dump, let's ignore it too.
|
||||||
|
erl_crash.dump
|
||||||
|
|
||||||
|
# Also ignore archive artifacts (built via "mix archive.build").
|
||||||
|
*.ez
|
||||||
|
|
||||||
|
# Ignore package tarball (built via "mix hex.build").
|
||||||
|
access-*.tar
|
||||||
|
|
|
@ -0,0 +1,75 @@
|
||||||
|
# Help
|
||||||
|
|
||||||
|
## Running the tests
|
||||||
|
|
||||||
|
From the terminal, change to the base directory of the exercise then execute the tests with:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ mix test
|
||||||
|
```
|
||||||
|
|
||||||
|
This will execute the test file found in the `test` subfolder -- a file ending in `_test.exs`
|
||||||
|
|
||||||
|
Documentation:
|
||||||
|
|
||||||
|
* [`mix test` - Elixir's test execution tool](https://hexdocs.pm/mix/Mix.Tasks.Test.html)
|
||||||
|
* [`ExUnit` - Elixir's unit test library](https://hexdocs.pm/ex_unit/ExUnit.html)
|
||||||
|
|
||||||
|
## Pending tests
|
||||||
|
|
||||||
|
In test suites of practice exercises, all but the first test have been tagged to be skipped.
|
||||||
|
|
||||||
|
Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
# @tag :pending
|
||||||
|
test "shouting" do
|
||||||
|
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
If you wish to run all tests at once, you can include all skipped test by using the `--include` flag on the `mix test` command:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ mix test --include pending
|
||||||
|
```
|
||||||
|
|
||||||
|
Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the file `test/test_helper.exs`.
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
# ExUnit.configure(exclude: :pending, trace: true)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Useful `mix test` options
|
||||||
|
|
||||||
|
* `test/<FILE>.exs:LINENUM` - runs only a single test, the test from `<FILE>.exs` whose definition is on line `LINENUM`
|
||||||
|
* `--failed` - runs only tests that failed the last time they ran
|
||||||
|
* `--max-failures` - the suite stops evaluating tests when this number of test failures
|
||||||
|
is reached
|
||||||
|
* `--seed 0` - disables randomization so the tests in a single file will always be ran
|
||||||
|
in the same order they were defined in
|
||||||
|
|
||||||
|
## Submitting your solution
|
||||||
|
|
||||||
|
You can submit your solution using the `exercism submit lib/rpg/character_sheet.ex` command.
|
||||||
|
This command will upload your solution to the Exercism website and print the solution page's URL.
|
||||||
|
|
||||||
|
It's possible to submit an incomplete solution which allows you to:
|
||||||
|
|
||||||
|
- See how others have completed the exercise
|
||||||
|
- Request help from a mentor
|
||||||
|
|
||||||
|
## Need to get help?
|
||||||
|
|
||||||
|
If you'd like help solving the exercise, check the following pages:
|
||||||
|
|
||||||
|
- The [Elixir track's documentation](https://exercism.org/docs/tracks/elixir)
|
||||||
|
- The [Elixir track's programming category on the forum](https://forum.exercism.org/c/programming/elixir)
|
||||||
|
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
|
||||||
|
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
|
||||||
|
|
||||||
|
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
|
||||||
|
|
||||||
|
If you're stuck on something, it may help to look at some of the [available resources](https://exercism.org/docs/tracks/elixir/resources) out there where answers might be found.
|
|
@ -0,0 +1,43 @@
|
||||||
|
# Hints
|
||||||
|
|
||||||
|
## General
|
||||||
|
|
||||||
|
- Read about [IO][getting-started-io] in the Getting Started guide.
|
||||||
|
- Read about [debugging with `IO.inspect/2`][getting-started-debugging-io-inspect] in the Getting Started guide.
|
||||||
|
- Read about the [`IO` module][module-io] in the documentation.
|
||||||
|
|
||||||
|
## 1. Welcome the new player
|
||||||
|
|
||||||
|
- There is a [built-in function][io-puts] that writes a string to the standard output.
|
||||||
|
|
||||||
|
## 2. Ask for the character's name
|
||||||
|
|
||||||
|
- There is a [built-in function][io-gets] that reads a string from the standard output. It can also write a prompt, so there is no need to use a separate function to write the question.
|
||||||
|
- There is a [build-in function][string-trim] in the `String` module that can remove trailing and leading whitespace from the input.
|
||||||
|
|
||||||
|
## 3. Ask for the character's class
|
||||||
|
|
||||||
|
- There is a [built-in function][io-gets] that reads a string from the standard output. It can also write a prompt, so there is no need to use a separate function to write the question.
|
||||||
|
- There is a [build-in function][string-trim] in the `String` module that can remove trailing and leading whitespace from the input.
|
||||||
|
|
||||||
|
## 4. Ask for the character's level
|
||||||
|
|
||||||
|
- There is a [built-in function][io-gets] that reads a string from the standard output. It can also write a prompt, so there is no need to use a separate function to write the question.
|
||||||
|
- There is a [build-in function][string-trim] in the `String` module that can remove trailing and leading whitespace from the input.
|
||||||
|
- There is a [build-in function][string-to-integer] in the `String` module that can convert a string to an integer.
|
||||||
|
|
||||||
|
## 5. Combine previous steps into one
|
||||||
|
|
||||||
|
- Reuse functions implemented in previous steps.
|
||||||
|
- There is a [built-in function][io-inspect], useful for debugging, that can write to the standard output more than just strings. That functions accepts options, for example a string that will be written before the passed value.
|
||||||
|
- The function and its option mentioned above will append a colon and a space to that string for you.
|
||||||
|
|
||||||
|
[module-io]: https://hexdocs.pm/elixir/IO.html
|
||||||
|
[getting-started-io]: https://elixir-lang.org/getting-started/io-and-the-file-system.html#the-io-module
|
||||||
|
[getting-started-debugging-io-inspect]: https://elixir-lang.org/getting-started/debugging.html#ioinspect2
|
||||||
|
[elixir-school-io-inspect-label]: https://elixirschool.com/blog/til-io-inspect-labels/
|
||||||
|
[io-puts]: https://hexdocs.pm/elixir/IO.html#puts/2
|
||||||
|
[io-gets]: https://hexdocs.pm/elixir/IO.html#gets/2
|
||||||
|
[io-inspect]: https://hexdocs.pm/elixir/IO.html#inspect/2
|
||||||
|
[string-trim]: https://hexdocs.pm/elixir/String.html#trim/1
|
||||||
|
[string-to-integer]: https://hexdocs.pm/elixir/String.html#to_integer/1
|
|
@ -0,0 +1,108 @@
|
||||||
|
# RPG Character Sheet
|
||||||
|
|
||||||
|
Welcome to RPG Character Sheet on Exercism's Elixir Track.
|
||||||
|
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||||
|
If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
## IO
|
||||||
|
|
||||||
|
Functions for handling input and output are provided by the `IO` module.
|
||||||
|
|
||||||
|
### Output
|
||||||
|
|
||||||
|
To write a string to the standard output, use `IO.puts`. `IO.puts` always adds a new line at the end of the string. If you don't want that behavior, use `IO.write` instead. Both functions return the atom `:ok` if they succeed.
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
IO.puts("Hi!")
|
||||||
|
# > Hi!
|
||||||
|
# => :ok
|
||||||
|
```
|
||||||
|
|
||||||
|
`IO.puts` is useful for writing strings, but not much else. If you need a tool for debugging that will allow you to write any value to standard output, use `IO.inspect` instead. `IO.inspect` returns the value it was passed unchanged, so it can be inserted in any point in your code. It also accepts many options, for example `:label`, that will allow you to distinguish it from other `IO.inspect` calls.
|
||||||
|
|
||||||
|
### Input
|
||||||
|
|
||||||
|
To read a line from the standard input, use `IO.gets`. `IO.gets` accepts one argument - a string that it will print as a prompt for the input. `IO.gets` doesn't add a new line after the prompt, include it yourself if you need it.
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
IO.gets("What's your name?\n")
|
||||||
|
# > What's your name?
|
||||||
|
# < Mary
|
||||||
|
# => "Mary\n"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
|
||||||
|
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`.
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
RPG.CharacterSheet.welcome()
|
||||||
|
# > Welcome! Let's fill out your character sheet together.
|
||||||
|
# => :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.
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
RPG.CharacterSheet.ask_name()
|
||||||
|
# > What is your character's name?
|
||||||
|
# < Mathilde
|
||||||
|
# => "Mathilde"
|
||||||
|
```
|
||||||
|
|
||||||
|
## 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.
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
RPG.CharacterSheet.ask_class()
|
||||||
|
# > What is your character's class?
|
||||||
|
# < healer
|
||||||
|
# => "healer"
|
||||||
|
```
|
||||||
|
|
||||||
|
## 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.
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
RPG.CharacterSheet.ask_level()
|
||||||
|
# > What is your character's level?
|
||||||
|
# < 2
|
||||||
|
# => 2
|
||||||
|
```
|
||||||
|
|
||||||
|
## 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".
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
RPG.CharacterSheet.run()
|
||||||
|
# > Welcome! Let's fill out your character sheet together.
|
||||||
|
# > What is your character's name?
|
||||||
|
# < Mathilde
|
||||||
|
# > What is your character's class?
|
||||||
|
# < healer
|
||||||
|
# > What is your character's level?
|
||||||
|
# < 2
|
||||||
|
# > Your character: %{class: "healer", level: 2, name: "Mathilde"}
|
||||||
|
# => %{class: "healer", level: 2, name: "Mathilde"}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Source
|
||||||
|
|
||||||
|
### Created by
|
||||||
|
|
||||||
|
- @angelikatyborska
|
||||||
|
|
||||||
|
### Contributed to by
|
||||||
|
|
||||||
|
- @neenjaw
|
|
@ -0,0 +1,31 @@
|
||||||
|
defmodule RPG.CharacterSheet do
|
||||||
|
def welcome() do
|
||||||
|
IO.puts("Welcome! Let's fill out your character sheet together.")
|
||||||
|
end
|
||||||
|
|
||||||
|
def ask_name() do
|
||||||
|
IO.gets("What is your character's name?\n")
|
||||||
|
|> String.trim()
|
||||||
|
end
|
||||||
|
|
||||||
|
def ask_class() do
|
||||||
|
IO.gets("What is your character's class?\n")
|
||||||
|
|> String.trim()
|
||||||
|
end
|
||||||
|
|
||||||
|
def ask_level() do
|
||||||
|
IO.gets("What is your character's level?\n")
|
||||||
|
|> String.trim()
|
||||||
|
|> String.to_integer()
|
||||||
|
end
|
||||||
|
|
||||||
|
def run() do
|
||||||
|
welcome()
|
||||||
|
|
||||||
|
%{}
|
||||||
|
|> Map.put(:name, ask_name())
|
||||||
|
|> Map.put(:class, ask_class())
|
||||||
|
|> Map.put(:level, ask_level())
|
||||||
|
|> IO.inspect(label: "Your character")
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,28 @@
|
||||||
|
defmodule RPG.MixProject do
|
||||||
|
use Mix.Project
|
||||||
|
|
||||||
|
def project do
|
||||||
|
[
|
||||||
|
app: :rpg_character_sheet,
|
||||||
|
version: "0.1.0",
|
||||||
|
# elixir: "~> 1.10",
|
||||||
|
start_permanent: Mix.env() == :prod,
|
||||||
|
deps: deps()
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
# Run "mix help compile.app" to learn about applications.
|
||||||
|
def application do
|
||||||
|
[
|
||||||
|
extra_applications: [:logger]
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
# Run "mix help deps" to learn about dependencies.
|
||||||
|
defp deps do
|
||||||
|
[
|
||||||
|
# {:dep_from_hexpm, "~> 0.3.0"},
|
||||||
|
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
|
||||||
|
]
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,133 @@
|
||||||
|
defmodule RPG.CharacterSheetTest do
|
||||||
|
use ExUnit.Case
|
||||||
|
import ExUnit.CaptureIO
|
||||||
|
|
||||||
|
describe "welcome/0" do
|
||||||
|
@tag task_id: 1
|
||||||
|
test "it prints a welcome message" do
|
||||||
|
io =
|
||||||
|
capture_io(fn ->
|
||||||
|
assert RPG.CharacterSheet.welcome() == :ok
|
||||||
|
end)
|
||||||
|
|
||||||
|
assert io == "Welcome! Let's fill out your character sheet together.\n"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "ask_name/0" do
|
||||||
|
@tag task_id: 2
|
||||||
|
test "it prints a prompt" do
|
||||||
|
io =
|
||||||
|
capture_io("\n", fn ->
|
||||||
|
RPG.CharacterSheet.ask_name()
|
||||||
|
end)
|
||||||
|
|
||||||
|
assert io == "What is your character's name?\n"
|
||||||
|
end
|
||||||
|
|
||||||
|
@tag task_id: 2
|
||||||
|
test "returns the trimmed input" do
|
||||||
|
capture_io("Maxwell The Great\n", fn ->
|
||||||
|
assert RPG.CharacterSheet.ask_name() == "Maxwell The Great"
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "ask_class/0" do
|
||||||
|
@tag task_id: 3
|
||||||
|
test "it prints a prompt" do
|
||||||
|
io =
|
||||||
|
capture_io("\n", fn ->
|
||||||
|
RPG.CharacterSheet.ask_class()
|
||||||
|
end)
|
||||||
|
|
||||||
|
assert io == "What is your character's class?\n"
|
||||||
|
end
|
||||||
|
|
||||||
|
@tag task_id: 3
|
||||||
|
test "returns the trimmed input" do
|
||||||
|
capture_io("rogue\n", fn ->
|
||||||
|
assert RPG.CharacterSheet.ask_class() == "rogue"
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "ask_level/0" do
|
||||||
|
@tag task_id: 4
|
||||||
|
test "it prints a prompt" do
|
||||||
|
io =
|
||||||
|
capture_io("1\n", fn ->
|
||||||
|
RPG.CharacterSheet.ask_level()
|
||||||
|
end)
|
||||||
|
|
||||||
|
assert io == "What is your character's level?\n"
|
||||||
|
end
|
||||||
|
|
||||||
|
@tag task_id: 4
|
||||||
|
test "returns the trimmed input as an integer" do
|
||||||
|
capture_io("3\n", fn ->
|
||||||
|
assert RPG.CharacterSheet.ask_level() == 3
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "run/0" do
|
||||||
|
@tag task_id: 5
|
||||||
|
test "it asks for name, class, and level" do
|
||||||
|
io =
|
||||||
|
capture_io("Susan The Fearless\nfighter\n6\n", fn ->
|
||||||
|
RPG.CharacterSheet.run()
|
||||||
|
end)
|
||||||
|
|
||||||
|
assert io =~ """
|
||||||
|
Welcome! Let's fill out your character sheet together.
|
||||||
|
What is your character's name?
|
||||||
|
What is your character's class?
|
||||||
|
What is your character's level?
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
|
||||||
|
@tag task_id: 5
|
||||||
|
test "it returns a character map" do
|
||||||
|
capture_io("The Stranger\nrogue\n2\n", fn ->
|
||||||
|
assert RPG.CharacterSheet.run() == %{
|
||||||
|
name: "The Stranger",
|
||||||
|
class: "rogue",
|
||||||
|
level: 2
|
||||||
|
}
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
@tag task_id: 5
|
||||||
|
test "it only has a single colon and single space after the 'Your character' label" do
|
||||||
|
io =
|
||||||
|
capture_io("Anne\nhealer\n4\n", fn ->
|
||||||
|
RPG.CharacterSheet.run()
|
||||||
|
end)
|
||||||
|
|
||||||
|
case Regex.run(~r/.*(Your character.*)%{/, io) do
|
||||||
|
[_, label] ->
|
||||||
|
assert label == "Your character: "
|
||||||
|
|
||||||
|
_ ->
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@tag task_id: 5
|
||||||
|
test "it inspects the character map" do
|
||||||
|
io =
|
||||||
|
capture_io("Anne\nhealer\n4\n", fn ->
|
||||||
|
RPG.CharacterSheet.run()
|
||||||
|
end)
|
||||||
|
|
||||||
|
assert io =~
|
||||||
|
"\nYour character: " <>
|
||||||
|
inspect(%{
|
||||||
|
name: "Anne",
|
||||||
|
class: "healer",
|
||||||
|
level: 4
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,2 @@
|
||||||
|
ExUnit.start()
|
||||||
|
ExUnit.configure(exclude: :pending, trace: true, seed: 0)
|
Loading…
Reference in New Issue