Skip to content

Method: toString()

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