Skip to content

Commit 9bdd710

Browse files
committed
updates
1 parent 1b99db8 commit 9bdd710

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+863
-107
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"track":"elixir","exercise":"dnd-character","id":"28ecd6ba5d0c42d2a91cbdf015d7aee3","url":"https://exercism.io/my/solutions/28ecd6ba5d0c42d2a91cbdf015d7aee3","handle":"neenjaw","is_requester":true,"auto_approve":false}

dnd-character/.formatter.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Used by "mix format"
2+
[
3+
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
4+
]

dnd-character/.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# The directory Mix will write compiled artifacts to.
2+
/_build/
3+
4+
# If you run "mix test --cover", coverage assets end up here.
5+
/cover/
6+
7+
# The directory Mix downloads your dependencies sources to.
8+
/deps/
9+
10+
# Where third-party dependencies like ExDoc output generated docs.
11+
/doc/
12+
13+
# Ignore .fetch files in case you like to edit your project deps locally.
14+
/.fetch
15+
16+
# If the VM crashes, it generates a dump, let's ignore it too.
17+
erl_crash.dump
18+
19+
# Also ignore archive artifacts (built via "mix archive.build").
20+
*.ez
21+
22+
# Ignore package tarball (built via "mix hex.build").
23+
dnd_character-*.tar
24+

dnd-character/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
For a game of [Dungeons & Dragons][DND], each player starts by generating a
2+
character they can play with. This character has, among other things, six
3+
abilities; strength, dexterity, constitution, intelligence, wisdom and
4+
charisma. These six abilities have scores that are determined randomly. You
5+
do this by rolling four 6-sided dice and record the sum of the largest three
6+
dice. You do this six times, once for each ability.
7+
8+
Your character's initial hitpoints are 10 + your character's constitution
9+
modifier. You find your character's constitution modifier by subtracting 10
10+
from your character's constitution, divide by 2 and round down.
11+
12+
Write a random character generator that follows the rules above.
13+
14+
For example, the six throws of four dice may look like:
15+
16+
* 5, 3, 1, 6: You discard the 1 and sum 5 + 3 + 6 = 14, which you assign to strength.
17+
* 3, 2, 5, 3: You discard the 2 and sum 3 + 5 + 3 = 11, which you assign to dexterity.
18+
* 1, 1, 1, 1: You discard the 1 and sum 1 + 1 + 1 = 3, which you assign to constitution.
19+
* 2, 1, 6, 6: You discard the 1 and sum 2 + 6 + 6 = 14, which you assign to intelligence.
20+
* 3, 5, 3, 4: You discard the 3 and sum 5 + 3 + 4 = 12, which you assign to wisdom.
21+
* 6, 6, 6, 6: You discard the 6 and sum 6 + 6 + 6 = 18, which you assign to charisma.
22+
23+
Because constitution is 3, the constitution modifier is -4 and the hitpoints are 6.
24+
25+
## Notes
26+
27+
Most programming languages feature (pseudo-)random generators, but few
28+
programming languages are designed to roll dice. One such language is [Troll].
29+
30+
[DND]: https://en.wikipedia.org/wiki/Dungeons_%26_Dragons
31+
[Troll]: http://hjemmesider.diku.dk/~torbenm/Troll/

dnd-character/lib/dnd_character.ex

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
defmodule DndCharacter do
2+
@type t :: %__MODULE__{
3+
strength: pos_integer(),
4+
dexterity: pos_integer(),
5+
constitution: pos_integer(),
6+
intelligence: pos_integer(),
7+
wisdom: pos_integer(),
8+
charisma: pos_integer(),
9+
hitpoints: pos_integer()
10+
}
11+
12+
defstruct ~w[strength dexterity constitution intelligence wisdom charisma hitpoints]a
13+
14+
@spec modifier(pos_integer()) :: integer()
15+
def modifier(score) do
16+
Integer.floor_div((score - 10), 2)
17+
end
18+
19+
@spec ability :: pos_integer()
20+
def ability do
21+
1..4
22+
|> Enum.map(fn _ -> Enum.random(1..6) end)
23+
|> Enum.sort()
24+
|> Enum.drop(1)
25+
|> Enum.sum()
26+
end
27+
28+
@spec character :: t()
29+
def character do
30+
constitution = ability()
31+
32+
%__MODULE__{
33+
strength: ability(),
34+
dexterity: ability(),
35+
constitution: constitution,
36+
intelligence: ability(),
37+
wisdom: ability(),
38+
charisma: ability(),
39+
hitpoints: 10 + modifier(constitution)
40+
}
41+
end
42+
end

