Skip to content

Method: build()

1: /*
2: * Copyright © 2021-2023 Fachhochschule für die Wirtschaft (FHDW) Hannover
3: *
4: * This file is part of ipspiel24-Ssp.
5: *
6: * Ipspiel24-Ssp 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-Ssp 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-Ssp. If not, see
14: * <http://www.gnu.org/licenses/>.
15: */
16: package de.fhdw.gaming.ipspiel24.ssp.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.ssp.domain.SspPlayer;
24: import de.fhdw.gaming.ipspiel24.ssp.domain.SspPlayerBuilder;
25: import de.fhdw.gaming.ipspiel24.ssp.domain.SspPossibleChoice;
26:
27: /**
28: * Implements {@link SspPlayerBuilder}.
29: */
30: final class SspPlayerBuilderImpl implements SspPlayerBuilder {
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<SspPossibleChoice, Map<SspPossibleChoice, Double>>> possibleOutcomes;
41:
42: /**
43: * Creates an {@link SspPlayerBuilderImpl}.
44: */
45: SspPlayerBuilderImpl() {
46: this.name = Optional.empty();
47: this.possibleOutcomes = Optional.empty();
48: }
49:
50: @Override
51: public SspPlayerBuilderImpl changeName(final String newName) {
52: this.name = Optional.of(newName);
53: return this;
54: }
55:
56: @Override
57: public SspPlayerBuilder changePossibleOutcomes(
58: final Map<SspPossibleChoice, Map<SspPossibleChoice, Double>> newPossibleOutcomes) {
59: this.possibleOutcomes = Optional.of(newPossibleOutcomes);
60: return this;
61: }
62:
63: @Override
64: public SspPlayer build() throws GameException {
65: return new SspPlayerImpl(
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<SspPossibleChoice, Map<SspPossibleChoice, Double>> checkPossibleOutcomes(
76: final Map<SspPossibleChoice, Map<SspPossibleChoice, Double>> outcomes) {
77: this.checkPossibleOutcome(outcomes, SspPossibleChoice.SPC_ROCK, SspPossibleChoice.SPC_ROCK);
78: this.checkPossibleOutcome(outcomes, SspPossibleChoice.SPC_ROCK, SspPossibleChoice.SPC_PAPER);
79: this.checkPossibleOutcome(outcomes, SspPossibleChoice.SPC_ROCK, SspPossibleChoice.SPC_SCISSORS);
80: this.checkPossibleOutcome(outcomes, SspPossibleChoice.SPC_PAPER, SspPossibleChoice.SPC_PAPER);
81: this.checkPossibleOutcome(outcomes, SspPossibleChoice.SPC_PAPER, SspPossibleChoice.SPC_SCISSORS);
82: this.checkPossibleOutcome(outcomes, SspPossibleChoice.SPC_PAPER, SspPossibleChoice.SPC_ROCK);
83: this.checkPossibleOutcome(outcomes, SspPossibleChoice.SPC_SCISSORS, SspPossibleChoice.SPC_SCISSORS);
84: this.checkPossibleOutcome(outcomes, SspPossibleChoice.SPC_SCISSORS, SspPossibleChoice.SPC_ROCK);
85: this.checkPossibleOutcome(outcomes, SspPossibleChoice.SPC_SCISSORS, SspPossibleChoice.SPC_PAPER);
86: return outcomes;
87: }
88:
89: /**
90: * Checks if a given outcome is defined for a player.
91: *
92: * @param outcomes The possible outcomes for the player.
93: * @param firstChoice The choice of the first player.
94: * @param secondChoice The choice of the second player.
95: */
96: private void checkPossibleOutcome(final Map<SspPossibleChoice, Map<SspPossibleChoice, Double>> outcomes,
97: final SspPossibleChoice firstChoice,
98: final SspPossibleChoice secondChoice) {
99: if (outcomes.getOrDefault(firstChoice, Collections.emptyMap()).get(secondChoice) == null) {
100: throw new IllegalArgumentException(
101: String.format(
102: "No outcome defined for player '%s' and combination %s/%s.",
103: this.name,
104: toAnswer(firstChoice),
105: toAnswer(secondChoice)));
106: }
107: }
108:
109: /**
110: * Maps a boolean value to a "yes" or "no" answer.
111: *
112: * @param value The value to be mapped.
113: */
114: private static String toAnswer(final SspPossibleChoice value) {
115: switch (value) {
116: case SPC_ROCK:
117: return "Rock";
118: case SPC_PAPER:
119: return "Paper";
120: case SPC_SCISSORS:
121: return "Scissors";
122: default:
123: return "default";
124: }
125: }
126: }