Skip to contentPackage: State
State
Coverage
1: /*
2: * Copyright © 2020 Fachhochschule für die Wirtschaft (FHDW) Hannover
3: *
4: * This file is part of gaming-core.
5: *
6: * Gaming-core is free software: you can redistribute it and/or modify it under
7: * the terms of the GNU General Public License as published by the Free Software
8: * Foundation, either version 3 of the License, or (at your option) any later
9: * version.
10: *
11: * Gaming-core is distributed in the hope that it will be useful, but WITHOUT
12: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14: * details.
15: *
16: * You should have received a copy of the GNU General Public License along with
17: * gaming-core. If not, see <http://www.gnu.org/licenses/>.
18: */
19: package de.fhdw.gaming.core.domain;
20:
21: import java.util.Map;
22: import java.util.Optional;
23: import java.util.Set;
24:
25: /**
26: * Represents the state of a game.
27: * <p>
28: * Note that CRTP (Curiously Recurring Template Pattern) is used to retain the subtype of the state. This allows clients
29: * to clone a state via {@link #deepCopy()} while retaining its type at compile-time.
30: *
31: * @param <P> The type of allowed players.
32: * @param <S> The type of game states.
33: */
34: public interface State<P extends Player, S extends State<P, S>> extends Stateful {
35:
36: /**
37: * Returns all the players of the game.
38: */
39: Map<String, P> getPlayers();
40:
41: /**
42: * Returns the state of a player.
43: *
44: * @param playerName The name of the player.
45: * @throws IllegalArgumentException if the player is unknown.
46: */
47: PlayerState getPlayerState(String playerName) throws IllegalArgumentException;
48:
49: /**
50: * Changes the state of a player. If the new state is {@link PlayerState#PLAYING}, any outcome previously set by
51: * calling {@link #setPlayerOutcome(String, double)} is removed.
52: *
53: * @param playerName The name of the player.
54: * @param newState The new state.
55: *
56: * @throws IllegalArgumentException if the player is unknown.
57: */
58: void setPlayerState(String playerName, PlayerState newState) throws IllegalArgumentException;
59:
60: /**
61: * Returns a player's outcome (if available). The outcome is unavailable if the player is still in state
62: * {@link PlayerState#PLAYING}. Otherwise, if no outcome has been set by {@link #setPlayerOutcome(String, double)},
63: * the outcome is computed from the player's state:
64: * <ul>
65: * <li>1.0 if the new state is {@link PlayerState#WON}</li>
66: * <li>0.0 if the new state is {@link PlayerState#DRAW}</li>
67: * <li>-1.0 if the new state is {@link PlayerState#LOST} or {@link PlayerState#RESIGNED}</li>
68: * </ul>
69: *
70: * @param playerName The name of the player.
71: * @throws IllegalArgumentException if the player is unknown.
72: */
73: default Optional<Double> getPlayerOutcome(final String playerName) throws IllegalArgumentException {
74:• switch (this.getPlayerState(playerName)) {
75: case WON:
76: return Optional.of(1.0);
77: case LOST:
78: case RESIGNED:
79: return Optional.of(-1.0);
80: case DRAW:
81: return Optional.of(0.0);
82: case PLAYING:
83: default:
84: return Optional.empty();
85: }
86: }
87:
88: /**
89: * Sets a player's outcome. Requires that the player's state is not {@link PlayerState#PLAYING}.
90: *
91: * @param playerName The name of the player.
92: * @param newOutcome The new outcome.
93: */
94: void setPlayerOutcome(String playerName, double newOutcome) throws IllegalArgumentException;
95:
96: /**
97: * Computes the set of players being able to do the next move.
98: *
99: * @return A set of players that can act next (possibly in parallel). Can be empty if no players have remained.
100: */
101: Set<P> computeNextPlayers();
102:
103: /**
104: * This operation is called after each move and also if no valid move has been found for the set of currently active
105: * players.
106: */
107: void nextTurn();
108:
109: @Override
110: S deepCopy();
111: }