From c2e651cd5a645eab7b8f9aa093a8f2d0b417676c Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Mon, 1 Jul 2024 21:27:01 -0400 Subject: [PATCH] dnd-character --- elixir/dnd-character/.exercism/config.json | 25 +++++ elixir/dnd-character/.exercism/metadata.json | 1 + elixir/dnd-character/.formatter.exs | 4 + elixir/dnd-character/.gitignore | 24 +++++ elixir/dnd-character/HELP.md | 75 +++++++++++++++ elixir/dnd-character/README.md | 65 +++++++++++++ elixir/dnd-character/lib/dnd_character.ex | 34 +++++++ elixir/dnd-character/mix.exs | 28 ++++++ .../dnd-character/test/dnd_character_test.exs | 93 +++++++++++++++++++ elixir/dnd-character/test/test_helper.exs | 2 + 10 files changed, 351 insertions(+) create mode 100644 elixir/dnd-character/.exercism/config.json create mode 100644 elixir/dnd-character/.exercism/metadata.json create mode 100644 elixir/dnd-character/.formatter.exs create mode 100644 elixir/dnd-character/.gitignore create mode 100644 elixir/dnd-character/HELP.md create mode 100644 elixir/dnd-character/README.md create mode 100644 elixir/dnd-character/lib/dnd_character.ex create mode 100644 elixir/dnd-character/mix.exs create mode 100644 elixir/dnd-character/test/dnd_character_test.exs create mode 100644 elixir/dnd-character/test/test_helper.exs diff --git a/elixir/dnd-character/.exercism/config.json b/elixir/dnd-character/.exercism/config.json new file mode 100644 index 0000000..3813c43 --- /dev/null +++ b/elixir/dnd-character/.exercism/config.json @@ -0,0 +1,25 @@ +{ + "authors": [ + "Br1ght0ne" + ], + "contributors": [ + "angelikatyborska", + "Cohen-Carlisle", + "devonestes", + "neenjaw" + ], + "files": { + "solution": [ + "lib/dnd_character.ex" + ], + "test": [ + "test/dnd_character_test.exs" + ], + "example": [ + ".meta/example.ex" + ] + }, + "blurb": "Randomly generate Dungeons & Dragons characters.", + "source": "Simon Shine, Erik Schierboom", + "source_url": "https://github.com/exercism/problem-specifications/issues/616#issuecomment-437358945" +} diff --git a/elixir/dnd-character/.exercism/metadata.json b/elixir/dnd-character/.exercism/metadata.json new file mode 100644 index 0000000..efdcd49 --- /dev/null +++ b/elixir/dnd-character/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"dnd-character","id":"68ec265ac1f144819dfb831fa55924b7","url":"https://exercism.org/tracks/elixir/exercises/dnd-character","handle":"negrienko","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/elixir/dnd-character/.formatter.exs b/elixir/dnd-character/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/dnd-character/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/dnd-character/.gitignore b/elixir/dnd-character/.gitignore new file mode 100644 index 0000000..8aa1a42 --- /dev/null +++ b/elixir/dnd-character/.gitignore @@ -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"). +dnd_character-*.tar + diff --git a/elixir/dnd-character/HELP.md b/elixir/dnd-character/HELP.md new file mode 100644 index 0000000..42f5880 --- /dev/null +++ b/elixir/dnd-character/HELP.md @@ -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/.exs:LINENUM` - runs only a single test, the test from `.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/dnd_character.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. \ No newline at end of file diff --git a/elixir/dnd-character/README.md b/elixir/dnd-character/README.md new file mode 100644 index 0000000..a76ab7e --- /dev/null +++ b/elixir/dnd-character/README.md @@ -0,0 +1,65 @@ +# D&D Character + +Welcome to D&D Character on Exercism's Elixir Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Introduction + +After weeks of anticipation, you and your friends get together for your very first game of [Dungeons & Dragons][dnd] (D&D). +Since this is the first session of the game, each player has to generate a character to play with. +The character's abilities are determined by rolling 6-sided dice, but where _are_ the dice? +With a shock, you realize that your friends are waiting for _you_ to produce the dice; after all it was your idea to play D&D! +Panicking, you realize you forgot to bring the dice, which would mean no D&D game. +As you have some basic coding skills, you quickly come up with a solution: you'll write a program to simulate dice rolls. + +[dnd]: https://en.wikipedia.org/wiki/Dungeons_%26_Dragons + +## Instructions + +For a game of [Dungeons & Dragons][dnd], each player starts by generating a character they can play with. +This character has, among other things, six abilities; strength, dexterity, constitution, intelligence, wisdom and charisma. +These six abilities have scores that are determined randomly. +You do this by rolling four 6-sided dice and recording the sum of the largest three dice. +You do this six times, once for each ability. + +Your character's initial hitpoints are 10 + your character's constitution modifier. +You find your character's constitution modifier by subtracting 10 from your character's constitution, divide by 2 and round down. + +Write a random character generator that follows the above rules. + +For example, the six throws of four dice may look like: + +- 5, 3, 1, 6: You discard the 1 and sum 5 + 3 + 6 = 14, which you assign to strength. +- 3, 2, 5, 3: You discard the 2 and sum 3 + 5 + 3 = 11, which you assign to dexterity. +- 1, 1, 1, 1: You discard the 1 and sum 1 + 1 + 1 = 3, which you assign to constitution. +- 2, 1, 6, 6: You discard the 1 and sum 2 + 6 + 6 = 14, which you assign to intelligence. +- 3, 5, 3, 4: You discard the 3 and sum 5 + 3 + 4 = 12, which you assign to wisdom. +- 6, 6, 6, 6: You discard the 6 and sum 6 + 6 + 6 = 18, which you assign to charisma. + +Because constitution is 3, the constitution modifier is -4 and the hitpoints are 6. + +~~~~exercism/note +Most programming languages feature (pseudo-)random generators, but few programming languages are designed to roll dice. +One such language is [Troll][troll]. + +[troll]: https://di.ku.dk/Ansatte/?pure=da%2Fpublications%2Ftroll-a-language-for-specifying-dicerolls(84a45ff0-068b-11df-825d-000ea68e967b)%2Fexport.html +~~~~ + +[dnd]: https://en.wikipedia.org/wiki/Dungeons_%26_Dragons + +## Source + +### Created by + +- @Br1ght0ne + +### Contributed to by + +- @angelikatyborska +- @Cohen-Carlisle +- @devonestes +- @neenjaw + +### Based on + +Simon Shine, Erik Schierboom - https://github.com/exercism/problem-specifications/issues/616#issuecomment-437358945 \ No newline at end of file diff --git a/elixir/dnd-character/lib/dnd_character.ex b/elixir/dnd-character/lib/dnd_character.ex new file mode 100644 index 0000000..2b8de61 --- /dev/null +++ b/elixir/dnd-character/lib/dnd_character.ex @@ -0,0 +1,34 @@ +defmodule DndCharacter do + alias ElixirLS.LanguageServer.Providers.Completion.Reducers.Struct + @type t :: %__MODULE__{ + strength: pos_integer(), + dexterity: pos_integer(), + constitution: pos_integer(), + intelligence: pos_integer(), + wisdom: pos_integer(), + charisma: pos_integer(), + hitpoints: pos_integer() + } + + @abilities ~w(strength dexterity constitution intelligence wisdom charisma hitpoints)a + + defstruct @abilities + + @spec modifier(pos_integer()) :: integer() + def modifier(score), do: floor((score - 10) / 2) + + @spec ability :: pos_integer() + def ability do + 0..2 + |> Enum.map(fn _x -> Enum.random(1..6) end) + |> Enum.sort(:desc) + |> Enum.sum() + end + + @spec character :: t() + def character do + for ability <- @abilities, reduce: %{} do acc -> Map.put(acc, ability, ability()) end + |> then(&Map.put(&1, :hitpoints, 10 + modifier(Map.get(&1, :constitution)))) + |> then(&struct(__MODULE__, &1)) + end +end diff --git a/elixir/dnd-character/mix.exs b/elixir/dnd-character/mix.exs new file mode 100644 index 0000000..9955a45 --- /dev/null +++ b/elixir/dnd-character/mix.exs @@ -0,0 +1,28 @@ +defmodule DndCharacter.MixProject do + use Mix.Project + + def project do + [ + app: :dnd_character, + version: "0.1.0", + # elixir: "~> 1.8", + 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 diff --git a/elixir/dnd-character/test/dnd_character_test.exs b/elixir/dnd-character/test/dnd_character_test.exs new file mode 100644 index 0000000..09cd3f5 --- /dev/null +++ b/elixir/dnd-character/test/dnd_character_test.exs @@ -0,0 +1,93 @@ +defmodule DndCharacterTest do + use ExUnit.Case + + import DndCharacter, only: [modifier: 1, ability: 0, character: 0] + + describe "ability modifier" do + test "for score 3 is -4" do + assert modifier(3) === -4 + end + + test "for score 4 is -3" do + assert modifier(4) === -3 + end + + test "for score 5 is -3" do + assert modifier(5) === -3 + end + + test "for score 6 is -2" do + assert modifier(6) === -2 + end + + test "for score 7 is -2" do + assert modifier(7) === -2 + end + + test "for score 8 is -1" do + assert modifier(8) === -1 + end + + test "for score 9 is -1" do + assert modifier(9) === -1 + end + + test "for score 10 is 0" do + assert modifier(10) === 0 + end + + test "for score 11 is 0" do + assert modifier(11) === 0 + end + + test "for score 12 is +1" do + assert modifier(12) === 1 + end + + test "for score 13 is +1" do + assert modifier(13) === 1 + end + + test "for score 14 is +2" do + assert modifier(14) === 2 + end + + test "for score 15 is +2" do + assert modifier(15) === 2 + end + + test "for score 16 is +3" do + assert modifier(16) === 3 + end + + test "for score 17 is +3" do + assert modifier(17) === 3 + end + + test "for score 18 is +4" do + assert modifier(18) === 4 + end + end + + describe "random ability" do + test "is within range" do + Enum.each(1..200, fn _ -> assert ability() in 3..18 end) + end + end + + describe "random character" do + test "has each ability within range and valid hitpoints" do + Enum.each(1..20, fn _ -> + character = character() + + assert character.strength in 3..18 + assert character.dexterity in 3..18 + assert character.constitution in 3..18 + assert character.intelligence in 3..18 + assert character.wisdom in 3..18 + assert character.charisma in 3..18 + assert character.hitpoints === 10 + modifier(character.constitution) + end) + end + end +end diff --git a/elixir/dnd-character/test/test_helper.exs b/elixir/dnd-character/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/elixir/dnd-character/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true)