Skip to content

Method: DemoPlayerImpl(String, Map)

1: /*
2: * Copyright © 2021-2023 Fachhochschule für die Wirtschaft (FHDW) Hannover
3: *
4: * This file is part of ipspiel23-demo.
5: *
6: * Ipspiel23-demo is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
7: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
8: * version.
9: *
10: * Ipspiel23-demo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
11: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12: *
13: * You should have received a copy of the GNU General Public License along with ipspiel23-demo. If not, see
14: * <http://www.gnu.org/licenses/>.
15: */
16: package de.fhdw.gaming.ipspiel23.demo.domain.impl;
17:
18: import java.util.Collections;
19: import java.util.LinkedHashMap;
20: import java.util.Map;
21: import java.util.Objects;
22: import java.util.Optional;
23:
24: import de.fhdw.gaming.core.domain.AbstractPlayer;
25: import de.fhdw.gaming.ipspiel23.demo.domain.DemoPlayer;
26:
27: /**
28: * Implements {@link DemoPlayer}.
29: */
30: final class DemoPlayerImpl extends AbstractPlayer<DemoPlayer> implements DemoPlayer {
31:
32: /**
33: * The possible outcomes of this player. The key for the first-level map is the answer of the first player, the key
34: * for the second-level map is the answer of the second player.
35: */
36: private final Map<Boolean, Map<Boolean, Double>> possibleOutcomes;
37: /**
38: * The answer of the player.
39: */
40: private Optional<Boolean> answer;
41:
42: /**
43: * Creates a Demo player.
44: *
45: * @param name The name of the player.
46: * @param possibleOutcomes The possible outcomes of this player. The key for the first-level map is the answer of
47: * the first player, the key for the second-level map is the answer of the second player.
48: */
49: DemoPlayerImpl(final String name,
50: final Map<Boolean, Map<Boolean, Double>> possibleOutcomes) {
51: super(name);
52: this.possibleOutcomes = Collections
53: .unmodifiableMap(new LinkedHashMap<>(Objects.requireNonNull(possibleOutcomes, "possibleOutcomes")));
54: this.answer = Optional.empty();
55: }
56:
57: /**
58: * Creates a Demo player.
59: *
60: * @param source The {@link DemoPlayer} to copy.
61: */
62: DemoPlayerImpl(final DemoPlayer source) {
63: super(source);
64: this.possibleOutcomes = source.getPossibleOutcomes();
65: this.answer = source.getAnswer();
66: }
67:
68: @Override
69: public String toString() {
70: return String.format(
71: "DemoPlayer[name=%s, state=%s, outcome=%s, answer=%s]",
72: this.getName(),
73: this.getState(),
74: this.getOutcome(),
75: this.answer);
76: }
77:
78: @Override
79: public boolean equals(final Object obj) {
80: if (obj instanceof DemoPlayerImpl) {
81: final DemoPlayerImpl other = (DemoPlayerImpl) obj;
82: return super.equals(obj) && this.answer.equals(other.answer)
83: && this.possibleOutcomes.equals(other.possibleOutcomes);
84: }
85: return false;
86: }
87:
88: @SuppressWarnings("PMD.UselessOverridingMethod")
89: @Override
90: public int hashCode() {
91: return super.hashCode() ^ Objects.hash(this.answer, this.possibleOutcomes);
92: }
93:
94: @Override
95: public Map<Boolean, Map<Boolean, Double>> getPossibleOutcomes() {
96: return this.possibleOutcomes;
97: }
98:
99: @Override
100: public Optional<Boolean> getAnswer() {
101: return this.answer;
102: }
103:
104: @Override
105: public void setAnswer(final boolean newAnswer) {
106: if (this.answer.isPresent()) {
107: throw new IllegalStateException(String.format("Player %s tried to change her answer.", this.getName()));
108: }
109: this.answer = Optional.of(newAnswer);
110: }
111:
112: @Override
113: public DemoPlayer deepCopy() {
114: return new DemoPlayerImpl(this);
115: }
116: }