Skip to content

Method: checkPossibleOutcomes(Map)

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