Skip to content

Method: changeName(String)

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.Collections;
19: import java.util.Map;
20: import java.util.Optional;
21:
22: import de.fhdw.gaming.GefangenenDilemma.domain.GDPlayer;
23: import de.fhdw.gaming.GefangenenDilemma.domain.GDPlayerBuilder;
24: import de.fhdw.gaming.core.domain.GameException;
25:
26: /**
27: * Implements {@link GDPlayerBuilder}.
28: */
29: final class GDPlayerBuilderImpl implements GDPlayerBuilder {
30:
31: /**
32: * The name of the player.
33: */
34: private Optional<String> name;
35: /**
36: * The possible outcomes of this player. The key for the first-level map is the answer of the first player, the key
37: * for the second-level map is the answer of the second player.
38: */
39: private Optional<Map<GDGameBuilderFactoryImpl.MOVES,
40: Map<GDGameBuilderFactoryImpl.MOVES, Double>>> possibleOutcomes;
41:
42: /**
43: * Creates an {@link GDPlayerBuilderImpl}.
44: */
45: GDPlayerBuilderImpl() {
46: this.name = Optional.empty();
47: this.possibleOutcomes = Optional.empty();
48: }
49:
50: @Override
51: public GDPlayerBuilderImpl changeName(final String newName) {
52: this.name = Optional.of(newName);
53: return this;
54: }
55:
56: @Override
57: public GDPlayerBuilder changePossibleOutcomes(
58: final Map<GDGameBuilderFactoryImpl.MOVES,
59: Map<GDGameBuilderFactoryImpl.MOVES, Double>> newPossibleOutcomes) {
60: this.possibleOutcomes = Optional.of(newPossibleOutcomes);
61: return this;
62: }
63:
64: @Override
65: public GDPlayer build() throws GameException {
66: return new GDPlayerImpl(
67: this.name.orElseThrow(),
68: this.checkPossibleOutcomes(this.possibleOutcomes.orElseThrow()));
69: }
70:
71: /**
72: * Checks if all possible outcomes are defined for a player.
73: *
74: * @param outcomes The possible outcomes for the player.
75: */
76: private Map<GDGameBuilderFactoryImpl.MOVES,
77: Map<GDGameBuilderFactoryImpl.MOVES, Double>> checkPossibleOutcomes(
78: final Map<GDGameBuilderFactoryImpl.MOVES,
79: Map<GDGameBuilderFactoryImpl.MOVES, Double>> outcomes) {
80: this.checkPossibleOutcome(outcomes, GDGameBuilderFactoryImpl.MOVES.SNITCH,
81: GDGameBuilderFactoryImpl.MOVES.SNITCH);
82: this.checkPossibleOutcome(outcomes, GDGameBuilderFactoryImpl.MOVES.SNITCH,
83: GDGameBuilderFactoryImpl.MOVES.REMAINSILENT);
84: this.checkPossibleOutcome(outcomes, GDGameBuilderFactoryImpl.MOVES.REMAINSILENT,
85: GDGameBuilderFactoryImpl.MOVES.SNITCH);
86: this.checkPossibleOutcome(outcomes, GDGameBuilderFactoryImpl.MOVES.REMAINSILENT,
87: GDGameBuilderFactoryImpl.MOVES.REMAINSILENT);
88: return outcomes;
89: }
90:
91: /**
92: * Checks if a given outcome is defined for a player.
93: *
94: * @param outcomes The possible outcomes for the player.
95: * @param firstChoice The choice of the first player.
96: * @param secondChoice The choice of the second player.
97: */
98: private void checkPossibleOutcome(
99: final Map<GDGameBuilderFactoryImpl.MOVES,
100: Map<GDGameBuilderFactoryImpl.MOVES, Double>> outcomes,
101: final GDGameBuilderFactoryImpl.MOVES firstChoice,
102: final GDGameBuilderFactoryImpl.MOVES secondChoice) {
103: if (outcomes.getOrDefault(firstChoice, Collections.emptyMap()).get(secondChoice) == null) {
104: throw new IllegalArgumentException(
105: String.format(
106: "No outcome defined for player '%s' and combination %s/%s.",
107: this.name,
108: toString(firstChoice),
109: toString(secondChoice)));
110: }
111: }
112:
113: /**
114: * Maps a GefangenenDilemmaGameBuilderFactoryImpl.MOVES value to a "yes" or "no" answer.
115: *
116: * @param value The value to be mapped.
117: */
118: private static String toString(final GDGameBuilderFactoryImpl.MOVES value) {
119: switch (value) {
120: case SNITCH:
121: return "SNITCH";
122: case REMAINSILENT:
123: return "REMAINSILENT";
124: default:
125: return "NaN";
126: }
127: }
128: }