Skip to content

Method: outcomeToState(Double)

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