Skip to content

Method: getSecondPlayer()

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