Skip to content

Method: getStrategy(Map)

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