Skip to content

Method: setState(PlayerState)

1: /*
2: * Copyright © 2021-2022 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.Objects;
22: import java.util.Optional;
23:
24: /**
25: * A foundation block for implementing players.
26: *
27: * @param <P> The type of allowed players.
28: */
29: public abstract class AbstractPlayer<P extends Player<P>> implements Player<P> {
30:
31: /**
32: * The name of this player.
33: */
34: private final String name;
35: /**
36: * The state of the player.
37: */
38: private PlayerState state;
39: /**
40: * The outcome of the player.
41: */
42: private Optional<Double> outcome;
43:
44: /**
45: * Creates a player.
46: *
47: * @param name The name of the player.
48: */
49: protected AbstractPlayer(final String name) {
50: this.name = Objects.requireNonNull(name, "name");
51: this.state = PlayerState.PLAYING;
52: this.outcome = Optional.empty();
53: }
54:
55: /**
56: * Creates a Demo player.
57: *
58: * @param source The {@link Player} to copy.
59: */
60: protected AbstractPlayer(final P source) {
61: this.name = source.getName();
62: this.state = source.getState();
63: this.outcome = source.getOutcome();
64: }
65:
66: @Override
67: public boolean equals(final Object obj) {
68: if (obj instanceof AbstractPlayer) {
69: final AbstractPlayer<?> other = (AbstractPlayer<?>) obj;
70: return this.name.equals(other.name) && this.state.equals(other.state)
71: && this.outcome.equals(other.outcome);
72: }
73: return false;
74: }
75:
76: @Override
77: public int hashCode() {
78: return this.name.hashCode();
79: }
80:
81: @Override
82: public final String getName() {
83: return this.name;
84: }
85:
86: @Override
87: public final PlayerState getState() {
88: return this.state;
89: }
90:
91: @Override
92: public final void setState(final PlayerState newState) {
93: this.state = newState;
94:• if (newState.equals(PlayerState.PLAYING)) {
95: this.outcome = Optional.empty();
96: }
97: }
98:
99: @Override
100: public final Optional<Double> getOutcome() {
101: return this.outcome.isPresent() ? this.outcome : mapStateToOutcome(this.state);
102: }
103:
104: @Override
105: public final void setOutcome(final double newOutcome) {
106: if (this.state.equals(PlayerState.PLAYING)) {
107: throw new IllegalStateException(String.format("Cannot set outcome for player %s.", this.name));
108: }
109: this.outcome = Optional.of(newOutcome);
110: }
111:
112: /**
113: * Maps passed player state to the player's outcome. The outcome is unavailable
114: * if the state is {@link PlayerState#PLAYING}. Otherwise, the outcome is computed from the player
115: * state as follows:
116: * <ul>
117: * <li>1.0 if the state is {@link PlayerState#WON}</li>
118: * <li>0.0 if the state is {@link PlayerState#DRAW}</li>
119: * <li>-1.0 if the state is {@link PlayerState#LOST} or
120: * {@link PlayerState#RESIGNED}</li>
121: * </ul>
122: *
123: * @param state The player state.
124: */
125: public static Optional<Double> mapStateToOutcome(final PlayerState state) {
126: switch (state) {
127: case WON:
128: return Optional.of(1.0);
129: case LOST:
130: case RESIGNED:
131: return Optional.of(-1.0);
132: case DRAW:
133: return Optional.of(0.0);
134: case PLAYING:
135: default:
136: return Optional.empty();
137: }
138: }
139: }