Skip to content

Commit f1625b3

Browse files
lemoncurrySmarticles101
authored andcommitted
dnd-character: add new exercise (exercism#1621)
* wip dnd-character: add new exercise * update config.json * create reference DnDCharacter.java * add version file * add build.gradle * add starter file DnDCharacter.java * add test file * update settings.gradle * create README.md
1 parent 70ccdcc commit f1625b3

8 files changed

Lines changed: 304 additions & 0 deletions

File tree

‎config.json‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@
7474
"strings"
7575
]
7676
},
77+
{
78+
"slug": "dnd-character",
79+
"uuid": "09bb515c-0270-4d34-8d56-89ee04588494",
80+
"core": false,
81+
"unlocked_by": "two-fer",
82+
"difficulty": 2,
83+
"topics": [
84+
"game",
85+
"integers",
86+
"randomness"
87+
]
88+
},
7789
{
7890
"slug": "pangram",
7991
"uuid": "133b0f84-bdc7-4508-a0a1-5732a7db81ef",
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import java.util.Arrays;
2+
import java.util.Random;
3+
4+
class DnDCharacter {
5+
6+
private int strength;
7+
private int dexterity;
8+
private int constitution;
9+
private int intelligence;
10+
private int wisdom;
11+
private int charisma;
12+
private int hitpoints;
13+
14+
DnDCharacter() {
15+
this.strength = ability();
16+
this.dexterity = ability();
17+
this.constitution = ability();
18+
this.intelligence = ability();
19+
this.wisdom = ability();
20+
this.charisma = ability();
21+
this.hitpoints = 10 + modifier(this.constitution);
22+
}
23+
24+
int ability() {
25+
int[] scores = rollDices();
26+
int scoreSum = 0;
27+
for (int i = 1; i < 4; i++) {
28+
scoreSum += scores[i];
29+
}
30+
return scoreSum;
31+
}
32+
33+
int modifier(int constitution) {
34+
return Math.floorDiv(constitution - 10, 2);
35+
}
36+
37+
int getStrength() {
38+
return strength;
39+
}
40+
41+
int getDexterity() {
42+
return dexterity;
43+
}
44+
45+
int getConstitution() {
46+
return constitution;
47+
}
48+
49+
int getIntelligence() {
50+
return intelligence;
51+
}
52+
53+
int getWisdom() {
54+
return wisdom;
55+
}
56+
57+
int getCharisma() {
58+
return charisma;
59+
}
60+
61+
int getHitpoints() {
62+
return hitpoints;
63+
}
64+
65+
private int[] rollDices() {
66+
int[] scoreSum = new int[4];
67+
Random random = new Random();
68+
for (int i = 0; i < 4; i++) {
69+
int score = random.nextInt(6) + 1;
70+
scoreSum[i] = score;
71+
}
72+
Arrays.sort(scoreSum);
73+
return scoreSum;
74+
}
75+
76+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.1.0

‎exercises/dnd-character/README.md‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Dnd Character
2+
3+
For a game of [Dungeons & Dragons][DND], each player starts by generating a
4+
character they can play with. This character has, among other things, six
5+
abilities; strength, dexterity, constitution, intelligence, wisdom and
6+
charisma. These six abilities have scores that are determined randomly. You
7+
do this by rolling four 6-sided dice and record the sum of the largest three
8+
dice. You do this six times, once for each ability.
9+
10+
Your character's initial hitpoints are 10 + your character's constitution
11+
modifier. You find your character's constitution modifier by subtracting 10
12+
from your character's constitution, divide by 2 and round down.
13+
14+
Write a random character generator that follows the rules above.
15+
16+
For example, the six throws of four dice may look like:
17+
18+
* 5, 3, 1, 6: You discard the 1 and sum 5 + 3 + 6 = 14, which you assign to strength.
19+
* 3, 2, 5, 3: You discard the 2 and sum 3 + 5 + 3 = 11, which you assign to dexterity.
20+
* 1, 1, 1, 1: You discard the 1 and sum 1 + 1 + 1 = 3, which you assign to constitution.
21+
* 2, 1, 6, 6: You discard the 1 and sum 2 + 6 + 6 = 14, which you assign to intelligence.
22+
* 3, 5, 3, 4: You discard the 3 and sum 5 + 3 + 4 = 12, which you assign to wisdom.
23+
* 6, 6, 6, 6: You discard the 6 and sum 6 + 6 + 6 = 18, which you assign to charisma.
24+
25+
Because constitution is 3, the constitution modifier is -4 and the hitpoints are 6.
26+
27+
## Notes
28+
29+
Most programming languages feature (pseudo-)random generators, but few
30+
programming languages are designed to roll dice. One such language is [Troll].
31+
32+
[DND]: https://en.wikipedia.org/wiki/Dungeons_%26_Dragons
33+
[Troll]: http://hjemmesider.diku.dk/~torbenm/Troll/
34+
35+
# Running the tests
36+
37+
You can run all the tests for an exercise by entering
38+
39+
```sh
40+
$ gradle test
41+
```
42+
43+
in your terminal.
44+
45+
## Source
46+
47+
Simon Shine, Erik Schierboom [https://github.com/exercism/problem-specifications/issues/616#issuecomment-437358945](https://github.com/exercism/problem-specifications/issues/616#issuecomment-437358945)
48+
49+
## Submitting Incomplete Solutions
50+
51+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testCompile "junit:junit:4.12"
11+
}
12+
13+
test {
14+
testLogging {
15+
exceptionFormat = 'full'
16+
events = ["passed", "failed", "skipped"]
17+
}
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class DnDCharacter {
2+
3+
int ability() {
4+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
5+
}
6+
7+
int modifier(int input) {
8+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
9+
}
10+
11+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import org.junit.Ignore;
2+
import org.junit.Test;
3+
4+
import static org.junit.Assert.*;
5+
6+
public class DnDCharacterTest {
7+
8+
private DnDCharacter dndCharacter = new DnDCharacter();
9+
10+
@Test
11+
public void testAbilityModifierForScore3IsNegative4() {
12+
assertEquals(-4, dndCharacter.modifier(3));
13+
}
14+
15+
@Ignore("Remove to run test")
16+
@Test
17+
public void testAbilityModifierForScore4IsNegative3() {
18+
assertEquals(-3, dndCharacter.modifier(4));
19+
}
20+
21+
@Ignore("Remove to run test")
22+
@Test
23+
public void testAbilityModifierForScore5IsNegative3() {
24+
assertEquals(-3, dndCharacter.modifier(5));
25+
}
26+
27+
@Ignore("Remove to run test")
28+
@Test
29+
public void testAbilityModifierForScore6IsNegative2() {
30+
assertEquals(-2, dndCharacter.modifier(6));
31+
}
32+
33+
@Ignore("Remove to run test")
34+
@Test
35+
public void testAbilityModifierForScore7IsNegative2() {
36+
assertEquals(-2, dndCharacter.modifier(7));
37+
}
38+
39+
@Ignore("Remove to run test")
40+
@Test
41+
public void testAbilityModifierForScore8IsNegative1() {
42+
assertEquals(-1, dndCharacter.modifier(8));
43+
}
44+
45+
@Ignore("Remove to run test")
46+
@Test
47+
public void testAbilityModifierForScore9IsNegative1() {
48+
assertEquals(-1, dndCharacter.modifier(9));
49+
}
50+
51+
@Ignore("Remove to run test")
52+
@Test
53+
public void testAbilityModifierForScore10Is0() {
54+
assertEquals(0, dndCharacter.modifier(10));
55+
}
56+
57+
@Ignore("Remove to run test")
58+
@Test
59+
public void testAbilityModifierForScore11Is0() {
60+
assertEquals(0, dndCharacter.modifier(11));
61+
}
62+
63+
@Ignore("Remove to run test")
64+
@Test
65+
public void testAbilityModifierForScore12Is1() {
66+
assertEquals(1, dndCharacter.modifier(12));
67+
}
68+
69+
@Ignore("Remove to run test")
70+
@Test
71+
public void testAbilityModifierForScore13Is1() {
72+
assertEquals(1, dndCharacter.modifier(13));
73+
}
74+
75+
@Ignore("Remove to run test")
76+
@Test
77+
public void testAbilityModifierForScore14Is2() {
78+
assertEquals(2, dndCharacter.modifier(14));
79+
}
80+
81+
@Ignore("Remove to run test")
82+
@Test
83+
public void testAbilityModifierForScore15Is2() {
84+
assertEquals(2, dndCharacter.modifier(15));
85+
}
86+
87+
@Ignore("Remove to run test")
88+
@Test
89+
public void testAbilityModifierForScore16Is3() {
90+
assertEquals(3, dndCharacter.modifier(16));
91+
}
92+
93+
@Ignore("Remove to run test")
94+
@Test
95+
public void testAbilityModifierForScore17Is3() {
96+
assertEquals(3, dndCharacter.modifier(17));
97+
}
98+
99+
@Ignore("Remove to run test")
100+
@Test
101+
public void testAbilityModifierForScore18Is4() {
102+
assertEquals(4, dndCharacter.modifier(18));
103+
}
104+
105+
@Ignore("Remove to run test")
106+
@Test
107+
public void testRandomAbilityIsWithinRange() {
108+
int score = dndCharacter.ability();
109+
assertTrue(score > 2 && score < 19);
110+
}
111+
112+
@Ignore("Remove to run test")
113+
@Test
114+
public void testRandomCharacterIsValid() {
115+
for (int i = 0; i < 1000; i++) {
116+
DnDCharacter character = new DnDCharacter();
117+
assertTrue(character.getStrength() > 2 && character.getStrength() < 19);
118+
assertTrue(character.getDexterity() > 2 && character.getDexterity() < 19);
119+
assertTrue(character.getConstitution() > 2 && character.getConstitution() < 19);
120+
assertTrue(character.getIntelligence() > 2 && character.getIntelligence() < 19);
121+
assertTrue(character.getWisdom() > 2 && character.getWisdom() < 19);
122+
assertTrue(character.getCharisma() > 2 && character.getCharisma() < 19);
123+
assertEquals(character.getHitpoints(),
124+
10 + character.modifier(character.getConstitution()));
125+
}
126+
}
127+
128+
@Ignore("Remove to run test")
129+
@Test
130+
public void testEachAbilityIsOnlyCalculatedOnce() {
131+
assertEquals(dndCharacter.getStrength(), dndCharacter.getStrength());
132+
}
133+
134+
}

‎exercises/settings.gradle‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ include 'darts'
2727
include 'diamond'
2828
include 'difference-of-squares'
2929
include 'diffie-hellman'
30+
include 'dnd-character'
3031
include 'dominoes'
3132
include 'error-handling'
3233
include 'etl'

0 commit comments

Comments
 (0)