Skip to content

Method: getPossibleOutcomes()

1: package de.fhdw.gaming.ipspiel22.kopfundzahlundkante.domain.impl;
2:
3: import java.util.Collections;
4: import java.util.LinkedHashMap;
5: import java.util.Map;
6: import java.util.Objects;
7: import java.util.Optional;
8:
9: import de.fhdw.gaming.core.domain.AbstractPlayer;
10: import de.fhdw.gaming.ipspiel22.kopfundzahlundkante.domain.KopfundZahlundKanteAnswerEnum;
11: import de.fhdw.gaming.ipspiel22.kopfundzahlundkante.domain.KopfundZahlundKantePlayer;
12:
13: /**
14: * Implements {@link KopfundZahlundKantePlayer}.
15: */
16: public final class KopfundZahlundKantePlayerImpl extends AbstractPlayer<KopfundZahlundKantePlayer>
17: implements KopfundZahlundKantePlayer {
18:
19: /**
20: * The possible outcomes of this player. The key for the first-level map is the answer of the first player, the key
21: * for the second-level map is the answer of the second player.
22: */
23: private final Map<KopfundZahlundKanteAnswerEnum, Map<KopfundZahlundKanteAnswerEnum, Double>> possibleOutcomes;
24: /**
25: * The answer of the player.
26: */
27: private Optional<KopfundZahlundKanteAnswerEnum> answer;
28:
29: /**
30: * Creates a KopfundZahl player.
31: *
32: * @param name The name of the player.
33: * @param possibleOutcomes The possible outcomes of this player. The key for the first-level map is the answer of
34: * the first player, the key for the second-level map is the answer of the second player.
35: */
36: public KopfundZahlundKantePlayerImpl(final String name,
37: final Map<KopfundZahlundKanteAnswerEnum, Map<KopfundZahlundKanteAnswerEnum, Double>> possibleOutcomes) {
38: super(name);
39: this.possibleOutcomes = Collections.unmodifiableMap(
40: new LinkedHashMap<>(Objects.requireNonNull(possibleOutcomes, "possibleOutcomes")));
41: this.answer = Optional.empty();
42: }
43:
44: /**
45: * Creates a KopfundZahl player.
46: *
47: * @param source The {@link KopfundZahlundKantePlayer} to copy.
48: */
49: KopfundZahlundKantePlayerImpl(final KopfundZahlundKantePlayer source) {
50: super(source);
51: this.possibleOutcomes = source.getPossibleOutcomes();
52: this.answer = source.getAnswer();
53: }
54:
55: @Override
56: public String toString() {
57: return String
58: .format("KopfundZahlundKantePlayer[name=%s, state=%s, outcome=%s, answer=%s]", this.getName(),
59: this.getState(), this.getOutcome(), this.answer);
60: }
61:
62: @Override
63: public boolean equals(final Object obj) {
64: if (obj instanceof KopfundZahlundKantePlayerImpl) {
65: final KopfundZahlundKantePlayerImpl other = (KopfundZahlundKantePlayerImpl) obj;
66: return super.equals(obj) && this.answer.equals(other.answer);
67: }
68: return false;
69: }
70:
71: @SuppressWarnings("PMD.UselessOverriding")
72: @Override
73: public int hashCode() {
74: return super.hashCode();
75: }
76:
77: @Override
78: public Map<KopfundZahlundKanteAnswerEnum, Map<KopfundZahlundKanteAnswerEnum, Double>> getPossibleOutcomes() {
79: return this.possibleOutcomes;
80: }
81:
82: @Override
83: public Optional<KopfundZahlundKanteAnswerEnum> getAnswer() {
84: return this.answer;
85: }
86:
87: @Override
88: public void setAnswer(final KopfundZahlundKanteAnswerEnum newAnswer) {
89: if (this.answer.isPresent()) {
90: throw new IllegalStateException(String.format("Player %s tried to change her answer.", this.getName()));
91: }
92: this.answer = Optional.of(newAnswer);
93: }
94:
95: @Override
96: public KopfundZahlundKantePlayer deepCopy() {
97: return new KopfundZahlundKantePlayerImpl(this);
98: }
99: }