From 7a93a9962a0ce504739ca6f9842167fc14eeb1b6 Mon Sep 17 00:00:00 2001 From: G-J van Rooyen Date: Sun, 8 Sep 2024 16:55:45 +0200 Subject: [PATCH] Added the dnd-character exercise --- config.json | 8 ++ .../dnd-character/.docs/instructions.md | 32 +++++ .../dnd-character/.docs/introduction.md | 10 ++ .../practice/dnd-character/.meta/config.json | 19 +++ .../.meta/dnd_character_example.odin | 43 ++++++ .../practice/dnd-character/.meta/tests.toml | 72 ++++++++++ .../practice/dnd-character/dnd_character.odin | 15 +++ .../dnd-character/dnd_character_test.odin | 124 ++++++++++++++++++ 8 files changed, 323 insertions(+) create mode 100644 exercises/practice/dnd-character/.docs/instructions.md create mode 100644 exercises/practice/dnd-character/.docs/introduction.md create mode 100644 exercises/practice/dnd-character/.meta/config.json create mode 100644 exercises/practice/dnd-character/.meta/dnd_character_example.odin create mode 100644 exercises/practice/dnd-character/.meta/tests.toml create mode 100644 exercises/practice/dnd-character/dnd_character.odin create mode 100644 exercises/practice/dnd-character/dnd_character_test.odin diff --git a/config.json b/config.json index eeb5d46..141e54b 100644 --- a/config.json +++ b/config.json @@ -138,6 +138,14 @@ "practices": [], "prerequisites": [], "difficulty": 1 + }, + { + "slug": "dnd-character", + "name": "D&D Character", + "uuid": "15654c1f-b8ba-4d18-9e39-a7f6c27b2a51", + "practices": [], + "prerequisites": [], + "difficulty": 1 } ] }, diff --git a/exercises/practice/dnd-character/.docs/instructions.md b/exercises/practice/dnd-character/.docs/instructions.md new file mode 100644 index 0000000..e14e794 --- /dev/null +++ b/exercises/practice/dnd-character/.docs/instructions.md @@ -0,0 +1,32 @@ +# 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 diff --git a/exercises/practice/dnd-character/.docs/introduction.md b/exercises/practice/dnd-character/.docs/introduction.md new file mode 100644 index 0000000..5301f61 --- /dev/null +++ b/exercises/practice/dnd-character/.docs/introduction.md @@ -0,0 +1,10 @@ +# 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 diff --git a/exercises/practice/dnd-character/.meta/config.json b/exercises/practice/dnd-character/.meta/config.json new file mode 100644 index 0000000..f70525c --- /dev/null +++ b/exercises/practice/dnd-character/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "gvrooyen" + ], + "files": { + "solution": [ + "dnd_character.odin" + ], + "test": [ + "dnd_character_test.odin" + ], + "example": [ + ".meta/dnd_character_example.odin" + ] + }, + "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/exercises/practice/dnd-character/.meta/dnd_character_example.odin b/exercises/practice/dnd-character/.meta/dnd_character_example.odin new file mode 100644 index 0000000..e81c51d --- /dev/null +++ b/exercises/practice/dnd-character/.meta/dnd_character_example.odin @@ -0,0 +1,43 @@ +package dnd_character + +import "core:log" +import "core:math" +import "core:math/rand" +import "core:slice" + +Character :: struct { + strength: int, + dexterity: int, + constitution: int, + intelligence: int, + wisdom: int, + charisma: int, + hitpoints: int, +} + +modifier :: proc(score: int) -> int { + return math.floor_div(score - 10, 2) +} + +ability :: proc() -> int { + rolls := [4]int{} + for i in 0 ..= 3 { + rolls[i] = rand.int_max(6) + 1 + } + slice.reverse_sort(rolls[:]) + return rolls[0] + rolls[1] + rolls[2] +} + +character :: proc() -> Character { + c := Character { + ability(), + ability(), + ability(), + ability(), + ability(), + ability(), + 0, + } + c.hitpoints = 10 + modifier(c.constitution) + return c +} diff --git a/exercises/practice/dnd-character/.meta/tests.toml b/exercises/practice/dnd-character/.meta/tests.toml new file mode 100644 index 0000000..719043b --- /dev/null +++ b/exercises/practice/dnd-character/.meta/tests.toml @@ -0,0 +1,72 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[1e9ae1dc-35bd-43ba-aa08-e4b94c20fa37] +description = "ability modifier -> ability modifier for score 3 is -4" + +[cc9bb24e-56b8-4e9e-989d-a0d1a29ebb9c] +description = "ability modifier -> ability modifier for score 4 is -3" + +[5b519fcd-6946-41ee-91fe-34b4f9808326] +description = "ability modifier -> ability modifier for score 5 is -3" + +[dc2913bd-6d7a-402e-b1e2-6d568b1cbe21] +description = "ability modifier -> ability modifier for score 6 is -2" + +[099440f5-0d66-4b1a-8a10-8f3a03cc499f] +description = "ability modifier -> ability modifier for score 7 is -2" + +[cfda6e5c-3489-42f0-b22b-4acb47084df0] +description = "ability modifier -> ability modifier for score 8 is -1" + +[c70f0507-fa7e-4228-8463-858bfbba1754] +description = "ability modifier -> ability modifier for score 9 is -1" + +[6f4e6c88-1cd9-46a0-92b8-db4a99b372f7] +description = "ability modifier -> ability modifier for score 10 is 0" + +[e00d9e5c-63c8-413f-879d-cd9be9697097] +description = "ability modifier -> ability modifier for score 11 is 0" + +[eea06f3c-8de0-45e7-9d9d-b8cab4179715] +description = "ability modifier -> ability modifier for score 12 is +1" + +[9c51f6be-db72-4af7-92ac-b293a02c0dcd] +description = "ability modifier -> ability modifier for score 13 is +1" + +[94053a5d-53b6-4efc-b669-a8b5098f7762] +description = "ability modifier -> ability modifier for score 14 is +2" + +[8c33e7ca-3f9f-4820-8ab3-65f2c9e2f0e2] +description = "ability modifier -> ability modifier for score 15 is +2" + +[c3ec871e-1791-44d0-b3cc-77e5fb4cd33d] +description = "ability modifier -> ability modifier for score 16 is +3" + +[3d053cee-2888-4616-b9fd-602a3b1efff4] +description = "ability modifier -> ability modifier for score 17 is +3" + +[bafd997a-e852-4e56-9f65-14b60261faee] +description = "ability modifier -> ability modifier for score 18 is +4" + +[4f28f19c-2e47-4453-a46a-c0d365259c14] +description = "random ability is within range" + +[385d7e72-864f-4e88-8279-81a7d75b04ad] +description = "random character is valid" + +[2ca77b9b-c099-46c3-a02c-0d0f68ffa0fe] +description = "each ability is only calculated once" +include = false + +[dca2b2ec-f729-4551-84b9-078876bb4808] +description = "each ability is only calculated once" +reimplements = "2ca77b9b-c099-46c3-a02c-0d0f68ffa0fe" diff --git a/exercises/practice/dnd-character/dnd_character.odin b/exercises/practice/dnd-character/dnd_character.odin new file mode 100644 index 0000000..7ac6f6a --- /dev/null +++ b/exercises/practice/dnd-character/dnd_character.odin @@ -0,0 +1,15 @@ +package dnd_character + +Character :: struct {} // Please complete the `Character` struct. + +modifier :: proc(score: int) -> int { + #panic("Please complete the `modifier` procedure.") +} + +ability :: proc() -> int { + #panic("Please complete the `ability` procedure.") +} + +character :: proc() -> Character { + #panic("Please complete the `character` procedure.") +} diff --git a/exercises/practice/dnd-character/dnd_character_test.odin b/exercises/practice/dnd-character/dnd_character_test.odin new file mode 100644 index 0000000..3f39272 --- /dev/null +++ b/exercises/practice/dnd-character/dnd_character_test.odin @@ -0,0 +1,124 @@ +/* These are the unit tests for the exercise. Only the first one is enabled to start with. You can + * enable the other tests by uncommenting the `@(test)` attribute of the test procedure. Your + * solution should pass all tests before it is ready for submission. + */ + +package dnd_character + +import "core:mem" +import "core:testing" + +FUZZ_ITERATIONS :: 20 + +@(test) +test_ability_modifier_for_score_3_is_minus_4 :: proc(t: ^testing.T) { + testing.expect_value(t, modifier(3), -4) +} + +// @(test) +test_ability_modifier_for_score_4_is_minus_3 :: proc(t: ^testing.T) { + testing.expect_value(t, modifier(4), -3) +} + +// @(test) +test_ability_modifier_for_score_5_is_minus_3 :: proc(t: ^testing.T) { + testing.expect_value(t, modifier(5), -3) +} + +// @(test) +test_ability_modifier_for_score_6_is_minus_2 :: proc(t: ^testing.T) { + testing.expect_value(t, modifier(6), -2) +} + +// @(test) +test_ability_modifier_for_score_7_is_minus_2 :: proc(t: ^testing.T) { + testing.expect_value(t, modifier(7), -2) +} + +// @(test) +test_ability_modifier_for_score_8_is_minus_1 :: proc(t: ^testing.T) { + testing.expect_value(t, modifier(8), -1) +} + +// @(test) +test_ability_modifier_for_score_9_is_minus_1 :: proc(t: ^testing.T) { + testing.expect_value(t, modifier(9), -1) +} + +// @(test) +test_ability_modifier_for_score_10_is_0 :: proc(t: ^testing.T) { + testing.expect_value(t, modifier(10), 0) +} + +// @(test) +test_ability_modifier_for_score_11_is_0 :: proc(t: ^testing.T) { + testing.expect_value(t, modifier(11), 0) +} + +// @(test) +test_ability_modifier_for_score_12_is_plus_1 :: proc(t: ^testing.T) { + testing.expect_value(t, modifier(12), 1) +} + +// @(test) +test_ability_modifier_for_score_13_is_plus_1 :: proc(t: ^testing.T) { + testing.expect_value(t, modifier(13), 1) +} + +// @(test) +test_ability_modifier_for_score_14_is_plus_2 :: proc(t: ^testing.T) { + testing.expect_value(t, modifier(14), 2) +} + +// @(test) +test_ability_modifier_for_score_15_is_plus_2 :: proc(t: ^testing.T) { + testing.expect_value(t, modifier(15), 2) +} + +// @(test) +test_ability_modifier_for_score_16_is_plus_3 :: proc(t: ^testing.T) { + testing.expect_value(t, modifier(16), 3) +} + +// @(test) +test_ability_modifier_for_score_17_is_plus_3 :: proc(t: ^testing.T) { + testing.expect_value(t, modifier(17), 3) +} + +// @(test) +test_ability_modifier_for_score_18_is_plus_4 :: proc(t: ^testing.T) { + testing.expect_value(t, modifier(18), 4) +} + +// @(test) +test_random_ability_is_within_range :: proc(t: ^testing.T) { + for _ in 0 ..< FUZZ_ITERATIONS { + a := ability() + testing.expect(t, a >= 3 && a <= 18) + } +} + +// @(test) +test_random_character_is_valid :: proc(t: ^testing.T) { + for _ in 0 ..< FUZZ_ITERATIONS { + c := character() + testing.expect(t, c.strength >= 3 && c.strength <= 18) + testing.expect(t, c.dexterity >= 3 && c.dexterity <= 18) + testing.expect(t, c.constitution >= 3 && c.constitution <= 18) + testing.expect(t, c.intelligence >= 3 && c.intelligence <= 18) + testing.expect(t, c.wisdom >= 3 && c.wisdom <= 18) + testing.expect(t, c.charisma >= 3 && c.charisma <= 18) + testing.expect_value(t, c.hitpoints, 10 + modifier(c.constitution)) + } +} + +// @(test) +test_no_memory_leaks :: proc(t: ^testing.T) { + track: mem.Tracking_Allocator + mem.tracking_allocator_init(&track, context.allocator) + defer mem.tracking_allocator_destroy(&track) + context.allocator = mem.tracking_allocator(&track) + test_random_character_is_valid(t) + testing.expect_value(t, len(track.allocation_map), 0) + testing.expect_value(t, len(track.bad_free_array), 0) +}