Skip to content

Method: lambda$nextTurn$1(IHTPlayer)

1: package de.fhdw.gaming.ipspiel23.ht.domain.impl;
2:
3: import java.util.ArrayList;
4: import java.util.LinkedHashMap;
5: import java.util.List;
6: import java.util.Map;
7: import java.util.Objects;
8: import java.util.Set;
9: import java.util.stream.Collectors;
10:
11: import de.fhdw.gaming.core.domain.PlayerState;
12: import de.fhdw.gaming.ipspiel23.ht.domain.Answer;
13: import de.fhdw.gaming.ipspiel23.ht.domain.IHTPlayer;
14: import de.fhdw.gaming.ipspiel23.ht.domain.IHTState;
15:
16: /**
17: * Represents the state of a Heads or Tails game.
18: */
19: final class HTState implements IHTState {
20:
21: /**
22: * Represents the first player.
23: */
24: private final IHTPlayer playerOne;
25:
26: /**
27: * Represents the second player.
28: */
29: private final IHTPlayer playerTwo;
30:
31: /**
32: * A map which maps the names of the players to the players.
33: */
34: private Map<String, IHTPlayer> playerMap;
35:
36: /**
37: * A list of the players in the order in which they are added to the game.
38: */
39: private final List<IHTPlayer> orderedPlayers;
40:
41: /**
42: * Creates a new {@link HTState} which is a deep copy of the given {@link HTState}.
43: *
44: * @param state The {@link HTState} to copy.
45: */
46: HTState(final HTState state) {
47: this.playerOne = state.getPlayerOne().deepCopy();
48: this.playerTwo = state.getPlayerTwo().deepCopy();
49: this.orderedPlayers = new ArrayList<>();
50: this.orderedPlayers.add(playerOne);
51: this.orderedPlayers.add(playerTwo);
52: }
53:
54: /**
55: * Creates a new {@link HTState} with the given players.
56: *
57: * @param player1 The first player.
58: * @param player2 The second player.
59: * @throws IllegalArgumentException if the names of the players are identical.
60: */
61: public HTState(final IHTPlayer player1, final IHTPlayer player2) {
62: this.playerOne = Objects.requireNonNull(player1, "player1");
63: this.playerTwo = Objects.requireNonNull(player2, "player2");
64:
65: if (this.playerOne.getName().equalsIgnoreCase(this.playerTwo.getName())) {
66: throw new IllegalArgumentException("Player names cannot be identical :P");
67: }
68: this.orderedPlayers = new ArrayList<>();
69: this.orderedPlayers.add(this.playerOne);
70: this.orderedPlayers.add(this.playerTwo);
71: }
72:
73: /**
74: * Returns the first player.
75: */
76: public IHTPlayer getPlayerOne() {
77: return this.playerOne;
78: }
79:
80: /**
81: * Returns the second player.
82: */
83: public IHTPlayer getPlayerTwo() {
84: return this.playerTwo;
85: }
86:
87: @Override
88: public boolean equals(final Object obj) {
89: if (obj instanceof HTState) {
90: final HTState other = (HTState) obj;
91: return this.playerOne.equals(other.playerOne) && this.playerTwo.equals(other.playerTwo);
92: }
93: return false;
94: }
95:
96: @Override
97: public int hashCode() {
98: return Objects.hash(this.playerOne, this.playerTwo);
99: }
100:
101: @Override
102: public Map<String, IHTPlayer> getPlayers() {
103: if (this.playerMap == null) {
104: this.playerMap = new LinkedHashMap<>();
105: this.playerMap.put(this.playerOne.getName(), this.playerOne);
106: this.playerMap.put(this.playerTwo.getName(), this.playerTwo);
107: }
108: return playerMap;
109: }
110:
111: @Override
112: public Set<IHTPlayer> computeNextPlayers() {
113: return getPlayers().values().stream()
114: .filter(player -> player.getAnswer().isEmpty())
115: .collect(Collectors.toSet());
116: }
117:
118: @Override
119: public void nextTurn() {
120: if (computeNextPlayers().isEmpty()) {
121: final List<Answer> answers = this.orderedPlayers.stream()
122: .map(player -> player.getAnswer().orElseThrow())
123: .collect(Collectors.toList());
124: this.orderedPlayers.stream()
125: .forEach(player -> applyOutcome(player, answers));
126: }
127: }
128:
129: /**
130: * Applies the outcome of the game to the given player.
131: *
132: * @param player The player.
133: * @param answers The answers of the players.
134: */
135: private static void applyOutcome(final IHTPlayer player, final List<Answer> answers) {
136: final Double outcome = player.getPossibleOutcomes().get(answers.get(0)).get(answers.get(1));
137: player.setState(outcomeToState(outcome));
138: player.setOutcome(outcome);
139: }
140:
141: @Override
142: public IHTState deepCopy() {
143: return new HTState(this);
144: }
145:
146: /**
147: * Converts an outcome to a {@link PlayerState}.
148: * @param outcome the 64-bit double precision floating point number to convert.
149: * @return the new player state (DRAW if outcome == 0.0, WON if outcome > 0.0, LOST otherwise)
150: */
151: static PlayerState outcomeToState(final Double outcome) {
152: return outcome > 0.0
153: ? PlayerState.WON
154: : outcome < 0.0
155: ? PlayerState.LOST
156: : PlayerState.DRAW;
157: }
158:
159: @Override
160: public IHTPlayer getFirstPlayer() {
161: return this.playerOne;
162: }
163:
164: @Override
165: public IHTPlayer getSecondPlayer() {
166: return this.playerTwo;
167: }
168:
169: @Override
170: public String toString() {
171: return String.format("%s[firstPlayer=%s, secondPlayer=%s]", this.getClass().getSimpleName(),
172: this.playerOne, this.playerTwo);
173: }
174: }