Skip to content

Method: createPlayerBuilder()

1: package de.fhdw.gaming.ipspiel22.vierGewinnt.domain.impl;
2:
3: import java.util.LinkedHashMap;
4: import java.util.Map;
5: import java.util.Objects;
6: import java.util.Optional;
7:
8: import de.fhdw.gaming.core.domain.DefaultObserverFactoryProvider;
9: import de.fhdw.gaming.core.domain.GameBuilder;
10: import de.fhdw.gaming.core.domain.GameException;
11: import de.fhdw.gaming.core.domain.ObserverFactoryProvider;
12: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGGame;
13: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGGameBuilder;
14: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGPlayer;
15: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGPlayerBuilder;
16: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGStrategy;
17: import de.fhdw.gaming.ipspiel22.vierGewinnt.moves.impl.AbstractVGMove;
18:
19: /**
20: * Implements {@link VGGameBuilder}.
21: */
22: public class VGGameBuilderImpl implements VGGameBuilder {
23:
24: /**
25: * The {@link ObserverFactoryProvider}.
26: */
27: private ObserverFactoryProvider observerFactoryProvider;
28: /**
29: * The player using red tokens.
30: */
31: private Optional<VGPlayer> redPlayer;
32: /**
33: * The strategy of the player using red tokens.
34: */
35: private Optional<VGStrategy> redPlayerStrategy;
36: /**
37: * The player using yellow tokens.
38: */
39: private Optional<VGPlayer> yellowPlayer;
40:
41: /**
42: * The strategy of the player using yellow tokens.
43: */
44: private Optional<VGStrategy> yellowPlayerStrategy;
45: /**
46: * The maximum computation time per move in seconds.
47: */
48: private int maxComputationTimePerMove;
49:
50: /**
51: * Creates a Vier gewinnt game builder.
52: */
53: public VGGameBuilderImpl() {
54: this.observerFactoryProvider = new DefaultObserverFactoryProvider();
55: this.redPlayer = Optional.empty();
56: this.redPlayerStrategy = Optional.empty();
57: this.yellowPlayer = Optional.empty();
58: this.yellowPlayerStrategy = Optional.empty();
59: this.maxComputationTimePerMove = GameBuilder.DEFAULT_MAX_COMPUTATION_TIME_PER_MOVE;
60: }
61:
62: @Override
63: public VGPlayerBuilder createPlayerBuilder() {
64: return new VGPlayerBuilderImpl();
65: }
66:
67: @Override
68: public VGGameBuilder addPlayer(final VGPlayer player, final VGStrategy strategy)
69: throws GameException {
70:
71: if (player.isUsingRedChips() && this.redPlayer.isEmpty()) {
72: this.redPlayer = Optional.of(Objects.requireNonNull(player, "player"));
73: this.redPlayerStrategy = Optional.of(Objects.requireNonNull(strategy, "redPlayerStrategy"));
74: } else if (!player.isUsingRedChips() && this.yellowPlayer.isEmpty()) {
75: this.yellowPlayer = Optional.of(Objects.requireNonNull(player, "player"));
76: this.yellowPlayerStrategy = Optional.of(Objects.requireNonNull(strategy, "yellowPlayerStrategy"));
77: } else {
78: throw new GameException(String.format("More than two players are now allowed."));
79: }
80: return this;
81: }
82:
83: @Override
84: public VGGameBuilder changeMaximumComputationTimePerMove(final int newMaxComputationTimePerMove) {
85: this.maxComputationTimePerMove = newMaxComputationTimePerMove;
86: return this;
87: }
88:
89: @Override
90: public VGGameBuilder changeObserverFactoryProvider(final ObserverFactoryProvider newObserverFactoryProvider) {
91: this.observerFactoryProvider = newObserverFactoryProvider;
92: return this;
93: }
94:
95: @Override
96: public VGGame build(final int id) throws GameException, InterruptedException {
97: if (!this.redPlayer.isPresent() || !this.yellowPlayer.isPresent()) {
98: throw new GameException("A Vier gewinnt game needs two players.");
99: }
100:
101: final VGBoardImpl board = new VGBoardImpl();
102: final VGStateImpl initialState = new VGStateImpl(board, this.redPlayer.get(), this.yellowPlayer.get(), true);
103:
104: final Map<String, VGStrategy> strategies = new LinkedHashMap<>();
105: strategies.put(initialState.getRedPlayer().getName(), this.redPlayerStrategy.orElseThrow());
106: strategies.put(initialState.getYellowPlayer().getName(), this.yellowPlayerStrategy.orElseThrow());
107: return new VGGameImpl(
108: id,
109: initialState,
110: strategies,
111: this.maxComputationTimePerMove,
112: AbstractVGMove.class::isInstance,
113: this.observerFactoryProvider);
114: }
115:
116: /**
117: * getObserverFactoryProvider.
118: * @return
119: */
120: public ObserverFactoryProvider getObserverFactoryProvider() {
121: return observerFactoryProvider;
122: }
123:
124: /**
125: * getMaxComputationTimePerMove.
126: * @return
127: */
128: public int getMaxComputationTimePerMove() {
129: return maxComputationTimePerMove;
130: }
131: }