Skip to content

Method: setAnswer(AnswerOptions)

1: package de.fhdw.gaming.ipspiel23.freizeitgestaltung.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.ipspiel23.freizeitgestaltung.domain.FzgPlayer;
11: import de.fhdw.gaming.ipspiel23.freizeitgestaltung.move.AnswerOptions;
12:
13: /**
14: * Implements {@link FzgPlayer}.
15: *
16: */
17: public class FzgPlayerImpl extends AbstractPlayer<FzgPlayer> implements FzgPlayer {
18:
19: /**
20: * answer of the Player.
21: */
22: private Optional<AnswerOptions> answer;
23:
24: /**
25: * The possible outcomes of this player. The key for the first-level map is the answer of the first player, the key
26: * for the second-level map is the answer of the second player.
27: */
28: private final Map<AnswerOptions, Map<AnswerOptions, Double>> possibleOutcomes;
29:
30: /**
31: * Constructor.
32: *
33: * @param name Name of the Player.
34: * @param possibleOutcomes The possible outcomes of this player. The key for the first-level map is the answer of
35: * the first player, the key for the second-level map is the answer of the second player.
36: */
37: protected FzgPlayerImpl(final String name, final Map<AnswerOptions, Map<AnswerOptions, Double>> possibleOutcomes) {
38: super(name);
39: this.possibleOutcomes = Collections
40: .unmodifiableMap(new LinkedHashMap<>(Objects.requireNonNull(possibleOutcomes, "possibleOutcomes")));
41: this.answer = Optional.empty();
42: }
43:
44: /**
45: * Constructor for DeepCopy purpose.
46: *
47: * @param source the player to copy.
48: */
49: public FzgPlayerImpl(final FzgPlayer 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("FzgPlayer[name=%s, state=%s, outcome=%s, answer=%s]",
59: this.getName(),
60: this.getState(),
61: this.getOutcome(),
62: this.answer);
63: }
64:
65: @Override
66: public boolean equals(final Object obj) {
67: if (obj instanceof FzgPlayerImpl) {
68: final FzgPlayerImpl other = (FzgPlayerImpl) obj;
69: return super.equals(obj) && this.answer.equals(other.answer)
70: && this.possibleOutcomes.equals(other.possibleOutcomes);
71: }
72: return false;
73: }
74:
75: @Override
76: public int hashCode() {
77: return super.hashCode() ^ Objects.hash(this.answer, this.possibleOutcomes);
78: }
79:
80: @Override
81: public FzgPlayer deepCopy() {
82: return new FzgPlayerImpl(this);
83: }
84:
85: @Override
86: public void setAnswer(final AnswerOptions value) {
87:• if (this.answer.isPresent()) {
88: throw new IllegalStateException(String.format("Player %s tried to change her answer.", this.getName()));
89: }
90: this.answer = Optional.of(value);
91: }
92:
93: @Override
94: public Map<AnswerOptions, Map<AnswerOptions, Double>> getPossibleOutcomes() {
95: return possibleOutcomes;
96: }
97:
98: @Override
99: public Optional<AnswerOptions> getAnswer() {
100: return this.answer;
101: }
102: }