Skip to content

Commit e89a341

Browse files
authored
Add dnd-character (#392)
1 parent ab39fb7 commit e89a341

File tree

8 files changed

+304
-2
lines changed

8 files changed

+304
-2
lines changed

config.json

+10-2
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@
134134
"math"
135135
]
136136
},
137+
{
138+
"slug": "dnd-character",
139+
"name": "D&D Character",
140+
"uuid": "79f913e1-9571-4b1e-81f5-f0f3160898cc",
141+
"practices": [],
142+
"prerequisites": [],
143+
"difficulty": 2
144+
},
137145
{
138146
"slug": "eliuds-eggs",
139147
"name": "Eliud's Eggs",
@@ -458,10 +466,10 @@
458466
"practices": [],
459467
"prerequisites": [],
460468
"difficulty": 2,
469+
"status": "deprecated",
461470
"topics": [
462471
"math"
463-
],
464-
"status": "deprecated"
472+
]
465473
},
466474
{
467475
"slug": "two-fer",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Instructions
2+
3+
For a game of [Dungeons & Dragons][dnd], each player starts by generating a character they can play with.
4+
This character has, among other things, six abilities; strength, dexterity, constitution, intelligence, wisdom and charisma.
5+
These six abilities have scores that are determined randomly.
6+
You do this by rolling four 6-sided dice and recording the sum of the largest three dice.
7+
You do this six times, once for each ability.
8+
9+
Your character's initial hitpoints are 10 + your character's constitution modifier.
10+
You find your character's constitution modifier by subtracting 10 from your character's constitution, divide by 2 and round down.
11+
12+
Write a random character generator that follows the above rules.
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+
~~~~exercism/note
26+
Most programming languages feature (pseudo-)random generators, but few programming languages are designed to roll dice.
27+
One such language is [Troll][troll].
28+
29+
[troll]: https://di.ku.dk/Ansatte/?pure=da%2Fpublications%2Ftroll-a-language-for-specifying-dicerolls(84a45ff0-068b-11df-825d-000ea68e967b)%2Fexport.html
30+
~~~~
31+
32+
[dnd]: https://en.wikipedia.org/wiki/Dungeons_%26_Dragons
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Introduction
2+
3+
After weeks of anticipation, you and your friends get together for your very first game of [Dungeons & Dragons][dnd] (D&D).
4+
Since this is the first session of the game, each player has to generate a character to play with.
5+
The character's abilities are determined by rolling 6-sided dice, but where _are_ the dice?
6+
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!
7+
Panicking, you realize you forgot to bring the dice, which would mean no D&D game.
8+
As you have some basic coding skills, you quickly come up with a solution: you'll write a program to simulate dice rolls.
9+
10+
[dnd]: https://en.wikipedia.org/wiki/Dungeons_%26_Dragons
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"dnd-character.el"
8+
],
9+
"test": [
10+
"dnd-character-test.el"
11+
],
12+
"example": [
13+
".meta/example.el"
14+
]
15+
},
16+
"blurb": "Randomly generate Dungeons & Dragons characters.",
17+
"source": "Simon Shine, Erik Schierboom",
18+
"source_url": "https://github.com/exercism/problem-specifications/issues/616#issuecomment-437358945"
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
;;; dnd-character.el --- D&D Character (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
8+
(defun modifier (score)
9+
(floor (- score 10) 2))
10+
11+
12+
(defun ability ()
13+
(let ((rolls '()))
14+
(dotimes (i 4)
15+
(push (+ 1 (random 6)) rolls))
16+
(apply '+ (cdr (sort rolls '<)))))
17+
18+
(defun generate-dnd-character ()
19+
(let ((constitution (ability)))
20+
(record 'dnd-character
21+
(ability) ; strength
22+
(ability) ; dexterity
23+
constitution ; constitution
24+
(ability) ; intelligence
25+
(ability) ; wisdom
26+
(ability) ; charisma
27+
(+ 10 (modifier constitution))))) ; hitpoints
28+
29+
30+
(provide 'dnd-character)
31+
;;; dnd-character.el ends here
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[1e9ae1dc-35bd-43ba-aa08-e4b94c20fa37]
13+
description = "ability modifier -> ability modifier for score 3 is -4"
14+
15+
[cc9bb24e-56b8-4e9e-989d-a0d1a29ebb9c]
16+
description = "ability modifier -> ability modifier for score 4 is -3"
17+
18+
[5b519fcd-6946-41ee-91fe-34b4f9808326]
19+
description = "ability modifier -> ability modifier for score 5 is -3"
20+
21+
[dc2913bd-6d7a-402e-b1e2-6d568b1cbe21]
22+
description = "ability modifier -> ability modifier for score 6 is -2"
23+
24+
[099440f5-0d66-4b1a-8a10-8f3a03cc499f]
25+
description = "ability modifier -> ability modifier for score 7 is -2"
26+
27+
[cfda6e5c-3489-42f0-b22b-4acb47084df0]
28+
description = "ability modifier -> ability modifier for score 8 is -1"
29+
30+
[c70f0507-fa7e-4228-8463-858bfbba1754]
31+
description = "ability modifier -> ability modifier for score 9 is -1"
32+
33+
[6f4e6c88-1cd9-46a0-92b8-db4a99b372f7]
34+
description = "ability modifier -> ability modifier for score 10 is 0"
35+
36+
[e00d9e5c-63c8-413f-879d-cd9be9697097]
37+
description = "ability modifier -> ability modifier for score 11 is 0"
38+
39+
[eea06f3c-8de0-45e7-9d9d-b8cab4179715]
40+
description = "ability modifier -> ability modifier for score 12 is +1"
41+
42+
[9c51f6be-db72-4af7-92ac-b293a02c0dcd]
43+
description = "ability modifier -> ability modifier for score 13 is +1"
44+
45+
[94053a5d-53b6-4efc-b669-a8b5098f7762]
46+
description = "ability modifier -> ability modifier for score 14 is +2"
47+
48+
[8c33e7ca-3f9f-4820-8ab3-65f2c9e2f0e2]
49+
description = "ability modifier -> ability modifier for score 15 is +2"
50+
51+
[c3ec871e-1791-44d0-b3cc-77e5fb4cd33d]
52+
description = "ability modifier -> ability modifier for score 16 is +3"
53+
54+
[3d053cee-2888-4616-b9fd-602a3b1efff4]
55+
description = "ability modifier -> ability modifier for score 17 is +3"
56+
57+
[bafd997a-e852-4e56-9f65-14b60261faee]
58+
description = "ability modifier -> ability modifier for score 18 is +4"
59+
60+
[4f28f19c-2e47-4453-a46a-c0d365259c14]
61+
description = "random ability is within range"
62+
63+
[385d7e72-864f-4e88-8279-81a7d75b04ad]
64+
description = "random character is valid"
65+
66+
[2ca77b9b-c099-46c3-a02c-0d0f68ffa0fe]
67+
description = "each ability is only calculated once"
68+
include = false
69+
70+
[dca2b2ec-f729-4551-84b9-078876bb4808]
71+
description = "each ability is only calculated once"
72+
reimplements = "2ca77b9b-c099-46c3-a02c-0d0f68ffa0fe"
73+
include = false
74+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
;;; dnd-character-test.el --- D&amp;D Character (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
8+
(load-file "dnd-character.el")
9+
(declare-function modifier "dnd-character.el" (score))
10+
(declare-function ability "dnd-character.el" ())
11+
(declare-function generate-dnd-character "dnd-character.el" ())
12+
13+
14+
(ert-deftest ability-modifier-for-score-3-is-negative-4 ()
15+
(should (= -4 (modifier 3))))
16+
17+
18+
(ert-deftest ability-modifier-for-score-4-is-negative-3 ()
19+
(should (= -3 (modifier 4))))
20+
21+
22+
(ert-deftest ability-modifier-for-score-5-is-negative-3 ()
23+
(should (= -3 (modifier 5))))
24+
25+
26+
(ert-deftest ability-modifier-for-score-6-is-negative-2 ()
27+
(should (= -2 (modifier 6))))
28+
29+
30+
(ert-deftest ability-modifier-for-score-7-is-negative-2 ()
31+
(should (= -2 (modifier 7))))
32+
33+
34+
(ert-deftest ability-modifier-for-score-8-is-negative-1 ()
35+
(should (= -1 (modifier 8))))
36+
37+
38+
(ert-deftest ability-modifier-for-score-9-is-negative-1 ()
39+
(should (= -1 (modifier 9))))
40+
41+
42+
(ert-deftest ability-modifier-for-score-10-is-0 ()
43+
(should (= 0 (modifier 10))))
44+
45+
46+
(ert-deftest ability-modifier-for-score-11-is-0 ()
47+
(should (= 0 (modifier 11))))
48+
49+
50+
(ert-deftest ability-modifier-for-score-12-is-positive-1 ()
51+
(should (= 1 (modifier 12))))
52+
53+
54+
(ert-deftest ability-modifier-for-score-13-is-positive-1 ()
55+
(should (= 1 (modifier 13))))
56+
57+
58+
(ert-deftest ability-modifier-for-score-14-is-positive-2 ()
59+
(should (= 2 (modifier 14))))
60+
61+
62+
(ert-deftest ability-modifier-for-score-15-is-positive-2 ()
63+
(should (= 2 (modifier 15))))
64+
65+
66+
(ert-deftest ability-modifier-for-score-16-is-positive-3 ()
67+
(should (= 3 (modifier 16))))
68+
69+
70+
(ert-deftest ability-modifier-for-score-17-is-positive-3 ()
71+
(should (= 3 (modifier 17))))
72+
73+
74+
(ert-deftest ability-modifier-for-score-18-is-positive-4 ()
75+
(should (= 4 (modifier 18))))
76+
77+
78+
(defun valid-range-p (score)
79+
(and (<= 3 score) (>= 18 score)))
80+
81+
(ert-deftest random-ability-is-within-range ()
82+
(should (valid-range-p (ability))))
83+
84+
85+
(ert-deftest random-character-is-valid ()
86+
(let* ((dnd-char (generate-dnd-character))
87+
(strength (aref dnd-char 1))
88+
(dexterity (aref dnd-char 2))
89+
(constitution (aref dnd-char 3))
90+
(intelligence (aref dnd-char 4))
91+
(wisdom (aref dnd-char 5))
92+
(charisma (aref dnd-char 6))
93+
(hitpoints (aref dnd-char 7)))
94+
(should (and (recordp dnd-char)
95+
(valid-range-p strength)
96+
(valid-range-p dexterity)
97+
(valid-range-p constitution)
98+
(valid-range-p intelligence)
99+
(valid-range-p wisdom)
100+
(valid-range-p charisma)))))
101+
102+
103+
(provide 'dnd-character-test)
104+
;;; dnd-character-test.el ends here
105+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
;;; dnd-character.el --- D&amp;D Character (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
8+
(defun modifier (score)
9+
(error "Delete this S-Expression and write your own implementation"))
10+
11+
12+
(defun ability ()
13+
(error "Delete this S-Expression and write your own implementation"))
14+
15+
16+
(defun generate-dnd-character ()
17+
(error "Delete this S-Expression and write your own implementation"))
18+
19+
20+
(provide 'dnd-character)
21+
;;; dnd-character.el ends here
22+

0 commit comments

Comments
 (0)