Package: DemoGameBuilderFactoryImpl
DemoGameBuilderFactoryImpl
| name | instruction | branch | complexity | line | method | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| DemoGameBuilderFactoryImpl() | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| DemoGameBuilderFactoryImpl(DemoStrategyFactoryProvider) | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| createGameBuilder(InputProvider) | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| createPlayer(DemoPlayerBuilder, Map) | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| getMaximumNumberOfPlayers() | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| getMinimumNumberOfPlayers() | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| getName() | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| getStrategies() | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| getStrategy(Map) | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| requestPlayerData(InputProvider, String) | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
Coverage
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.ArrayList;
19: import java.util.LinkedHashMap;
20: import java.util.LinkedHashSet;
21: import java.util.List;
22: import java.util.Map;
23: import java.util.Optional;
24: import java.util.Set;
25: import java.util.regex.Pattern;
26: 
27: import de.fhdw.gaming.core.domain.GameBuilder;
28: import de.fhdw.gaming.core.domain.GameBuilderFactory;
29: import de.fhdw.gaming.core.domain.GameException;
30: import de.fhdw.gaming.core.domain.Strategy;
31: import de.fhdw.gaming.core.ui.InputProvider;
32: import de.fhdw.gaming.core.ui.InputProviderException;
33: import de.fhdw.gaming.core.ui.type.validator.MaxValueValidator;
34: import de.fhdw.gaming.core.ui.type.validator.MinValueValidator;
35: import de.fhdw.gaming.core.ui.type.validator.PatternValidator;
36: import de.fhdw.gaming.ipspiel24.demo.domain.DemoGameBuilder;
37: import de.fhdw.gaming.ipspiel24.demo.domain.DemoGameBuilderFactory;
38: import de.fhdw.gaming.ipspiel24.demo.domain.DemoPlayer;
39: import de.fhdw.gaming.ipspiel24.demo.domain.DemoPlayerBuilder;
40: import de.fhdw.gaming.ipspiel24.demo.domain.DemoStrategy;
41: import de.fhdw.gaming.ipspiel24.demo.domain.factory.DemoDefaultStrategyFactoryProvider;
42: import de.fhdw.gaming.ipspiel24.demo.domain.factory.DemoStrategyFactory;
43: import de.fhdw.gaming.ipspiel24.demo.domain.factory.DemoStrategyFactoryProvider;
44: import de.fhdw.gaming.ipspiel24.demo.moves.factory.DemoMoveFactory;
45: import de.fhdw.gaming.ipspiel24.demo.moves.impl.DemoDefaultMoveFactory;
46: 
47: /**
48:  * Implements {@link GameBuilderFactory} by creating a Demo game builder.
49:  */
50: public final class DemoGameBuilderFactoryImpl implements DemoGameBuilderFactory {
51: 
52:     /**
53:      * The number of players.
54:      */
55:     private static final int NUMBER_OF_PLAYERS = 2;
56:     /**
57:      * Smallest allowed maximum computation time per move in seconds.
58:      */
59:     private static final int MIN_MAX_COMPUTATION_TIME_PER_MOVE = 1;
60:     /**
61:      * Largest allowed maximum computation time per move in seconds.
62:      */
63:     private static final int MAX_MAX_COMPUTATION_TIME_PER_MOVE = 3600;
64: 
65:     /**
66:      * All available Demo strategies.
67:      */
68:     private final Set<DemoStrategy> strategies;
69: 
70:     /**
71:      * Creates a Demo game factory. Demo strategies are loaded by using the {@link java.util.ServiceLoader}.
72:      * <p>
73:      * This constructor is meant to be used by the {@link java.util.ServiceLoader}.
74:      */
75:     public DemoGameBuilderFactoryImpl() {
76:         this(new DemoDefaultStrategyFactoryProvider());
77:     }
78: 
79:     /**
80:      * Creates a Demo game factory.
81:      *
82:      * @param strategyFactoryProvider The {@link DemoStrategyFactoryProvider} for loading Demo strategies.
83:      */
84:     DemoGameBuilderFactoryImpl(final DemoStrategyFactoryProvider strategyFactoryProvider) {
85:         final DemoMoveFactory moveFactory = new DemoDefaultMoveFactory();
86: 
87:         final List<DemoStrategyFactory> factories = strategyFactoryProvider.getStrategyFactories();
88:         this.strategies = new LinkedHashSet<>();
89:•        for (final DemoStrategyFactory factory : factories) {
90:             this.strategies.add(factory.create(moveFactory));
91:         }
92:     }
93: 
94:     @Override
95:     public String getName() {
96:         return "Demo";
97:     }
98: 
99:     @Override
100:     public int getMinimumNumberOfPlayers() {
101:         return DemoGameBuilderFactoryImpl.NUMBER_OF_PLAYERS;
102:     }
103: 
104:     @Override
105:     public int getMaximumNumberOfPlayers() {
106:         return DemoGameBuilderFactoryImpl.NUMBER_OF_PLAYERS;
107:     }
108: 
109:     @Override
110:     public List<? extends Strategy<?, ?, ?>> getStrategies() {
111:         return new ArrayList<>(this.strategies);
112:     }
113: 
114:     @Override
115:     public DemoGameBuilder createGameBuilder(final InputProvider inputProvider) throws GameException {
116:         try {
117:             final DemoGameBuilder gameBuilder = new DemoGameBuilderImpl();
118: 
119:             @SuppressWarnings("unchecked")
120:             final Map<String,
121:                     Object> gameData = inputProvider.needInteger(
122:                             GameBuilderFactory.PARAM_MAX_COMPUTATION_TIME_PER_MOVE,
123:                             "Maximum computation time per move in seconds",
124:                             Optional.of(GameBuilder.DEFAULT_MAX_COMPUTATION_TIME_PER_MOVE),
125:                             new MinValueValidator<>(DemoGameBuilderFactoryImpl.MIN_MAX_COMPUTATION_TIME_PER_MOVE),
126:                             new MaxValueValidator<>(DemoGameBuilderFactoryImpl.MAX_MAX_COMPUTATION_TIME_PER_MOVE))
127:                             .requestData("Game properties");
128: 
129:             gameBuilder.changeMaximumComputationTimePerMove(
130:                     (Integer) gameData.get(GameBuilderFactory.PARAM_MAX_COMPUTATION_TIME_PER_MOVE));
131: 
132:             final InputProvider firstPlayerInputProvider = inputProvider.getNext(gameData);
133:             final Map<String, Object> firstPlayerData = this.requestPlayerData(firstPlayerInputProvider, "Player 1");
134:             final DemoPlayer firstPlayer = this.createPlayer(gameBuilder.createPlayerBuilder(), firstPlayerData);
135:             final DemoStrategy firstPlayerStrategy = this.getStrategy(firstPlayerData);
136:             gameBuilder.addPlayer(firstPlayer, firstPlayerStrategy);
137: 
138:             final InputProvider secondPlayerInputProvider = firstPlayerInputProvider.getNext(firstPlayerData);
139:             final Map<String, Object> secondPlayerData = this.requestPlayerData(secondPlayerInputProvider, "Player 2");
140:             final DemoPlayer secondPlayer = this.createPlayer(gameBuilder.createPlayerBuilder(), secondPlayerData);
141:             final DemoStrategy secondPlayerStrategy = this.getStrategy(secondPlayerData);
142:             gameBuilder.addPlayer(secondPlayer, secondPlayerStrategy);
143: 
144:             return gameBuilder;
145:         } catch (final InputProviderException e) {
146:             throw new GameException(String.format("Creating Demo game was aborted: %s", e.getMessage()), e);
147:         }
148:     }
149: 
150:     /**
151:      * Returns data for a player builder.
152:      *
153:      * @param inputProvider The input provider.
154:      * @param title         The title for the UI.
155:      * @throws InputProviderException if the operation has been aborted prematurely (e.g. if the user cancelled a
156:      *                                dialog).
157:      */
158:     @SuppressWarnings("unchecked")
159:     private Map<String, Object> requestPlayerData(final InputProvider inputProvider, final String title)
160:             throws GameException, InputProviderException {
161: 
162:         inputProvider
163:                 .needString(
164:                         GameBuilderFactory.PARAM_PLAYER_NAME,
165:                         "Name",
166:                         Optional.empty(),
167:                         new PatternValidator(Pattern.compile("\\S+(\\s+\\S+)*")))
168:                 .needInteger(
169:                         DemoGameBuilderFactory.PARAM_PLAYER_OUTCOME_ON_NO_NO,
170:                         "Player's outcome on No/No",
171:                         Optional.of(-1))
172:                 .needInteger(
173:                         DemoGameBuilderFactory.PARAM_PLAYER_OUTCOME_ON_NO_YES,
174:                         "Player's outcome on No/Yes",
175:                         Optional.of(0))
176:                 .needInteger(
177:                         DemoGameBuilderFactory.PARAM_PLAYER_OUTCOME_ON_YES_NO,
178:                         "Player's outcome on Yes/No",
179:                         Optional.of(0))
180:                 .needInteger(
181:                         DemoGameBuilderFactory.PARAM_PLAYER_OUTCOME_ON_YES_YES,
182:                         "Player's outcome on Yes/Yes",
183:                         Optional.of(1))
184:                 .needObject(GameBuilderFactory.PARAM_PLAYER_STRATEGY, "Strategy", Optional.empty(), this.strategies);
185: 
186:         return inputProvider.requestData(title);
187:     }
188: 
189:     /**
190:      * Creates a Demo player.
191:      *
192:      * @param playerBuilder The player builder.
193:      * @param playerData    The requested player data.
194:      * @return The created {@link DemoPlayer}.
195:      * @throws InputProviderException if the operation has been aborted prematurely (e.g. if the user cancelled a
196:      *                                dialog).
197:      */
198:     private DemoPlayer createPlayer(final DemoPlayerBuilder playerBuilder,
199:             final Map<String, Object> playerData) throws GameException, InputProviderException {
200: 
201:         final Map<Boolean, Map<Boolean, Double>> possibleOutcomes = new LinkedHashMap<>();
202: 
203:         final Map<Boolean, Double> possibleOutcomesNo = new LinkedHashMap<>();
204:         possibleOutcomesNo.put(
205:                 Boolean.FALSE,
206:                 (double) (Integer) playerData.get(DemoGameBuilderFactory.PARAM_PLAYER_OUTCOME_ON_NO_NO));
207:         possibleOutcomesNo.put(
208:                 Boolean.TRUE,
209:                 (double) (Integer) playerData.get(DemoGameBuilderFactory.PARAM_PLAYER_OUTCOME_ON_NO_YES));
210:         possibleOutcomes.put(Boolean.FALSE, possibleOutcomesNo);
211: 
212:         final Map<Boolean, Double> possibleOutcomesYes = new LinkedHashMap<>();
213:         possibleOutcomesYes.put(
214:                 Boolean.FALSE,
215:                 (double) (Integer) playerData.get(DemoGameBuilderFactory.PARAM_PLAYER_OUTCOME_ON_YES_NO));
216:         possibleOutcomesYes.put(
217:                 Boolean.TRUE,
218:                 (double) (Integer) playerData.get(DemoGameBuilderFactory.PARAM_PLAYER_OUTCOME_ON_YES_YES));
219:         possibleOutcomes.put(Boolean.TRUE, possibleOutcomesYes);
220: 
221:         return playerBuilder.changeName((String) playerData.get(GameBuilderFactory.PARAM_PLAYER_NAME))
222:                 .changePossibleOutcomes(possibleOutcomes).build();
223:     }
224: 
225:     /**
226:      * Returns a Demo strategy.
227:      *
228:      * @param playerData The requested player data.
229:      * @return The Demo strategy.
230:      */
231:     private DemoStrategy getStrategy(final Map<String, Object> playerData) {
232:         return (DemoStrategy) playerData.get(GameBuilderFactory.PARAM_PLAYER_STRATEGY);
233:     }
234: }
    