Skip to content

Package: HTPlayerBuilder

HTPlayerBuilder

nameinstructionbranchcomplexitylinemethod
HTPlayerBuilder()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
build()
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
changeName(String)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
changePossibleOutcomes(Map)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
checkPossibleOutcome(Map, Answer, Answer)
M: 0 C: 30
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
checkPossibleOutcomes(Map)
M: 0 C: 47
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%

Coverage

1: package de.fhdw.gaming.ipspiel23.ht.domain.impl;
2:
3: import java.util.Collections;
4: import java.util.Map;
5: import java.util.Optional;
6:
7: import de.fhdw.gaming.core.domain.GameException;
8: import de.fhdw.gaming.ipspiel23.ht.domain.Answer;
9: import de.fhdw.gaming.ipspiel23.ht.domain.IHTPlayer;
10: import de.fhdw.gaming.ipspiel23.ht.domain.IHTPlayerBuilder;
11:
12: /**
13: * A builder which implements the {@link IHTPlayerBuilder} and allows to create an {@link IHTPlayer}.
14: */
15: final class HTPlayerBuilder implements IHTPlayerBuilder {
16:
17: /**
18: * The name of the player.
19: */
20: private Optional<String> name;
21:
22: /**
23: * The possible outcomes of the player.
24: */
25: private Optional<Map<Answer, Map<Answer, Double>>> possibleOutcomes;
26:
27: /**
28: * Change the name of the player.
29: * <p>
30: * There is no default.
31: *
32: * @param newName The new name of the player.
33: * @return {@code this}
34: */
35: @Override
36: public IHTPlayerBuilder changeName(final String newName) {
37: name = Optional.of(newName);
38: return this;
39: }
40:
41: /**
42: * Changes the possible outcomes of the player.
43: * <p>
44: * There is no default.
45: *
46: * @param newOutcomes The possible outcomes of the player. The key for the first-level map is the answer of the
47: * first player, the key for the second-level map is the answer of the second player.
48: */
49: @Override
50: public IHTPlayerBuilder changePossibleOutcomes(final Map<Answer, Map<Answer, Double>> newOutcomes) {
51: this.possibleOutcomes = Optional.of(newOutcomes);
52: return this;
53: }
54:
55: /**
56: * Builds the player.
57: *
58: * @return The HT player.
59: * @throws GameException if creating the player is not allowed by the rules of the game.
60: */
61: @Override
62: public IHTPlayer build() throws GameException {
63: return new HTPlayer(
64: this.name.orElseThrow(),
65: checkPossibleOutcomes(this.possibleOutcomes.orElseThrow()));
66: }
67:
68: /**
69: * Checks if all possible outcomes are defined for a player.
70: *
71: * @param outcomes The possible outcomes for the player.
72: */
73: private Map<Answer, Map<Answer, Double>> checkPossibleOutcomes(
74: final Map<Answer, Map<Answer, Double>> outcomes) {
75: this.checkPossibleOutcome(outcomes, Answer.HEADS, Answer.HEADS);
76: this.checkPossibleOutcome(outcomes, Answer.HEADS, Answer.TAILS);
77: this.checkPossibleOutcome(outcomes, Answer.HEADS, Answer.EDGE);
78: this.checkPossibleOutcome(outcomes, Answer.TAILS, Answer.HEADS);
79: this.checkPossibleOutcome(outcomes, Answer.TAILS, Answer.TAILS);
80: this.checkPossibleOutcome(outcomes, Answer.TAILS, Answer.EDGE);
81: this.checkPossibleOutcome(outcomes, Answer.EDGE, Answer.HEADS);
82: this.checkPossibleOutcome(outcomes, Answer.EDGE, Answer.TAILS);
83: this.checkPossibleOutcome(outcomes, Answer.EDGE, Answer.EDGE);
84: return outcomes;
85: }
86:
87: /**
88: * Checks if a given outcome is defined for a player.
89: *
90: * @param outcomes The possible outcomes for the player.
91: * @param firstChoice The choice of the first player.
92: * @param secondChoice The choice of the second player.
93: */
94: private void checkPossibleOutcome(final Map<Answer, Map<Answer, Double>> outcomes, final Answer firstChoice,
95: final Answer secondChoice) {
96:• if (outcomes.getOrDefault(firstChoice, Collections.emptyMap()).get(secondChoice) == null) {
97: throw new IllegalArgumentException(
98: String.format(
99: "No outcome defined for player '%s' and combination %s/%s.",
100: name,
101: firstChoice,
102: secondChoice));
103: }
104: }
105: }