Skip to contentMethod: FGGameBuilderImpl()
      1: 
2: package de.fhdw.gaming.ipspiel24.fg.domain.impl;
3: 
4: import java.util.LinkedHashMap;
5: import java.util.Map;
6: import java.util.Objects;
7: import java.util.Optional;
8: 
9: import de.fhdw.gaming.core.domain.DefaultGame;
10: import de.fhdw.gaming.core.domain.DefaultObserverFactoryProvider;
11: import de.fhdw.gaming.core.domain.Game;
12: import de.fhdw.gaming.core.domain.GameBuilder;
13: import de.fhdw.gaming.core.domain.GameException;
14: import de.fhdw.gaming.core.domain.ObserverFactoryProvider;
15: import de.fhdw.gaming.ipspiel24.fg.domain.FGGameBuilder;
16: import de.fhdw.gaming.ipspiel24.fg.domain.FGPlayer;
17: import de.fhdw.gaming.ipspiel24.fg.domain.FGPlayerBuilder;
18: import de.fhdw.gaming.ipspiel24.fg.domain.FGState;
19: import de.fhdw.gaming.ipspiel24.fg.domain.FGStrategy;
20: import de.fhdw.gaming.ipspiel24.fg.moves.FGMove;
21: import de.fhdw.gaming.ipspiel24.fg.moves.impl.AbstractFGMove;
22: 
23: /**
24:  * Implements {@link FGGameBuilder}.
25:  */
26: final class FGGameBuilderImpl implements FGGameBuilder {
27: 
28:     /**
29:      * The {@link ObserverFactoryProvider}.
30:      */
31:     private ObserverFactoryProvider observerFactoryProvider;
32:     /**
33:      * The player using black tokens.
34:      */
35:     private Optional<FGPlayer> firstPlayer;
36:     /**
37:      * The strategy of the player using black tokens.
38:      */
39:     private Optional<FGStrategy> firstPlayerStrategy;
40:     /**
41:      * The player using white tokens.
42:      */
43:     private Optional<FGPlayer> secondPlayer;
44:     /**
45:      * The strategy of the player using white tokens.
46:      */
47:     private Optional<FGStrategy> secondPlayerStrategy;
48:     /**
49:      * The maximum computation time per move in seconds.
50:      */
51:     private int maxComputationTimePerMove;
52: 
53:     /**
54:      * Creates a FG game builder.
55:      */
56:     FGGameBuilderImpl() {
57:         this.observerFactoryProvider = new DefaultObserverFactoryProvider();
58:         this.firstPlayer = Optional.empty();
59:         this.firstPlayerStrategy = Optional.empty();
60:         this.secondPlayer = Optional.empty();
61:         this.secondPlayerStrategy = Optional.empty();
62:         this.maxComputationTimePerMove = GameBuilder.DEFAULT_MAX_COMPUTATION_TIME_PER_MOVE;
63:     }
64: 
65:     @Override
66:     public FGPlayerBuilder createPlayerBuilder() {
67:         return new FGPlayerBuilderImpl();
68:     }
69: 
70:     @Override
71:     public FGGameBuilder addPlayer(final FGPlayer player, final FGStrategy strategy)
72:             throws GameException {
73: 
74:         if (this.firstPlayer.isEmpty()) {
75:             this.firstPlayer = Optional.of(Objects.requireNonNull(player, "player"));
76:             this.firstPlayerStrategy = Optional.of(Objects.requireNonNull(strategy, "firstPlayerStrategy"));
77:         } else if (this.secondPlayer.isEmpty()) {
78:             this.secondPlayer = Optional.of(Objects.requireNonNull(player, "player"));
79:             this.secondPlayerStrategy = Optional.of(Objects.requireNonNull(strategy, "secondPlayerStrategy"));
80:         } else {
81:             throw new GameException(String.format("More than two players are now allowed."));
82:         }
83:         return this;
84:     }
85: 
86:     @Override
87:     public FGGameBuilder changeMaximumComputationTimePerMove(final int newMaxComputationTimePerMove) {
88:         this.maxComputationTimePerMove = newMaxComputationTimePerMove;
89:         return this;
90:     }
91: 
92:     @Override
93:     public FGGameBuilder changeObserverFactoryProvider(final ObserverFactoryProvider newObserverFactoryProvider) {
94:         this.observerFactoryProvider = newObserverFactoryProvider;
95:         return this;
96:     }
97: 
98:     @Override
99:     public Game<FGPlayer, FGState, FGMove, FGStrategy> build(final int id)
100:             throws GameException, InterruptedException {
101:         if (!this.firstPlayer.isPresent() || !this.secondPlayer.isPresent()) {
102:             throw new GameException("A FG game needs two players.");
103:         }
104: 
105:         final FGStateImpl initialState = new FGStateImpl(this.firstPlayer.get(), this.secondPlayer.get());
106: 
107:         final Map<String, FGStrategy> strategies = new LinkedHashMap<>();
108:         strategies.put(initialState.getFirstPlayer().getName(), this.firstPlayerStrategy.orElseThrow());
109:         strategies.put(initialState.getSecondPlayer().getName(), this.secondPlayerStrategy.orElseThrow());
110:         return new DefaultGame<>(
111:                 id,
112:                 initialState,
113:                 strategies,
114:                 this.maxComputationTimePerMove,
115:                 AbstractFGMove.class::isInstance,
116:                 new FGMoveGeneratorImpl(),
117:                 this.observerFactoryProvider);
118:     }
119: }