Skip to content

Method: changeObserverFactoryProvider(ObserverFactoryProvider)

1: /*
2: * Copyright © 2021-2023 Fachhochschule für die Wirtschaft (FHDW) Hannover
3: *
4: * This file is part of ipspiel23-Ssp.
5: *
6: * Ipspiel23-Ssp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
7: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
8: * version.
9: *
10: * Ipspiel23-Ssp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
11: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12: *
13: * You should have received a copy of the GNU General Public License along with ipspiel23-Ssp. If not, see
14: * <http://www.gnu.org/licenses/>.
15: */
16: package de.fhdw.gaming.ipspiel23.ssp.domain.impl;
17:
18: import de.fhdw.gaming.core.domain.DefaultObserverFactoryProvider;
19: import de.fhdw.gaming.core.domain.GameBuilder;
20: import de.fhdw.gaming.core.domain.GameException;
21: import de.fhdw.gaming.core.domain.ObserverFactoryProvider;
22: import de.fhdw.gaming.ipspiel23.ssp.domain.SspGame;
23: import de.fhdw.gaming.ipspiel23.ssp.domain.SspGameBuilder;
24: import de.fhdw.gaming.ipspiel23.ssp.domain.SspPlayer;
25: import de.fhdw.gaming.ipspiel23.ssp.domain.SspPlayerBuilder;
26: import de.fhdw.gaming.ipspiel23.ssp.domain.SspStrategy;
27: import de.fhdw.gaming.ipspiel23.ssp.moves.impl.AbstractSspMove;
28:
29: import java.util.LinkedHashMap;
30: import java.util.Map;
31: import java.util.Objects;
32: import java.util.Optional;
33:
34: /**
35: * Implements {@link SspGameBuilder}.
36: */
37: final class SspGameBuilderImpl implements SspGameBuilder {
38:
39: /**
40: * The {@link ObserverFactoryProvider}.
41: */
42: private ObserverFactoryProvider observerFactoryProvider;
43: /**
44: * The first player.
45: */
46: private Optional<SspPlayer> firstPlayer;
47: /**
48: * The strategy of the first player.
49: */
50: private Optional<SspStrategy> firstPlayerStrategy;
51: /**
52: * The second player.
53: */
54: private Optional<SspPlayer> secondPlayer;
55: /**
56: * The strategy of the second player.
57: */
58: private Optional<SspStrategy> secondPlayerStrategy;
59: /**
60: * The maximum computation time per move in seconds.
61: */
62: private int maxComputationTimePerMove;
63:
64: /**
65: * Creates a Ssp game builder.
66: */
67: SspGameBuilderImpl() {
68: this.observerFactoryProvider = new DefaultObserverFactoryProvider();
69: this.firstPlayer = Optional.empty();
70: this.firstPlayerStrategy = Optional.empty();
71: this.secondPlayer = Optional.empty();
72: this.secondPlayerStrategy = Optional.empty();
73: this.maxComputationTimePerMove = GameBuilder.DEFAULT_MAX_COMPUTATION_TIME_PER_MOVE;
74: }
75:
76: @Override
77: public SspPlayerBuilder createPlayerBuilder() {
78: return new SspPlayerBuilderImpl();
79: }
80:
81: @Override
82: public SspGameBuilder addPlayer(final SspPlayer player, final SspStrategy strategy)
83: throws GameException {
84:
85: if (this.firstPlayer.isEmpty()) {
86: this.firstPlayer = Optional.of(Objects.requireNonNull(player, "player"));
87: this.firstPlayerStrategy = Optional.of(Objects.requireNonNull(strategy, "firstPlayerStrategy"));
88: } else if (this.secondPlayer.isEmpty()) {
89: this.secondPlayer = Optional.of(Objects.requireNonNull(player, "player"));
90: this.secondPlayerStrategy = Optional.of(Objects.requireNonNull(strategy, "secondPlayerStrategy"));
91: } else {
92: throw new GameException(String.format("More than two players are now allowed."));
93: }
94: return this;
95: }
96:
97: @Override
98: public SspGameBuilder changeMaximumComputationTimePerMove(final int newMaxComputationTimePerMove) {
99: this.maxComputationTimePerMove = newMaxComputationTimePerMove;
100: return this;
101: }
102:
103: @Override
104: public SspGameBuilder changeObserverFactoryProvider(final ObserverFactoryProvider newObserverFactoryProvider) {
105: this.observerFactoryProvider = newObserverFactoryProvider;
106: return this;
107: }
108:
109: @Override
110: public SspGame build(final int id) throws GameException, InterruptedException {
111: if (!this.firstPlayer.isPresent() || !this.secondPlayer.isPresent()) {
112: throw new GameException("A Ssp game needs two players.");
113: }
114:
115: final SspStateImpl initialState = new SspStateImpl(this.firstPlayer.get(), this.secondPlayer.get());
116:
117: final Map<String, SspStrategy> strategies = new LinkedHashMap<>();
118: strategies.put(initialState.getFirstPlayer().getName(), this.firstPlayerStrategy.orElseThrow());
119: strategies.put(initialState.getSecondPlayer().getName(), this.secondPlayerStrategy.orElseThrow());
120: return new SspGameImpl(
121: id,
122: initialState,
123: strategies,
124: this.maxComputationTimePerMove,
125: AbstractSspMove.class::isInstance,
126: this.observerFactoryProvider);
127: }
128: }