Skip to content

Commit 2e5537d

Browse files
authored
Add practice exercise: dnd-character (#204)
1 parent f03eb06 commit 2e5537d

File tree

11 files changed

+573
-0
lines changed

11 files changed

+573
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,14 @@
186186
"prerequisites": [],
187187
"difficulty": 5
188188
},
189+
{
190+
"slug": "dnd-character",
191+
"name": "D&D Character",
192+
"uuid": "d2da9563-a621-4e02-b2e4-f9553f3b82fc",
193+
"practices": [],
194+
"prerequisites": [],
195+
"difficulty": 5
196+
},
189197
{
190198
"slug": "eliuds-eggs",
191199
"name": "Eliud's Eggs",
Lines changed: 32 additions & 0 deletions
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
Lines changed: 10 additions & 0 deletions
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
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"jimmytty"
4+
],
5+
"files": {
6+
"solution": [
7+
"dnd-character.sql"
8+
],
9+
"test": [
10+
"dnd-character_test.sql"
11+
],
12+
"example": [
13+
".meta/example.sql"
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+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
UPDATE "dnd-character"
2+
SET modifier = FLOOR((constitution - 10) / 2.0)
3+
WHERE property = 'modifier'
4+
AND input = 'score'
5+
;
6+
7+
UPDATE "dnd-character"
8+
SET wisdom = (
9+
SELECT SUM(score)
10+
FROM (
11+
SELECT ABS(RANDOM()) % (6 - 1) + 1 AS score
12+
FROM generate_series(1, 4) AS times
13+
ORDER BY score DESC
14+
LIMIT 3
15+
)
16+
)
17+
WHERE property = 'ability'
18+
AND input = 'random'
19+
;
20+
21+
22+
UPDATE "dnd-character"
23+
SET
24+
strength = (
25+
SELECT SUM(score)
26+
FROM (
27+
SELECT ABS(RANDOM()) % (6 - 1) + 1 AS score
28+
FROM generate_series(1, 4) AS times
29+
ORDER BY score DESC
30+
LIMIT 3
31+
)
32+
),
33+
dexterity = (
34+
SELECT SUM(score)
35+
FROM (
36+
SELECT ABS(RANDOM()) % (6 - 1) + 1 AS score
37+
FROM generate_series(1, 4) AS times
38+
ORDER BY score DESC
39+
LIMIT 3
40+
)
41+
),
42+
constitution = (
43+
SELECT SUM(score)
44+
FROM (
45+
SELECT ABS(RANDOM()) % (6 - 1) + 1 AS score
46+
FROM generate_series(1, 4) AS times
47+
ORDER BY score DESC
48+
LIMIT 3
49+
)
50+
),
51+
intelligence = (
52+
SELECT SUM(score)
53+
FROM (
54+
SELECT ABS(RANDOM()) % (6 - 1) + 1 AS score
55+
FROM generate_series(1, 4) AS times
56+
ORDER BY score DESC
57+
LIMIT 3
58+
)
59+
),
60+
wisdom = (
61+
SELECT SUM(score)
62+
FROM (
63+
SELECT ABS(RANDOM()) % (6 - 1) + 1 AS score
64+
FROM generate_series(1, 4) AS times
65+
ORDER BY score DESC
66+
LIMIT 3
67+
)
68+
),
69+
charisma = (
70+
SELECT SUM(score)
71+
FROM (
72+
SELECT ABS(RANDOM()) % (6 - 1) + 1 AS score
73+
FROM generate_series(1, 4) AS times
74+
ORDER BY score DESC
75+
LIMIT 3
76+
)
77+
)
78+
WHERE property = 'character'
79+
AND input = 'random'
80+
;
81+
UPDATE "dnd-character"
82+
SET modifier = FLOOR((constitution - 10) / 2.0)
83+
WHERE property = 'character'
84+
AND input = 'random'
85+
;
86+
UPDATE "dnd-character"
87+
SET hitpoints = 10 + modifier
88+
WHERE property = 'character'
89+
AND input = 'random'
90+
;
91+
92+
UPDATE "dnd-character" AS a
93+
SET strength = b.strength,
94+
dexterity = b.dexterity,
95+
constitution = b.constitution,
96+
intelligence = b.intelligence,
97+
wisdom = b.wisdom,
98+
charisma = b.charisma
99+
FROM "dnd-character" AS b
100+
WHERE a.property = b.property
101+
AND a.property = 'character'
102+
AND a.input = ''
103+
AND b.input = 'random'
104+
;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
DROP TABLE IF EXISTS "dnd-character";
2+
CREATE TABLE "dnd-character" (
3+
property TEXT NOT NULL,
4+
input TEXT NOT NULL,
5+
strength INTEGER ,
6+
dexterity INTEGER ,
7+
constitution INTEGER ,
8+
intelligence INTEGER ,
9+
wisdom INTEGER ,
10+
charisma INTEGER ,
11+
modifier INTEGER ,
12+
hitpoints INTEGER
13+
);
14+
15+
.mode csv
16+
.import ./data.csv "dnd-character"
17+
18+
UPDATE "dnd-character" SET strength = NULL WHERE strength = '';
19+
UPDATE "dnd-character" SET dexterity = NULL WHERE dexterity = '';
20+
UPDATE "dnd-character" SET constitution = NULL WHERE constitution = '';
21+
UPDATE "dnd-character" SET intelligence = NULL WHERE intelligence = '';
22+
UPDATE "dnd-character" SET wisdom = NULL WHERE wisdom = '';
23+
UPDATE "dnd-character" SET charisma = NULL WHERE charisma = '';
24+
UPDATE "dnd-character" SET modifier = NULL WHERE modifier = '';
25+
UPDATE "dnd-character" SET hitpoints = NULL WHERE hitpoints = '';
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
DROP TABLE IF EXISTS tests;
2+
CREATE TABLE IF NOT EXISTS tests (
3+
-- uuid and description are taken from the test.toml file
4+
uuid TEXT PRIMARY KEY,
5+
description TEXT NOT NULL,
6+
-- The following section is needed by the online test-runner
7+
status TEXT DEFAULT 'fail',
8+
message TEXT,
9+
output TEXT,
10+
test_code TEXT,
11+
task_id INTEGER DEFAULT NULL,
12+
-- Here are columns for the actual tests
13+
property TEXT NOT NULL,
14+
input TEXT NOT NULL,
15+
constitution INTEGER,
16+
modifier INTEGER,
17+
expected TEXT NOT NULL
18+
);
19+
20+
INSERT INTO tests (
21+
uuid,
22+
description,
23+
property,
24+
input,
25+
constitution,
26+
modifier,
27+
expected
28+
)
29+
VALUES
30+
('1e9ae1dc-35bd-43ba-aa08-e4b94c20fa37', 'ability modifier for score 3 is -4', 'modifier', 'score', 3, -4, ''),
31+
('cc9bb24e-56b8-4e9e-989d-a0d1a29ebb9c', 'ability modifier for score 4 is -3', 'modifier', 'score', 4, -3, ''),
32+
('5b519fcd-6946-41ee-91fe-34b4f9808326', 'ability modifier for score 5 is -3', 'modifier', 'score', 5, -3, ''),
33+
('dc2913bd-6d7a-402e-b1e2-6d568b1cbe21', 'ability modifier for score 6 is -2', 'modifier', 'score', 6, -2, ''),
34+
('099440f5-0d66-4b1a-8a10-8f3a03cc499f', 'ability modifier for score 7 is -2', 'modifier', 'score', 7, -2, ''),
35+
('cfda6e5c-3489-42f0-b22b-4acb47084df0', 'ability modifier for score 8 is -1', 'modifier', 'score', 8, -1, ''),
36+
('c70f0507-fa7e-4228-8463-858bfbba1754', 'ability modifier for score 9 is -1', 'modifier', 'score', 9, -1, ''),
37+
('6f4e6c88-1cd9-46a0-92b8-db4a99b372f7', 'ability modifier for score 10 is 0', 'modifier', 'score', 10, 0, ''),
38+
('e00d9e5c-63c8-413f-879d-cd9be9697097', 'ability modifier for score 11 is 0', 'modifier', 'score', 11, 0, ''),
39+
('eea06f3c-8de0-45e7-9d9d-b8cab4179715', 'ability modifier for score 12 is +1', 'modifier', 'score', 12, 1, ''),
40+
('9c51f6be-db72-4af7-92ac-b293a02c0dcd', 'ability modifier for score 13 is +1', 'modifier', 'score', 13, 1, ''),
41+
('94053a5d-53b6-4efc-b669-a8b5098f7762', 'ability modifier for score 14 is +2', 'modifier', 'score', 14, 2, ''),
42+
('8c33e7ca-3f9f-4820-8ab3-65f2c9e2f0e2', 'ability modifier for score 15 is +2', 'modifier', 'score', 15, 2, ''),
43+
('c3ec871e-1791-44d0-b3cc-77e5fb4cd33d', 'ability modifier for score 16 is +3', 'modifier', 'score', 16, 3, ''),
44+
('3d053cee-2888-4616-b9fd-602a3b1efff4', 'ability modifier for score 17 is +3', 'modifier', 'score', 17, 3, ''),
45+
('bafd997a-e852-4e56-9f65-14b60261faee', 'ability modifier for score 18 is +4', 'modifier', 'score', 18, 4, ''),
46+
('4f28f19c-2e47-4453-a46a-c0d365259c14', 'random ability is within range', 'ability', 'random', NULL, NULL, 'score >= 3 && score <= 18'),
47+
('385d7e72-864f-4e88-8279-81a7d75b04ad', 'random character is valid', 'character', 'random', NULL, NULL, '{"strength":"strength >= 3 && strength <= 18","dexterity":"dexterity >= 3 && dexterity <= 18","constitution":"constitution >= 3 && constitution <= 18","intelligence":"intelligence >= 3 && intelligence <= 18","wisdom":"wisdom >= 3 && wisdom <= 18","charisma":"charisma >= 3 && charisma <= 18","hitpoints":"hitpoints == 10 + modifier(constitution)"}'),
48+
('dca2b2ec-f729-4551-84b9-078876bb4808', 'each ability is only calculated once', 'character', '', NULL, NULL, '{"strength":"strength == strength","dexterity":"dexterity == dexterity","constitution":"constitution == constitution","intelligence":"intelligence == intelligence","wisdom":"wisdom == wisdom","charisma":"charisma == charisma"}');
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"modifier","score",,,3,,,,,
2+
"modifier","score",,,4,,,,,
3+
"modifier","score",,,5,,,,,
4+
"modifier","score",,,6,,,,,
5+
"modifier","score",,,7,,,,,
6+
"modifier","score",,,8,,,,,
7+
"modifier","score",,,9,,,,,
8+
"modifier","score",,,10,,,,,
9+
"modifier","score",,,11,,,,,
10+
"modifier","score",,,12,,,,,
11+
"modifier","score",,,13,,,,,
12+
"modifier","score",,,14,,,,,
13+
"modifier","score",,,15,,,,,
14+
"modifier","score",,,16,,,,,
15+
"modifier","score",,,17,,,,,
16+
"modifier","score",,,18,,,,,
17+
"ability","random",,,,,,,,
18+
"character","random",,,,,,,,
19+
"character","",,,,,,,,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- Schema:
2+
-- CREATE TABLE "dnd-character" (
3+
-- property TEXT NOT NULL,
4+
-- input TEXT NOT NULL,
5+
-- strength INTEGER ,
6+
-- dexterity INTEGER ,
7+
-- constitution INTEGER ,
8+
-- intelligence INTEGER ,
9+
-- wisdom INTEGER ,
10+
-- charisma INTEGER ,
11+
-- modifier INTEGER ,
12+
-- hitpoints INTEGER
13+
-- );
14+
--
15+
-- Task: update the dnd-character table and set the appropriate columns based on the property and the input.

0 commit comments

Comments
 (0)