Skip to content

Commit 4f96cba

Browse files
authored
Fix dnd character issues (exercism#2218)
1 parent 5986867 commit 4f96cba

3 files changed

Lines changed: 99 additions & 34 deletions

File tree

exercises/practice/dnd-character/.meta/src/reference/java/DnDCharacter.java

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
import java.util.Arrays;
1+
import java.util.Collections;
2+
import java.util.List;
23
import java.util.Random;
4+
import java.util.stream.Collectors;
35

46
class DnDCharacter {
57

8+
private static final int NUMBER_OF_DICE = 4;
9+
610
private int strength;
711
private int dexterity;
812
private int constitution;
@@ -12,22 +16,26 @@ class DnDCharacter {
1216
private int hitpoints;
1317

1418
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();
19+
this.strength = ability(rollDice());
20+
this.dexterity = ability(rollDice());
21+
this.constitution = ability(rollDice());
22+
this.intelligence = ability(rollDice());
23+
this.wisdom = ability(rollDice());
24+
this.charisma = ability(rollDice());
2125
this.hitpoints = 10 + modifier(this.constitution);
2226
}
2327

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;
28+
int ability(List<Integer> scores) {
29+
return scores.stream().sorted(Collections.reverseOrder())
30+
.limit(3)
31+
.mapToInt(nr -> nr).sum();
32+
}
33+
34+
List<Integer> rollDice() {
35+
return new Random()
36+
.ints(NUMBER_OF_DICE, 1, 7)
37+
.boxed()
38+
.collect(Collectors.toList());
3139
}
3240

3341
int modifier(int constitution) {
@@ -61,16 +69,4 @@ int getCharisma() {
6169
int getHitpoints() {
6270
return hitpoints;
6371
}
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-
7672
}

exercises/practice/dnd-character/src/main/java/DnDCharacter.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
import java.util.List;
2+
13
class DnDCharacter {
24

3-
int ability() {
5+
int ability(List<Integer> scores) {
6+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
7+
}
8+
9+
List<Integer> rollDice() {
410
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
511
}
612

@@ -35,5 +41,4 @@ int getCharisma() {
3541
int getHitpoints() {
3642
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
3743
}
38-
3944
}

exercises/practice/dnd-character/src/test/java/DnDCharacterTest.java

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import org.junit.Ignore;
22
import org.junit.Test;
33

4-
import static org.junit.Assert.*;
4+
import java.util.List;
5+
6+
import static org.junit.Assert.assertEquals;
7+
import static org.junit.Assert.assertTrue;
58

69
public class DnDCharacterTest {
710

@@ -104,9 +107,54 @@ public void testAbilityModifierForScore18Is4() {
104107

105108
@Ignore("Remove to run test")
106109
@Test
107-
public void testRandomAbilityIsWithinRange() {
108-
int score = dndCharacter.ability();
109-
assertTrue(score > 2 && score < 19);
110+
public void test4DiceWereUsedForRollingScores() {
111+
assertEquals(4, dndCharacter.rollDice().size());
112+
}
113+
114+
@Ignore("Remove to run test")
115+
@Test
116+
public void testDiceValuesBetween1And6() {
117+
assertTrue(dndCharacter.rollDice().stream().allMatch(d -> d >= 1 && d <= 6));
118+
}
119+
120+
@Ignore("Remove to run test")
121+
@Test
122+
public void testAbilityCalculationsUses3LargestNumbersFromScoresInDescendingOrder() {
123+
assertEquals(9, dndCharacter.ability(List.of(4, 3, 2, 1)));
124+
}
125+
126+
@Ignore("Remove to run test")
127+
@Test
128+
public void testAbilityCalculationsUses3LargestNumbersFromFromScoresInAscendingOrder() {
129+
assertEquals(9, dndCharacter.ability(List.of(1, 2, 3, 4)));
130+
}
131+
132+
@Ignore("Remove to run test")
133+
@Test
134+
public void testAbilityCalculationsUses3LargestNumbersFromScoresInRandomOrder() {
135+
assertEquals(9, dndCharacter.ability(List.of(2, 4, 3, 1)));
136+
}
137+
138+
@Ignore("Remove to run test")
139+
@Test
140+
public void testAbilityCalculationsWithLowestEqualNumbers() {
141+
assertEquals(3, dndCharacter.ability(List.of(1, 1, 1, 1)));
142+
}
143+
144+
@Ignore("Remove to run test")
145+
@Test
146+
public void testAbilityCalculationsWithHighestEqualNumbers() {
147+
assertEquals(18, dndCharacter.ability(List.of(6, 6, 6, 6)));
148+
}
149+
150+
@Ignore("Remove to run test")
151+
@Test
152+
public void testAbilityCalculationDoesNotChangeInputScores() {
153+
List<Integer> scores = List.of(1, 2, 3, 4);
154+
dndCharacter.ability(scores);
155+
156+
assertEquals(4, scores.size());
157+
assertEquals(scores, List.of(1, 2, 3, 4));
110158
}
111159

112160
@Ignore("Remove to run test")
@@ -120,8 +168,7 @@ public void testRandomCharacterIsValid() {
120168
assertTrue(character.getIntelligence() > 2 && character.getIntelligence() < 19);
121169
assertTrue(character.getWisdom() > 2 && character.getWisdom() < 19);
122170
assertTrue(character.getCharisma() > 2 && character.getCharisma() < 19);
123-
assertEquals(character.getHitpoints(),
124-
10 + character.modifier(character.getConstitution()));
171+
assertEquals(10 + character.modifier(character.getConstitution()), character.getHitpoints());
125172
}
126173
}
127174

@@ -131,4 +178,21 @@ public void testEachAbilityIsOnlyCalculatedOnce() {
131178
assertEquals(dndCharacter.getStrength(), dndCharacter.getStrength());
132179
}
133180

181+
@Ignore("Remove to run test")
182+
@Test
183+
public void testUniqueCharacterIsCreated() {
184+
DnDCharacter uniqueDnDCharacter = new DnDCharacter();
185+
for (int i = 0; i < 1000; i++) {
186+
DnDCharacter dnDCharacter = new DnDCharacter();
187+
boolean dnDCharactersHaveDifferentAttributes =
188+
dnDCharacter.getStrength() != uniqueDnDCharacter.getStrength()
189+
|| dnDCharacter.getDexterity() != uniqueDnDCharacter.getDexterity()
190+
|| dnDCharacter.getConstitution() != uniqueDnDCharacter.getConstitution()
191+
|| dnDCharacter.getIntelligence() != uniqueDnDCharacter.getIntelligence()
192+
|| dnDCharacter.getWisdom() != uniqueDnDCharacter.getWisdom()
193+
|| dnDCharacter.getCharisma() != uniqueDnDCharacter.getCharisma()
194+
|| dnDCharacter.getHitpoints() != uniqueDnDCharacter.getHitpoints();
195+
assertTrue(dnDCharactersHaveDifferentAttributes);
196+
}
197+
}
134198
}

0 commit comments

Comments
 (0)