dnd-character/mix.exs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
defmodule DndCharacter.MixProject do
2+
use Mix.Project
3+
4+
def project do
5+
[
6+
app: :dnd_character,
7+
version: "0.1.0",
8+
# elixir: "~> 1.8",
9+
start_permanent: Mix.env() == :prod,
10+
deps: deps()
11+
]
12+
end
13+
14+
# Run "mix help compile.app" to learn about applications.
15+
def application do
16+
[
17+
extra_applications: [:logger]
18+
]
19+
end
20+
21+
# Run "mix help deps" to learn about dependencies.
22+
defp deps do
23+
[
24+
# {:dep_from_hexpm, "~> 0.3.0"},
25+
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
26+
]
27+
end
28+
end
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
defmodule DndCharacterTest do
2+
use ExUnit.Case
3+
4+
import DndCharacter, only: [modifier: 1, ability: 0, character: 0]
5+
6+
# canonical data version: 1.1.0
7+
8+
describe "ability modifier" do
9+
# @tag :pending
10+
test "for score 3 is -4" do
11+
assert modifier(3) === -4
12+
end
13+
14+
@tag :pending
15+
test "for score 4 is -3" do
16+
assert modifier(4) === -3
17+
end
18+
19+
@tag :pending
20+
test "for score 5 is -3" do
21+
assert modifier(5) === -3
22+
end
23+
24+
@tag :pending
25+
test "for score 6 is -2" do
26+
assert modifier(6) === -2
27+
end
28+
29+
@tag :pending
30+
test "for score 7 is -2" do
31+
assert modifier(7) === -2
32+
end
33+
34+
@tag :pending
35+
test "for score 8 is -1" do
36+
assert modifier(8) === -1
37+
end
38+
39+
@tag :pending
40+
test "for score 9 is -1" do
41+
assert modifier(9) === -1
42+
end
43+
44+
@tag :pending
45+
test "for score 10 is 0" do
46+
assert modifier(10) === 0
47+
end
48+
49+
@tag :pending
50+
test "for score 11 is 0" do
51+
assert modifier(11) === 0
52+
end
53+
54+
@tag :pending
55+
test "for score 12 is +1" do
56+
assert modifier(12) === 1
57+
end
58+
59+
@tag :pending
60+
test "for score 13 is +1" do
61+
assert modifier(13) === 1
62+
end
63+
64+
@tag :pending
65+
test "for score 14 is +2" do
66+
assert modifier(14) === 2
67+
end
68+
69+
@tag :pending
70+
test "for score 15 is +2" do
71+
assert modifier(15) === 2
72+
end
73+
74+
@tag :pending
75+
test "for score 16 is +3" do
76+
assert modifier(16) === 3
77+
end
78+
79+
@tag :pending
80+
test "for score 17 is +3" do
81+
assert modifier(17) === 3
82+
end
83+
84+
@tag :pending
85+
test "for score 18 is +4" do
86+
assert modifier(18) === 4
87+
end
88+
end
89+
90+
describe "random ability" do
91+
@tag :pending
92+
test "is within range" do
93+
Enum.each(1..200, fn _ -> assert ability() in 3..18 end)
94+
end
95+
end
96+
97+
describe "random character" do
98+
setup do
99+
%{character: character()}
100+
end
101+
102+
@tag :pending
103+
test "has valid hitpoints", %{character: character} do
104+
assert character.hitpoints === 10 + modifier(character.constitution)
105+
end
106+
107+
@tag :pending
108+
test "has each ability only calculated once", %{character: character} do
109+
assert character.strength === character.strength
110+
end
111+
end
112+
end

dnd-character/test/test_helper.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ExUnit.start()
2+
ExUnit.configure(exclude: :pending, trace: true)

forth/forth.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ defmodule Forth do
3030
{"swap", &operation_swap/1},
3131
{"over", &operation_over/1}
3232
]
33-
|> Enum.reduce(%{}, fn {operator, operation}, map -> Map.put(map, operator, operation end))
33+
|> Enum.reduce(%{}, fn {operator, operation}, map -> Map.put(map, operator, operation) end)
3434
end
3535

3636
# Add function

forth/forth_test.exs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ defmodule ForthTest do
101101
end
102102
end
103103

104+
test "drop then redefining a redefined built-in word" do
105+
s =
106+
Forth.new()
107+
|> Forth.eval("1 drop : swap dup ; 1 swap : swap + ; swap")
108+
|> Forth.format_stack()
109+
110+
assert s == "2"
111+
end
112+
104113
# @tag :pending
105114
test "swap" do
106115
s =
@@ -185,6 +194,17 @@ defmodule ForthTest do
185194
assert s == "1 1"
186195
end
187196

197+
# @tag :pending
198+
test "redefining a redefined built-in word" do
199+
s =
200+
Forth.new()
201+
|> Forth.eval(": swap dup ; 1 swap")
202+
|> Forth.eval(": swap + ; swap")
203+
|> Forth.format_stack()
204+
205+
assert s == "2"
206+
end
207+
188208
# @tag :pending
189209
test "defining words with odd characters" do
190210
s =

0 commit comments

Comments
 (0)