Skip to content

Package: MuenzwurfGameBuilderFactoryImpl

MuenzwurfGameBuilderFactoryImpl

nameinstructionbranchcomplexitylinemethod
MuenzwurfGameBuilderFactoryImpl()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
MuenzwurfGameBuilderFactoryImpl(MuenzwurfStrategyFactoryProvider)
M: 0 C: 33
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
createGameBuilder(InputProvider)
M: 17 C: 100
85%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 2 C: 22
92%
M: 0 C: 1
100%
createPlayer(MuenzwurfPlayerBuilder, Map)
M: 0 C: 171
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 49
100%
M: 0 C: 1
100%
getMaximumNumberOfPlayers()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getMinimumNumberOfPlayers()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getName()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getStrategies()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getStrategy(Map)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
requestPlayerData(InputProvider, String, Optional)
M: 0 C: 43
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 12
100%
M: 0 C: 1
100%

Coverage

1: package de.fhdw.gaming.ipspiel24.muenzwurf.core.domain.impl;
2:
3: import java.util.ArrayList;
4: import java.util.LinkedHashMap;
5: import java.util.LinkedHashSet;
6: import java.util.List;
7: import java.util.Map;
8: import java.util.Optional;
9: import java.util.Set;
10: import java.util.regex.Pattern;
11:
12: import de.fhdw.gaming.core.domain.GameBuilder;
13: import de.fhdw.gaming.core.domain.GameBuilderFactory;
14: import de.fhdw.gaming.core.domain.GameException;
15: import de.fhdw.gaming.core.domain.Strategy;
16: import de.fhdw.gaming.core.ui.InputProvider;
17: import de.fhdw.gaming.core.ui.InputProviderException;
18: import de.fhdw.gaming.core.ui.type.validator.MaxValueValidator;
19: import de.fhdw.gaming.core.ui.type.validator.MinValueValidator;
20: import de.fhdw.gaming.core.ui.type.validator.PatternValidator;
21: import de.fhdw.gaming.ipspiel24.muenzwurf.core.domain.MuenzwurfGameBuilder;
22: import de.fhdw.gaming.ipspiel24.muenzwurf.core.domain.MuenzwurfGameBuilderFactory;
23: import de.fhdw.gaming.ipspiel24.muenzwurf.core.domain.MuenzwurfPlayer;
24: import de.fhdw.gaming.ipspiel24.muenzwurf.core.domain.MuenzwurfPlayerBuilder;
25: import de.fhdw.gaming.ipspiel24.muenzwurf.core.domain.MuenzwurfStrategy;
26: import de.fhdw.gaming.ipspiel24.muenzwurf.core.domain.MuenzwurfSide;
27: import de.fhdw.gaming.ipspiel24.muenzwurf.core.domain.factory.MuenzwurfDefaultStrategyFactoryProvider;
28: import de.fhdw.gaming.ipspiel24.muenzwurf.core.domain.factory.MuenzwurfStrategyFactory;
29: import de.fhdw.gaming.ipspiel24.muenzwurf.core.domain.factory.MuenzwurfStrategyFactoryProvider;
30: import de.fhdw.gaming.ipspiel24.muenzwurf.core.moves.factory.MuenzwurfMoveFactory;
31: import de.fhdw.gaming.ipspiel24.muenzwurf.core.moves.impl.MuenzwurfDefaultMoveFactory;
32:
33: /**
34: * Implements {@link GameBuilderFactory} by creating a Muenzwurf game builder.
35: */
36: public final class MuenzwurfGameBuilderFactoryImpl implements MuenzwurfGameBuilderFactory {
37:
38: /**
39: * The number of players.
40: */
41: private static final int NUMBER_OF_PLAYERS = 2;
42: /**
43: * Smallest allowed maximum computation time per move in seconds.
44: */
45: private static final int MIN_MAX_COMPUTATION_TIME_PER_MOVE = 1;
46: /**
47: * Largest allowed maximum computation time per move in seconds.
48: */
49: private static final int MAX_MAX_COMPUTATION_TIME_PER_MOVE = 3600;
50:
51: /**
52: * All available Muenzwurf strategies.
53: */
54: private final Set<MuenzwurfStrategy> strategies;
55:
56: /**
57: * Creates a Muenzwurf game factory. Muenzwurf strategies are loaded by using the {@link java.util.ServiceLoader}.
58: * <p>
59: * This constructor is meant to be used by the {@link java.util.ServiceLoader}.
60: */
61: public MuenzwurfGameBuilderFactoryImpl() {
62: this(new MuenzwurfDefaultStrategyFactoryProvider());
63: }
64:
65: /**
66: * Creates a Muenzwurf game factory.
67: *
68: * @param strategyFactoryProvider The {@link MuenzwurfStrategyFactoryProvider} for loading Muenzwurf strategies.
69: */
70: MuenzwurfGameBuilderFactoryImpl(final MuenzwurfStrategyFactoryProvider strategyFactoryProvider) {
71: final MuenzwurfMoveFactory moveFactory = new MuenzwurfDefaultMoveFactory();
72:
73: final List<MuenzwurfStrategyFactory> factories = strategyFactoryProvider.getStrategyFactories();
74: this.strategies = new LinkedHashSet<>();
75:• for (final MuenzwurfStrategyFactory factory : factories) {
76: this.strategies.add(factory.create(moveFactory));
77: }
78: }
79:
80: @Override
81: public String getName() {
82: return "Muenzwurf";
83: }
84:
85: @Override
86: public int getMinimumNumberOfPlayers() {
87: return MuenzwurfGameBuilderFactoryImpl.NUMBER_OF_PLAYERS;
88: }
89:
90: @Override
91: public int getMaximumNumberOfPlayers() {
92: return MuenzwurfGameBuilderFactoryImpl.NUMBER_OF_PLAYERS;
93: }
94:
95: @Override
96: public List<? extends Strategy<?, ?, ?>> getStrategies() {
97: return new ArrayList<>(this.strategies);
98: }
99:
100: @Override
101: public MuenzwurfGameBuilder createGameBuilder(final InputProvider inputProvider) throws GameException {
102: try {
103: final MuenzwurfGameBuilder gameBuilder = new MuenzwurfGameBuilderImpl();
104:
105: @SuppressWarnings("unchecked")
106: final Map<String,
107: Object> gameData = inputProvider.needInteger(
108: GameBuilderFactory.PARAM_MAX_COMPUTATION_TIME_PER_MOVE,
109: "Maximum computation time per move in seconds",
110: Optional.of(GameBuilder.DEFAULT_MAX_COMPUTATION_TIME_PER_MOVE),
111: new MinValueValidator<>(MuenzwurfGameBuilderFactoryImpl.MIN_MAX_COMPUTATION_TIME_PER_MOVE),
112: new MaxValueValidator<>(MuenzwurfGameBuilderFactoryImpl.MAX_MAX_COMPUTATION_TIME_PER_MOVE))
113: .requestData("Game properties");
114:
115: gameBuilder.changeMaximumComputationTimePerMove(
116: (Integer) gameData.get(GameBuilderFactory.PARAM_MAX_COMPUTATION_TIME_PER_MOVE));
117:
118: final InputProvider firstPlayerInputProvider = inputProvider.getNext(gameData);
119: final Map<String, Object> firstPlayerData = this.requestPlayerData(firstPlayerInputProvider,
120: "Player 1", Optional.empty());
121: final MuenzwurfPlayer firstPlayer = this.createPlayer(gameBuilder.createPlayerBuilder(), firstPlayerData);
122: final MuenzwurfStrategy firstPlayerStrategy = this.getStrategy(firstPlayerData);
123: gameBuilder.addPlayer(firstPlayer, firstPlayerStrategy);
124:
125: final InputProvider secondPlayerInputProvider = firstPlayerInputProvider.getNext(firstPlayerData);
126: final Map<String, Object> secondPlayerData = this.requestPlayerData(secondPlayerInputProvider,
127: "Player 2", Optional.of(
128:• !(Boolean) firstPlayerData.get(MuenzwurfGameBuilderFactory.PARAM_PLAYER_POINTGAIN_ON_SAME_RESULT)));
129: final MuenzwurfPlayer secondPlayer = this.createPlayer(gameBuilder.createPlayerBuilder(), secondPlayerData);
130: final MuenzwurfStrategy secondPlayerStrategy = this.getStrategy(secondPlayerData);
131: gameBuilder.addPlayer(secondPlayer, secondPlayerStrategy);
132:
133: return gameBuilder;
134: } catch (final InputProviderException e) {
135: throw new GameException(String.format("Creating Muenzwurf game was aborted: %s", e.getMessage()), e);
136: }
137: }
138:
139: /**
140: * Returns data for a player builder.
141: *
142: * @param inputProvider The input provider.
143: * @param title The title for the UI.
144: * @param pointsOnSame Whether the player gains poins on the same chosen side.
145: * @throws InputProviderException if the operation has been aborted prematurely (e.g. if the user cancelled a
146: * dialog).
147: */
148: @SuppressWarnings("unchecked")
149: private Map<String, Object> requestPlayerData(final InputProvider inputProvider, final String title,
150: final Optional<Boolean> pointsOnSame)
151: throws GameException, InputProviderException {
152:
153: inputProvider
154: .needString(
155: GameBuilderFactory.PARAM_PLAYER_NAME,
156: "Name",
157: Optional.empty(),
158: new PatternValidator(Pattern.compile("\\S+(\\s+\\S+)*")))
159: .needBoolean(
160: MuenzwurfGameBuilderFactory.PARAM_PLAYER_POINTGAIN_ON_SAME_RESULT,
161: "Player gets points on same result",
162: Optional.of(Boolean.TRUE))
163: .needObject(GameBuilderFactory.PARAM_PLAYER_STRATEGY, "Strategy", Optional.empty(), this.strategies);
164:
165:• if (pointsOnSame.isPresent()) {
166: inputProvider
167: .fixedBoolean(MuenzwurfGameBuilderFactory.PARAM_PLAYER_POINTGAIN_ON_SAME_RESULT,
168: pointsOnSame.get());
169: }
170:
171: return inputProvider.requestData(title);
172: }
173:
174: /**
175: * Creates a Muenzwurf player.
176: *
177: * @param playerBuilder The player builder.
178: * @param playerData The requested player data.
179: * @return The created {@link MuenzwurfPlayer}.
180: * @throws InputProviderException if the operation has been aborted prematurely (e.g. if the user cancelled a
181: * dialog).
182: */
183: private MuenzwurfPlayer createPlayer(final MuenzwurfPlayerBuilder playerBuilder,
184: final Map<String, Object> playerData) throws GameException, InputProviderException {
185:
186: final Map<MuenzwurfSide, Map<MuenzwurfSide, Double>> possibleOutcomes = new LinkedHashMap<>();
187: final Map<MuenzwurfSide, Double> possibleOutcomesTails = new LinkedHashMap<>();
188: final Map<MuenzwurfSide, Double> possibleOutcomesHeads = new LinkedHashMap<>();
189: final Map<MuenzwurfSide, Double> possibleOutcomesEdge = new LinkedHashMap<>();
190:• if ((Boolean) playerData.get(MuenzwurfGameBuilderFactory.PARAM_PLAYER_POINTGAIN_ON_SAME_RESULT)) {
191: possibleOutcomesTails.put(
192: MuenzwurfSide.TAILS,
193: 1.0);
194: possibleOutcomesTails.put(
195: MuenzwurfSide.HEADS,
196: -1.0);
197: possibleOutcomesTails.put(
198: MuenzwurfSide.EDGE,
199: -1.0);
200: possibleOutcomes.put(MuenzwurfSide.TAILS, possibleOutcomesTails);
201:
202: possibleOutcomesHeads.put(
203: MuenzwurfSide.TAILS,
204: -1.0);
205: possibleOutcomesHeads.put(
206: MuenzwurfSide.HEADS,
207: 1.0);
208: possibleOutcomesHeads.put(
209: MuenzwurfSide.EDGE,
210: -1.0);
211: possibleOutcomes.put(MuenzwurfSide.HEADS, possibleOutcomesHeads);
212:
213: possibleOutcomesEdge.put(
214: MuenzwurfSide.TAILS,
215: -1.0);
216: possibleOutcomesEdge.put(
217: MuenzwurfSide.HEADS,
218: -1.0);
219: possibleOutcomesEdge.put(
220: MuenzwurfSide.EDGE,
221: 1.0);
222: possibleOutcomes.put(MuenzwurfSide.EDGE, possibleOutcomesEdge);
223: } else {
224: possibleOutcomesTails.put(
225: MuenzwurfSide.TAILS,
226: -1.0);
227: possibleOutcomesTails.put(
228: MuenzwurfSide.HEADS,
229: 1.0);
230: possibleOutcomesTails.put(
231: MuenzwurfSide.EDGE,
232: 1.0);
233: possibleOutcomes.put(MuenzwurfSide.TAILS, possibleOutcomesTails);
234:
235: possibleOutcomesHeads.put(
236: MuenzwurfSide.TAILS,
237: 1.0);
238: possibleOutcomesHeads.put(
239: MuenzwurfSide.HEADS,
240: -1.0);
241: possibleOutcomesHeads.put(
242: MuenzwurfSide.EDGE,
243: 1.0);
244: possibleOutcomes.put(MuenzwurfSide.HEADS, possibleOutcomesHeads);
245:
246: possibleOutcomesEdge.put(
247: MuenzwurfSide.TAILS,
248: 1.0);
249: possibleOutcomesEdge.put(
250: MuenzwurfSide.HEADS,
251: 1.0);
252: possibleOutcomesEdge.put(
253: MuenzwurfSide.EDGE,
254: -1.0);
255: possibleOutcomes.put(MuenzwurfSide.EDGE, possibleOutcomesEdge);
256: }
257:
258: return playerBuilder.changeName((String) playerData.get(GameBuilderFactory.PARAM_PLAYER_NAME))
259: .changePossibleOutcomes(possibleOutcomes).build();
260: }
261:
262: /**
263: * Returns a Muenzwurf strategy.
264: *
265: * @param playerData The requested player data.
266: * @return The Muenzwurf strategy.
267: */
268: private MuenzwurfStrategy getStrategy(final Map<String, Object> playerData) {
269: return (MuenzwurfStrategy) playerData.get(GameBuilderFactory.PARAM_PLAYER_STRATEGY);
270: }
271: }