Skip to content

Package: KopfundZahlundKanteGameBuilderImpl

KopfundZahlundKanteGameBuilderImpl

nameinstructionbranchcomplexitylinemethod
KopfundZahlundKanteGameBuilderImpl()
M: 0 C: 23
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
addPlayer(KopfundZahlundKantePlayer, KopfundZahlundKanteStrategy)
M: 8 C: 40
83%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 7
88%
M: 0 C: 1
100%
build(int)
M: 5 C: 61
92%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 1 C: 8
89%
M: 0 C: 1
100%
changeMaximumComputationTimePerMove(int)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
changeObserverFactoryProvider(ObserverFactoryProvider)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
createPlayerBuilder()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: package de.fhdw.gaming.ipspiel22.kopfundzahlundkante.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.kopfundzahlundkante.domain.KopfundZahlundKanteGame;
13: import de.fhdw.gaming.ipspiel22.kopfundzahlundkante.domain.KopfundZahlundKanteGameBuilder;
14: import de.fhdw.gaming.ipspiel22.kopfundzahlundkante.domain.KopfundZahlundKantePlayer;
15: import de.fhdw.gaming.ipspiel22.kopfundzahlundkante.domain.KopfundZahlundKantePlayerBuilder;
16: import de.fhdw.gaming.ipspiel22.kopfundzahlundkante.domain.KopfundZahlundKanteStrategy;
17: import de.fhdw.gaming.ipspiel22.kopfundzahlundkante.moves.impl.AbstractKopfundZahlundKanteMove;
18:
19: /**
20: * Implements {@link KopfundZahlundKanteGameBuilder}.
21: */
22: public final class KopfundZahlundKanteGameBuilderImpl implements KopfundZahlundKanteGameBuilder {
23:
24: /**
25: * The {@link ObserverFactoryProvider}.
26: */
27: private ObserverFactoryProvider observerFactoryProvider;
28:
29: /**
30: * The builder for the firstPlayer.
31: */
32: private Optional<KopfundZahlundKantePlayer> firstPlayer;
33:
34: /**
35: * The strategy of the firstplayer.
36: */
37: private Optional<KopfundZahlundKanteStrategy> firstPlayerStrategy;
38:
39: /**
40: * The builder for the secondPlayer.
41: */
42: private Optional<KopfundZahlundKantePlayer> secondPlayer;
43:
44: /**
45: * The strategy of the secondplayer.
46: */
47: private Optional<KopfundZahlundKanteStrategy> 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 KopfundZahlundKanteGameBuilderImpl() {
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 KopfundZahlundKantePlayerBuilder createPlayerBuilder() {
68: return new KopfundZahlundKantePlayerBuilderImpl();
69: }
70:
71: @Override
72: public KopfundZahlundKanteGameBuilder addPlayer(final KopfundZahlundKantePlayer player,
73: final KopfundZahlundKanteStrategy strategy) 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 GameBuilder changeMaximumComputationTimePerMove(final int newMaxComputationTimePerMove) {
89: this.maxComputationTimePerMove = newMaxComputationTimePerMove;
90: return this;
91: }
92:
93: @Override
94: public KopfundZahlundKanteGameBuilder changeObserverFactoryProvider(
95: final ObserverFactoryProvider newObserverFactoryProvider) {
96: this.observerFactoryProvider = newObserverFactoryProvider;
97: return this;
98: }
99:
100: @Override
101: public KopfundZahlundKanteGame build(final int id) throws GameException, InterruptedException {
102:• if (!this.firstPlayer.isPresent() || !this.secondPlayer.isPresent()) {
103: throw new GameException("A Head and Tail game needs two players.");
104: }
105:
106: final KopfundZahlundKanteStateImpl initialState = new KopfundZahlundKanteStateImpl(this.firstPlayer.get(),
107: this.secondPlayer.get());
108:
109: final Map<String, KopfundZahlundKanteStrategy> strategies = new LinkedHashMap<>();
110: strategies.put(initialState.getFirstPlayer().getName(), this.firstPlayerStrategy.orElseThrow());
111: strategies.put(initialState.getSecondPlayer().getName(), this.secondPlayerStrategy.orElseThrow());
112: return new KopfundZahlundKanteGameImpl(
113: id,
114: initialState,
115: strategies,
116: this.maxComputationTimePerMove,
117: AbstractKopfundZahlundKanteMove.class::isInstance,
118: this.observerFactoryProvider);
119: }
120:
121: }