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 ipspiel24-GD.
5: *
6: * ipspiel24-GD 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-GD 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-GD. If not, see
14: * <http://www.gnu.org/licenses/>.
15: */
16: package de.fhdw.gaming.GefangenenDilemma.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.GefangenenDilemma.domain.GDPlayer;
25: import de.fhdw.gaming.GefangenenDilemma.domain.GDState;
26: import de.fhdw.gaming.core.domain.GameException;
27: import de.fhdw.gaming.core.domain.PlayerState;
28:
29: /**
30: * Implements {@link GDState}.
31: */
32: final class GDStateImpl implements GDState {
33:
34: /**
35: * The first player.
36: */
37: private final GDPlayer firstPlayer;
38: /**
39: * The second player.
40: */
41: private final GDPlayer secondPlayer;
42:
43: /**
44: * Creates a Gefangenen-Dilemma 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: GDStateImpl(final GDPlayer firstPlayer, final GDPlayer 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 Gefangenen-Dilemma state by copying an existing one.
64: *
65: * @param source The state to copy.
66: */
67: GDStateImpl(final GDStateImpl 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 GDPlayer getFirstPlayer() {
77: return this.firstPlayer;
78: }
79:
80: /**
81: * Returns the second player.
82: */
83: @Override
84: public GDPlayer getSecondPlayer() {
85: return this.secondPlayer;
86: }
87:
88: @Override
89: public String toString() {
90: return String.format("GDState[firstPlayer=%s, secondPlayer=%s]", this.firstPlayer, this.secondPlayer);
91: }
92:
93: @Override
94: public boolean equals(final Object obj) {
95: if (obj instanceof GDStateImpl) {
96: final GDStateImpl other = (GDStateImpl) obj;
97: return this.firstPlayer.equals(other.firstPlayer) && this.secondPlayer.equals(other.secondPlayer);
98: }
99: return false;
100: }
101:
102: @Override
103: public GDState deepCopy() {
104: return new GDStateImpl(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, GDPlayer> getPlayers() {
114: final Map<String, GDPlayer> 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<GDPlayer> computeNextPlayers() {
122: final Set<GDPlayer> 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<GDPlayer> playersWithoutMove = this.computeNextPlayers();
135: if (playersWithoutMove.isEmpty()) {
136: final GDGameBuilderFactoryImpl.MOVES answerOfFirstPlayer = this.firstPlayer.getAnswer()
137: .orElseThrow();
138: final GDGameBuilderFactoryImpl.MOVES answerOfSecondPlayer = this.secondPlayer.getAnswer()
139: .orElseThrow();
140:
141: final Double outcomeOfFirstPlayer = this.firstPlayer.getPossibleOutcomes().get(answerOfFirstPlayer)
142: .get(answerOfSecondPlayer);
143: this.firstPlayer.setState(outcomeToState(outcomeOfFirstPlayer));
144: this.firstPlayer.setOutcome(outcomeOfFirstPlayer);
145:
146: final Double outcomeOfSecondPlayer = this.secondPlayer.getPossibleOutcomes().get(answerOfSecondPlayer)
147: .get(answerOfFirstPlayer);
148: this.secondPlayer.setState(outcomeToState(outcomeOfSecondPlayer));
149: this.secondPlayer.setOutcome(outcomeOfSecondPlayer);
150: }
151: }
152:
153: /**
154: * Computes a player state from an outcome.
155: *
156: * @param outcome The player's outcome.
157: */
158: private static PlayerState outcomeToState(final Double outcome) {
159:• return outcome >= 0.0 ? PlayerState.WON : outcome < 0.0 ? PlayerState.LOST : PlayerState.DRAW;
160: }
161: }