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