Skip to content

feat: add dnd-character exercise #802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 7
},
{
"slug": "dnd-character",
"name": "D&D Character",
"uuid": "83e20a67-990e-4db7-b16a-48b4f516f893",
"practices": [],
"prerequisites": [],
"difficulty": 3
}
],
"foregone": [
Expand Down
31 changes: 31 additions & 0 deletions exercises/practice/dnd-character/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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 record 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 rules above.

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.

## Notes

Most programming languages feature (pseudo-)random generators, but few programming languages are designed to roll dice.
One such language is [Troll][troll].

[dnd]: https://en.wikipedia.org/wiki/Dungeons_%26_Dragons
[troll]: https://di.ku.dk/Ansatte/?pure=da%2Fpublications%2Ftroll-a-language-for-specifying-dicerolls(84a45ff0-068b-11df-825d-000ea68e967b)%2Fexport.html
21 changes: 21 additions & 0 deletions exercises/practice/dnd-character/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"authors": [
"vaeng"
],
"files": {
"solution": [
"dnd_character.cpp",
"dnd_character.h"
],
"test": [
"dnd_character_test.cpp"
],
"example": [
".meta/example.cpp",
".meta/example.h"
]
},
"blurb": "Randomly generate Dungeons & Dragons characters.",
"source": "Simon Shine, Erik Schierboom",
"source_url": "https://github.com/exercism/problem-specifications/issues/616#issuecomment-437358945"
}
17 changes: 17 additions & 0 deletions exercises/practice/dnd-character/.meta/example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "dnd_character.h"

#include <cmath>
#include <cstdlib>

namespace dnd_character {
int modifier(int score) {
return std::floor((static_cast<double>(score) - 10) / 2);
}

int dice_roll() { return 1 + std::rand() / ((RAND_MAX + 1u) / 6); }

// Throwing three dice is not the same as selecting a random number between 3
// and 18.
int ability() { return dice_roll() + dice_roll() + dice_roll(); }

} // namespace dnd_character
26 changes: 26 additions & 0 deletions exercises/practice/dnd-character/.meta/example.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

namespace dnd_character {
int modifier(int score);
int ability();

struct Character {
Character() {
strength = ability();
dexterity = ability();
constitution = ability();
intelligence = ability();
wisdom = ability();
charisma = ability();
hitpoints = 10 + modifier(constitution);
};
int strength;
int dexterity;
int constitution;
int intelligence;
int wisdom;
int charisma;
int hitpoints;
};

} // namespace dnd_character
72 changes: 72 additions & 0 deletions exercises/practice/dnd-character/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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"
64 changes: 64 additions & 0 deletions exercises/practice/dnd-character/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Get the exercise name from the current directory
get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME)

# Basic CMake project
cmake_minimum_required(VERSION 3.5.1)

# Name the project after the exercise
project(${exercise} CXX)

# Get a source filename from the exercise name by replacing -'s with _'s
string(REPLACE "-" "_" file ${exercise})

# Implementation could be only a header
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.cpp)
set(exercise_cpp ${file}.cpp)
else()
set(exercise_cpp "")
endif()

# Use the common Catch library?
if(EXERCISM_COMMON_CATCH)
# For Exercism track development only
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h $<TARGET_OBJECTS:catchlib>)
elseif(EXERCISM_TEST_SUITE)
# The Exercism test suite is being run, the Docker image already
# includes a pre-built version of Catch.
find_package(Catch2 REQUIRED)
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h)
target_link_libraries(${exercise} PRIVATE Catch2::Catch2WithMain)
# When Catch is installed system wide we need to include a different
# header, we need this define to use the correct one.
target_compile_definitions(${exercise} PRIVATE EXERCISM_TEST_SUITE)
else()
# Build executable from sources and headers
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h test/tests-main.cpp)
endif()

set_target_properties(${exercise} PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED OFF
CXX_EXTENSIONS OFF
)

set(CMAKE_BUILD_TYPE Debug)

if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(GNU|Clang)")
set_target_properties(${exercise} PROPERTIES
COMPILE_FLAGS "-Wall -Wextra -Wpedantic -Werror"
)
endif()

# Configure to run all the tests?
if(${EXERCISM_RUN_ALL_TESTS})
target_compile_definitions(${exercise} PRIVATE EXERCISM_RUN_ALL_TESTS)
endif()

# Tell MSVC not to warn us about unchecked iterators in debug builds
if(${MSVC})
set_target_properties(${exercise} PROPERTIES
COMPILE_DEFINITIONS_DEBUG _SCL_SECURE_NO_WARNINGS)
endif()

# Run the tests on every build
add_custom_target(test_${exercise} ALL DEPENDS ${exercise} COMMAND ${exercise})
5 changes: 5 additions & 0 deletions exercises/practice/dnd-character/dnd_character.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "dnd_character.h"

namespace dnd_character {

} // namespace dnd_character
5 changes: 5 additions & 0 deletions exercises/practice/dnd-character/dnd_character.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

