Skip to content

Method: equals(Object)

1: package de.fhdw.gaming.ipspiel22.kopfundzahl.domain.impl;
2:
3: import java.util.LinkedHashMap;
4: import java.util.LinkedHashSet;
5: import java.util.Map;
6: import java.util.Objects;
7: import java.util.Set;
8:
9: import de.fhdw.gaming.core.domain.GameException;
10: import de.fhdw.gaming.core.domain.PlayerState;
11: import de.fhdw.gaming.ipspiel22.kopfundzahl.domain.KopfundZahlPlayer;
12: import de.fhdw.gaming.ipspiel22.kopfundzahl.domain.KopfundZahlState;
13:
14: /**
15: * Implements {@link KopfundZahlState}.
16: */
17: public final class KopfundZahlStateImpl implements KopfundZahlState {
18:
19: /**
20: * The first player.
21: */
22: private final KopfundZahlPlayer firstPlayer;
23:
24: /**
25: * The second player.
26: */
27: private final KopfundZahlPlayer secondPlayer;
28:
29: /**
30: * Creates KopfundZahl state.
31: *
32: * @param firstPlayer The first player.
33: * @param secondPlayer The second player.
34: * @throws GameException if the state cannot be created according to the rules of the game.
35: */
36: public KopfundZahlStateImpl(final KopfundZahlPlayer firstPlayer, final KopfundZahlPlayer secondPlayer)
37: throws GameException {
38:
39: this.firstPlayer = Objects.requireNonNull(firstPlayer, "firstPlayer");
40: this.secondPlayer = Objects.requireNonNull(secondPlayer, "secondPlayer");
41:
42: if (this.firstPlayer.getName().equals(this.secondPlayer.getName())) {
43: throw new IllegalArgumentException(
44: String.format("Both players have the same name '%s'.", this.firstPlayer.getName()));
45: }
46: }
47:
48: /**
49: * Creates a KopfundZahl state by copying an existing one.
50: *
51: * @param source The state to copy.
52: */
53: KopfundZahlStateImpl(final KopfundZahlStateImpl source) {
54: this.firstPlayer = source.firstPlayer.deepCopy();
55: this.secondPlayer = source.secondPlayer.deepCopy();
56: }
57:
58: /**
59: * Returns the first player.
60: */
61: @Override
62: public KopfundZahlPlayer getFirstPlayer() {
63: return this.firstPlayer;
64: }
65:
66: /**
67: * Return the second player.
68: */
69: @Override
70: public KopfundZahlPlayer getSecondPlayer() {
71: return this.secondPlayer;
72: }
73:
74: @Override
75: public String toString() {
76: return String.format("KopfundZahlState[firstPlayer=%s, secondPlayer=%s]", this.firstPlayer, this.secondPlayer);
77: }
78:
79: @Override
80: public boolean equals(final Object obj) {
81:• if (obj instanceof KopfundZahlStateImpl) {
82: final KopfundZahlStateImpl other = (KopfundZahlStateImpl) obj;
83:• return this.firstPlayer.equals(other.firstPlayer) && this.secondPlayer.equals(other.secondPlayer);
84: }
85: return false;
86: }
87:
88: @Override
89: public int hashCode() {
90: return Objects.hash(this.firstPlayer, this.secondPlayer);
91: }
92:
93: @Override
94: public KopfundZahlState deepCopy() {
95: return new KopfundZahlStateImpl(this);
96: }
97:
98: @Override
99: public Map<String, KopfundZahlPlayer> getPlayers() {
100: final Map<String, KopfundZahlPlayer> result = new LinkedHashMap<>();
101: result.put(this.firstPlayer.getName(), this.firstPlayer);
102: result.put(this.secondPlayer.getName(), this.secondPlayer);
103: return result;
104: }
105:
106: @Override
107: public Set<KopfundZahlPlayer> computeNextPlayers() {
108: final Set<KopfundZahlPlayer> playersWithoutMove = new LinkedHashSet<>();
109: if (this.firstPlayer.getAnswer().isEmpty()) {
110: playersWithoutMove.add(this.firstPlayer);
111: }
112: if (this.secondPlayer.getAnswer().isEmpty()) {
113: playersWithoutMove.add(this.secondPlayer);
114: }
115: return playersWithoutMove;
116: }
117:
118: @Override
119: public void nextTurn() {
120: final Set<KopfundZahlPlayer> playersWithoutMove = this.computeNextPlayers();
121: if (playersWithoutMove.isEmpty()) {
122: final Boolean answerOfFirstPlayer = this.firstPlayer.getAnswer().orElseThrow();
123: final Boolean answerOfSecondPlayer = this.secondPlayer.getAnswer().orElseThrow();
124:
125: final Double outcomeOfFirstPlayer = this.firstPlayer.getPossibleOutcomes().get(answerOfFirstPlayer)
126: .get(answerOfSecondPlayer);
127: this.firstPlayer.setState(outcomeToState(outcomeOfFirstPlayer));
128: this.firstPlayer.setOutcome(outcomeOfFirstPlayer);
129:
130: final Double outcomeOfSecondPlayer = this.secondPlayer.getPossibleOutcomes().get(answerOfFirstPlayer)
131: .get(answerOfSecondPlayer);
132: this.secondPlayer.setState(outcomeToState(outcomeOfSecondPlayer));
133: this.secondPlayer.setOutcome(outcomeOfSecondPlayer);
134: }
135: }
136:
137: /**
138: * Computes a player state from an outcome.
139: *
140: * @param outcome The player's outcome.
141: */
142: private static PlayerState outcomeToState(final Double outcome) {
143: return outcome > 0.0 ? PlayerState.WON : PlayerState.LOST;
144: }
145: }