Skip to content

Method: outcomeToState(Double)

1:
2: package de.fhdw.gaming.ipspiel24.fg.domain.impl;
3:
4: import java.util.LinkedHashMap;
5: import java.util.LinkedHashSet;
6: import java.util.Map;
7: import java.util.Objects;
8: import java.util.Set;
9:
10: import de.fhdw.gaming.core.domain.GameException;
11: import de.fhdw.gaming.core.domain.PlayerState;
12: import de.fhdw.gaming.ipspiel24.fg.domain.FGActivity;
13: import de.fhdw.gaming.ipspiel24.fg.domain.FGPlayer;
14: import de.fhdw.gaming.ipspiel24.fg.domain.FGState;
15:
16: /**
17: * Implements {@link FGState}.
18: */
19: final class FGStateImpl implements FGState {
20:
21: /**
22: * The first player.
23: */
24: private final FGPlayer firstPlayer;
25: /**
26: * The second player.
27: */
28: private final FGPlayer secondPlayer;
29:
30: /**
31: * Creates a FG 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: FGStateImpl(final FGPlayer firstPlayer, final FGPlayer secondPlayer)
38: 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 FG state by copying an existing one.
51: *
52: * @param source The state to copy.
53: */
54: FGStateImpl(final FGStateImpl 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 FGPlayer getFirstPlayer() {
64: return this.firstPlayer;
65: }
66:
67: /**
68: * Returns the second player.
69: */
70: @Override
71: public FGPlayer getSecondPlayer() {
72: return this.secondPlayer;
73: }
74:
75: @Override
76: public String toString() {
77: return String.format("FGState[firstPlayer=%s, secondPlayer=%s]", this.firstPlayer, this.secondPlayer);
78: }
79:
80: @Override
81: public boolean equals(final Object obj) {
82: if (obj instanceof FGStateImpl) {
83: final FGStateImpl other = (FGStateImpl) obj;
84: return this.firstPlayer.equals(other.firstPlayer) && this.secondPlayer.equals(other.secondPlayer);
85: }
86: return false;
87: }
88:
89: @Override
90: public FGState deepCopy() {
91: return new FGStateImpl(this);
92: }
93:
94: @Override
95: public int hashCode() {
96: return Objects.hash(this.firstPlayer, this.secondPlayer);
97: }
98:
99: @Override
100: public Map<String, FGPlayer> getPlayers() {
101: final Map<String, FGPlayer> result = new LinkedHashMap<>();
102: result.put(this.firstPlayer.getName(), this.firstPlayer);
103: result.put(this.secondPlayer.getName(), this.secondPlayer);
104: return result;
105: }
106:
107: @Override
108: public Set<FGPlayer> computeNextPlayers() {
109: final Set<FGPlayer> playersWithoutMove = new LinkedHashSet<>();
110: if (this.firstPlayer.getAnswer().isEmpty()) {
111: playersWithoutMove.add(this.firstPlayer);
112: }
113: if (this.secondPlayer.getAnswer().isEmpty()) {
114: playersWithoutMove.add(this.secondPlayer);
115: }
116: return playersWithoutMove;
117: }
118:
119: @Override
120: public void nextTurn() {
121: final Set<FGPlayer> playersWithoutMove = this.computeNextPlayers();
122: if (playersWithoutMove.isEmpty()) {
123: final FGActivity answerOfFirstPlayer = this.firstPlayer.getAnswer().orElseThrow();
124: final FGActivity answerOfSecondPlayer = this.secondPlayer.getAnswer().orElseThrow();
125:
126: final Double outcomeOfFirstPlayer = this.firstPlayer.getPossibleOutcomes().get(answerOfFirstPlayer)
127: .get(answerOfSecondPlayer);
128: this.firstPlayer.setState(outcomeToState(outcomeOfFirstPlayer));
129: this.firstPlayer.setOutcome(outcomeOfFirstPlayer);
130:
131: final Double outcomeOfSecondPlayer = this.secondPlayer.getPossibleOutcomes().get(answerOfFirstPlayer)
132: .get(answerOfSecondPlayer);
133: this.secondPlayer.setState(outcomeToState(outcomeOfSecondPlayer));
134: this.secondPlayer.setOutcome(outcomeOfSecondPlayer);
135: }
136: }
137:
138: /**
139: * Computes a player state from an outcome.
140: *
141: * @param outcome The player's outcome.
142: */
143: private static PlayerState outcomeToState(final Double outcome) {
144:• return outcome > 0.0 ? PlayerState.WON : outcome < 0.0 ? PlayerState.LOST : PlayerState.DRAW;
145: }
146: }