namespace dnd_character {

} // namespace dnd_character
101 changes: 101 additions & 0 deletions exercises/practice/dnd-character/dnd_character_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include "dnd_character.h"
#ifdef EXERCISM_TEST_SUITE
#include <catch2/catch.hpp>
#else
#include "test/catch.hpp"
#endif

TEST_CASE("ability modifier for score 3 is -4", "[1e9ae1dc-35bd-43ba-aa08-e4b94c20fa37]") {
REQUIRE(-4 == dnd_character::modifier(3));
}

#if defined(EXERCISM_RUN_ALL_TESTS)

TEST_CASE("ability modifier for score 4 is -3", "[cc9bb24e-56b8-4e9e-989d-a0d1a29ebb9c]") {
REQUIRE(-3 == dnd_character::modifier(4));
}

TEST_CASE("ability modifier for score 5 is -3", "[5b519fcd-6946-41ee-91fe-34b4f9808326]") {
REQUIRE(-3 == dnd_character::modifier(5));
}

TEST_CASE("ability modifier for score 6 is -2", "[dc2913bd-6d7a-402e-b1e2-6d568b1cbe21]") {
REQUIRE(-2 == dnd_character::modifier(6));
}

TEST_CASE("ability modifier for score 7 is -2", "[099440f5-0d66-4b1a-8a10-8f3a03cc499f]") {
REQUIRE(-2 == dnd_character::modifier(7));
}

TEST_CASE("ability modifier for score 8 is -1", "[cfda6e5c-3489-42f0-b22b-4acb47084df0]") {
REQUIRE(-1 == dnd_character::modifier(8));
}

TEST_CASE("ability modifier for score 9 is -1", "[c70f0507-fa7e-4228-8463-858bfbba1754]") {
REQUIRE(-1 == dnd_character::modifier(9));
}

TEST_CASE("ability modifier for score 10 is 0", "[6f4e6c88-1cd9-46a0-92b8-db4a99b372f7]") {
REQUIRE(0 == dnd_character::modifier(10));
}

TEST_CASE("ability modifier for score 11 is 0", "[e00d9e5c-63c8-413f-879d-cd9be9697097]") {
REQUIRE(0 == dnd_character::modifier(11));
}

TEST_CASE("ability modifier for score 12 is +1", "[eea06f3c-8de0-45e7-9d9d-b8cab4179715]") {
REQUIRE(1 == dnd_character::modifier(12));
}

TEST_CASE("ability modifier for score 13 is +1", "[9c51f6be-db72-4af7-92ac-b293a02c0dcd]") {
REQUIRE(1 == dnd_character::modifier(13));
}

TEST_CASE("ability modifier for score 14 is +2", "[94053a5d-53b6-4efc-b669-a8b5098f7762]") {
REQUIRE(2 == dnd_character::modifier(14));
}

TEST_CASE("ability modifier for score 15 is +2", "[8c33e7ca-3f9f-4820-8ab3-65f2c9e2f0e2]") {
REQUIRE(2 == dnd_character::modifier(15));
}

TEST_CASE("ability modifier for score 16 is +3", "[c3ec871e-1791-44d0-b3cc-77e5fb4cd33d]") {
REQUIRE(3 == dnd_character::modifier(16));
}

TEST_CASE("ability modifier for score 17 is +3", "[3d053cee-2888-4616-b9fd-602a3b1efff4]") {
REQUIRE(3 == dnd_character::modifier(17));
}

TEST_CASE("ability modifier for score 18 is +4", "[bafd997a-e852-4e56-9f65-14b60261faee]") {
REQUIRE(4 == dnd_character::modifier(18));
}

TEST_CASE("random ability is within range", "[4f28f19c-2e47-4453-a46a-c0d365259c14]") {
int result{dnd_character::ability()};
//REQUIRE("score >= 3 && score <= 18" == dnd_character::ability());
REQUIRE((result >= 3 && result <= 18));
}

TEST_CASE("random character is valid", "[385d7e72-864f-4e88-8279-81a7d75b04ad]") {
dnd_character::Character character;
REQUIRE((character.strength >= 3 && character.strength <= 18));
REQUIRE((character.dexterity >= 3 && character.dexterity <= 18));
REQUIRE((character.constitution >= 3 && character.constitution <= 18));
REQUIRE((character.intelligence >= 3 && character.intelligence <= 18));
REQUIRE((character.wisdom >= 3 && character.wisdom <= 18));
REQUIRE((character.charisma >= 3 && character.charisma <= 18));
REQUIRE(character.hitpoints == 10 + dnd_character::modifier(character.constitution));
}

TEST_CASE("each ability is only calculated once", "[dca2b2ec-f729-4551-84b9-078876bb4808]") {
dnd_character::Character character;
REQUIRE(character.strength == character.strength);
REQUIRE(character.dexterity == character.dexterity);
REQUIRE(character.constitution == character.constitution);
REQUIRE(character.intelligence == character.intelligence);
REQUIRE(character.wisdom == character.wisdom);
REQUIRE(character.charisma == character.charisma);
}

#endif
Loading