Skip to content

Method: equals(Object)

1: /*
2: * Copyright © 2021-2023 Fachhochschule für die Wirtschaft (FHDW) Hannover
3: *
4: * This file is part of ipspiel24-demo.
5: *
6: * Ipspiel24-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: * Ipspiel24-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 ipspiel24-demo. If not, see
14: * <http://www.gnu.org/licenses/>.
15: */
16: package de.fhdw.gaming.ipspiel24.muenzwurf.core.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.ipspiel24.muenzwurf.core.domain.MuenzwurfPlayer;
26: import de.fhdw.gaming.ipspiel24.muenzwurf.core.domain.MuenzwurfSide;
27:
28: /**
29: * Implements {@link MuenzwurfPlayer}.
30: */
31: final class MuenzwurfPlayerImpl extends AbstractPlayer<MuenzwurfPlayer> implements MuenzwurfPlayer {
32:
33: /**
34: * The possible outcomes of this player. The key for the first-level map is the answer of the first player, the key
35: * for the second-level map is the answer of the second player.
36: */
37: private final Map<MuenzwurfSide, Map<MuenzwurfSide, Double>> possibleOutcomes;
38: /**
39: * The answer of the player.
40: */
41: private Optional<MuenzwurfSide> answer;
42:
43: /**
44: * Decides whether the player gets points for heads/heads;tails/tails or
45: * heads/tails;tails/heads. True for the former.
46: */
47: private Boolean pointsForSame;
48:
49: /**
50: * Creates a Muenzwurf player.
51: *
52: * @param name The name of the player.
53: * @param possibleOutcomes The possible outcomes of this player. The key for the first-level map is the answer of
54: * the first player, the key for the second-level map is the answer of the second player.
55: */
56: MuenzwurfPlayerImpl(final String name,
57: final Map<MuenzwurfSide, Map<MuenzwurfSide, Double>> possibleOutcomes) {
58: super(name);
59: this.possibleOutcomes = Collections
60: .unmodifiableMap(new LinkedHashMap<>(Objects.requireNonNull(possibleOutcomes, "possibleOutcomes")));
61: this.answer = Optional.empty();
62: }
63:
64: /**
65: * Creates a Muenzwurf player.
66: *
67: * @param source The {@link DemoPlayer} to copy.
68: */
69: MuenzwurfPlayerImpl(final MuenzwurfPlayer source) {
70: super(source);
71: this.possibleOutcomes = source.getPossibleOutcomes();
72: this.answer = source.getSide();
73: }
74:
75: /**
76: * Gets pointsForSame.
77: * @return The boolean currently save for pointsForSame.
78: */
79: @Override
80: public Boolean getPointsForSame() {
81: return this.pointsForSame;
82: }
83:
84: /**
85: * Sets pointsForSame.
86: * @param toSet The boolean to set pointsForSame to.
87: */
88: @Override
89: public void setPointsForSame(final Boolean toSet) {
90: this.pointsForSame = toSet;
91: }
92:
93: @Override
94: public String toString() {
95: return String
96: .format("MuenzwurfPlayer[name=%s, state=%s, outcome=%s, answer=%s]", this.getName(), this.getState(),
97: this.getOutcome(),
98: this.answer);
99: }
100:
101: @Override
102: public boolean equals(final Object obj) {
103:• if (obj instanceof MuenzwurfPlayerImpl) {
104: final MuenzwurfPlayerImpl other = (MuenzwurfPlayerImpl) obj;
105:• return super.equals(obj) && this.answer.equals(other.answer)
106:• && this.possibleOutcomes.equals(other.possibleOutcomes);
107: }
108: return false;
109: }
110:
111: @SuppressWarnings("PMD.UselessOverridingMethod")
112: @Override
113: public int hashCode() {
114: return super.hashCode() ^ Objects.hash(this.answer, this.possibleOutcomes);
115: }
116:
117: @Override
118: public Map<MuenzwurfSide, Map<MuenzwurfSide, Double>> getPossibleOutcomes() {
119: return this.possibleOutcomes;
120: }
121:
122: @Override
123: public Optional<MuenzwurfSide> getSide() {
124: return this.answer;
125: }
126:
127: @Override
128: public void chooseSide(final MuenzwurfSide side) throws IllegalStateException {
129: if (this.answer.isPresent()) {
130: throw new IllegalStateException(String.format("Player %s tried to change her answer.", this.getName()));
131: }
132: this.answer = Optional.of(side);
133: }
134:
135: @Override
136: public MuenzwurfPlayer deepCopy() {
137: return new MuenzwurfPlayerImpl(this);
138: }
139: }