Skip to content

Method: changeMaximumComputationTimePerMove(int)

1: package de.fhdw.gaming.ipspiel22.kopfundzahl.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.kopfundzahl.domain.KopfundZahlGame;
13: import de.fhdw.gaming.ipspiel22.kopfundzahl.domain.KopfundZahlGameBuilder;
14: import de.fhdw.gaming.ipspiel22.kopfundzahl.domain.KopfundZahlPlayer;
15: import de.fhdw.gaming.ipspiel22.kopfundzahl.domain.KopfundZahlPlayerBuilder;
16: import de.fhdw.gaming.ipspiel22.kopfundzahl.domain.KopfundZahlStrategy;
17: import de.fhdw.gaming.ipspiel22.kopfundzahl.moves.impl.AbstractKopfundZahlMove;
18:
19: /**
20: * Implements {@link KopfundZahlGameBuilder}.
21: */
22: public final class KopfundZahlGameBuilderImpl implements KopfundZahlGameBuilder {
23:
24: /**
25: * The {@link ObserverFactoryProvider}.
26: */
27: private ObserverFactoryProvider observerFactoryProvider;
28:
29: /**
30: * The builder for the firstPlayer.
31: */
32: private Optional<KopfundZahlPlayer> firstPlayer;
33:
34: /**
35: * The strategy of the firstplayer.
36: */
37: private Optional<KopfundZahlStrategy> firstPlayerStrategy;
38:
39: /**
40: * The builder for the secondPlayer.
41: */
42: private Optional<KopfundZahlPlayer> secondPlayer;
43:
44: /**
45: * The strategy of the secondplayer.
46: */
47: private Optional<KopfundZahlStrategy> secondPlayerStrategy;
48:
49: /**
50: * The maximum computation time per move in seconds.
51: */
52: private int maxComputationTimePerMove;
53:
54: /**
55: * Creates a KopfundZahl game builder.
56: */
57: public KopfundZahlGameBuilderImpl() {
58: this.observerFactoryProvider = new DefaultObserverFactoryProvider();
59: this.firstPlayer = Optional.empty();
60: this.firstPlayerStrategy = Optional.empty();
61: this.secondPlayer = Optional.empty();
62: this.secondPlayerStrategy = Optional.empty();
63: this.maxComputationTimePerMove = GameBuilder.DEFAULT_MAX_COMPUTATION_TIME_PER_MOVE;
64: }
65:
66: @Override
67: public KopfundZahlPlayerBuilder createPlayerBuilder() {
68: return new KopfundZahlPlayerBuilderImpl();
69: }
70:
71: @Override
72: public KopfundZahlGameBuilder addPlayer(final KopfundZahlPlayer player,
73: final KopfundZahlStrategy strategy)
74: throws GameException {
75:
76: if (this.firstPlayer.isEmpty()) {
77: this.firstPlayer = Optional.of(Objects.requireNonNull(player, "player"));
78: this.firstPlayerStrategy = Optional.of(Objects.requireNonNull(strategy, "firstPlayerStrategy"));
79: } else if (this.secondPlayer.isEmpty()) {
80: this.secondPlayer = Optional.of(Objects.requireNonNull(player, "player"));
81: this.secondPlayerStrategy = Optional.of(Objects.requireNonNull(strategy, "secondPlayerStrategy"));
82: } else {
83: throw new GameException(String.format("More than two players are now allowed."));
84: }
85: return this;
86: }
87:
88: @Override
89: public GameBuilder changeMaximumComputationTimePerMove(final int newMaxComputationTimePerMove) {
90: this.maxComputationTimePerMove = newMaxComputationTimePerMove;
91: return this;
92: }
93:
94: @Override
95: public KopfundZahlGameBuilder changeObserverFactoryProvider(
96: final ObserverFactoryProvider newObserverFactoryProvider) {
97: this.observerFactoryProvider = newObserverFactoryProvider;
98: return this;
99: }
100:
101: @Override
102: public KopfundZahlGame build(final int id) throws GameException, InterruptedException {
103: if (!this.firstPlayer.isPresent() || !this.secondPlayer.isPresent()) {
104: throw new GameException("A Head and Tail game needs two players.");
105: }
106:
107: final KopfundZahlStateImpl initialState = new KopfundZahlStateImpl(this.firstPlayer.get(),
108: this.secondPlayer.get());
109:
110: final Map<String, KopfundZahlStrategy> strategies = new LinkedHashMap<>();
111: strategies.put(initialState.getFirstPlayer().getName(), this.firstPlayerStrategy.orElseThrow());
112: strategies.put(initialState.getSecondPlayer().getName(), this.secondPlayerStrategy.orElseThrow());
113: return new KopfundZahlGameImpl(
114: id,
115: initialState,
116: strategies,
117: this.maxComputationTimePerMove,
118: AbstractKopfundZahlMove.class::isInstance,
119: this.observerFactoryProvider);
120: }
121:
122: }