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