Skip to content

Method: checkPossibleOutcome(Map, AnswerOptions, AnswerOptions)

1: package de.fhdw.gaming.ipspiel23.freizeitgestaltung.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.freizeitgestaltung.domain.FzgPlayer;
9: import de.fhdw.gaming.ipspiel23.freizeitgestaltung.domain.FzgPlayerBuilder;
10: import de.fhdw.gaming.ipspiel23.freizeitgestaltung.move.AnswerOptions;
11:
12: /**
13: * implements {@link FzgPlayerBuilder}.
14: *
15: */
16: public class FzgPlayerBuilderImpl implements FzgPlayerBuilder {
17:
18: /**
19: * The name of the player.
20: */
21: private Optional<String> name;
22: /**
23: * The possible outcomes of this player. The key for the first-level map is the answer of the first player, the key
24: * for the second-level map is the answer of the second player.
25: */
26: private Optional<Map<AnswerOptions, Map<AnswerOptions, Double>>> possibleOutcomes;
27:
28: /**
29: * Constructor.
30: */
31: public FzgPlayerBuilderImpl() {
32: this.name = Optional.empty();
33: this.possibleOutcomes = Optional.empty();
34: }
35:
36: @Override
37: public FzgPlayerBuilder changeName(final String newName) {
38: this.name = Optional.of(newName);
39: return this;
40: }
41:
42: @Override
43: public FzgPlayerBuilder changePossibleOutcomes(
44: final Map<AnswerOptions, Map<AnswerOptions, Double>> newPossibleOutcomes) {
45: this.possibleOutcomes = Optional.of(newPossibleOutcomes);
46: return this;
47: }
48:
49: @Override
50: public FzgPlayer build() throws GameException {
51: return new FzgPlayerImpl(this.name.orElseThrow(), checkPossibleOutcomes(this.possibleOutcomes.orElseThrow()));
52: }
53:
54: /**
55: * checks if all possible outcomes are defined for the player.
56: *
57: * @param outcomes The possible outcomes for the player.
58: */
59: private Map<AnswerOptions, Map<AnswerOptions, Double>> checkPossibleOutcomes(
60: final Map<AnswerOptions, Map<AnswerOptions, Double>> outcomes) {
61: this.checkPossibleOutcome(outcomes, AnswerOptions.CINEMA, AnswerOptions.CINEMA);
62: this.checkPossibleOutcome(outcomes, AnswerOptions.CINEMA, AnswerOptions.FOOTBALL);
63: this.checkPossibleOutcome(outcomes, AnswerOptions.FOOTBALL, AnswerOptions.CINEMA);
64: this.checkPossibleOutcome(outcomes, AnswerOptions.FOOTBALL, AnswerOptions.FOOTBALL);
65: return outcomes;
66: }
67:
68: /**
69: * Checks if a given outcome is defined for a player.
70: *
71: * @param outcomes The possible outcomes for the player.
72: * @param firstChoice The choice of the first player.
73: * @param secondChoice The choice of the second player.
74: */
75: private void checkPossibleOutcome(final Map<AnswerOptions, Map<AnswerOptions, Double>> outcomes,
76: final AnswerOptions firstChoice,
77: final AnswerOptions secondChoice) {
78:• if (outcomes.getOrDefault(firstChoice, Collections.emptyMap()).get(secondChoice) == null) {
79: throw new IllegalArgumentException(
80: String.format(
81: "No outcome defined for player '%s' and combination %s/%s.",
82: this.name.get(),
83: firstChoice,
84: secondChoice));
85: }
86: }
87: }