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 ipspiel24-GD.
5: *
6: * ipspiel24-GD 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: * ipspiel24-GD 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 ipspiel24-GD. If not, see
14: * <http://www.gnu.org/licenses/>.
15: */
16: package de.fhdw.gaming.GefangenenDilemma.domain.impl;
17:
18: import java.util.LinkedHashMap;
19: import java.util.Map;
20: import java.util.Objects;
21: import java.util.Optional;
22:
23: import de.fhdw.gaming.GefangenenDilemma.domain.GDGameBuilder;
24: import de.fhdw.gaming.GefangenenDilemma.domain.GDPlayer;
25: import de.fhdw.gaming.GefangenenDilemma.domain.GDPlayerBuilder;
26: import de.fhdw.gaming.GefangenenDilemma.domain.GDState;
27: import de.fhdw.gaming.GefangenenDilemma.domain.GDStrategy;
28: import de.fhdw.gaming.GefangenenDilemma.moves.GDMove;
29: import de.fhdw.gaming.GefangenenDilemma.moves.impl.AbstractGDMove;
30: import de.fhdw.gaming.core.domain.DefaultGame;
31: import de.fhdw.gaming.core.domain.DefaultObserverFactoryProvider;
32: import de.fhdw.gaming.core.domain.Game;
33: import de.fhdw.gaming.core.domain.GameBuilder;
34: import de.fhdw.gaming.core.domain.GameException;
35: import de.fhdw.gaming.core.domain.ObserverFactoryProvider;
36:
37: /**
38: * Implements {@link GDGameBuilder}.
39: */
40: final class GDGameBuilderImpl implements GDGameBuilder {
41:
42: /**
43: * The {@link ObserverFactoryProvider}.
44: */
45: private ObserverFactoryProvider observerFactoryProvider;
46: /**
47: * The player using black tokens.
48: */
49: private Optional<GDPlayer> firstPlayer;
50: /**
51: * The strategy of the player using black tokens.
52: */
53: private Optional<GDStrategy> firstPlayerStrategy;
54: /**
55: * The player using white tokens.
56: */
57: private Optional<GDPlayer> secondPlayer;
58: /**
59: * The strategy of the player using white tokens.
60: */
61: private Optional<GDStrategy> secondPlayerStrategy;
62: /**
63: * The maximum computation time per move in seconds.
64: */
65: private int maxComputationTimePerMove;
66:
67: /**
68: * Creates a Gefangenen-Dilemma game builder.
69: */
70: GDGameBuilderImpl() {
71: this.observerFactoryProvider = new DefaultObserverFactoryProvider();
72: this.firstPlayer = Optional.empty();
73: this.firstPlayerStrategy = Optional.empty();
74: this.secondPlayer = Optional.empty();
75: this.secondPlayerStrategy = Optional.empty();
76: this.maxComputationTimePerMove = GameBuilder.DEFAULT_MAX_COMPUTATION_TIME_PER_MOVE;
77: }
78:
79: @Override
80: public GDPlayerBuilder createPlayerBuilder() {
81: return new GDPlayerBuilderImpl();
82: }
83:
84: @Override
85: public GDGameBuilder addPlayer(final GDPlayer player,
86: final GDStrategy strategy)
87: throws GameException {
88:
89: if (this.firstPlayer.isEmpty()) {
90: this.firstPlayer = Optional.of(Objects.requireNonNull(player, "player"));
91: this.firstPlayerStrategy = Optional.of(Objects.requireNonNull(strategy, "firstPlayerStrategy"));
92: } else if (this.secondPlayer.isEmpty()) {
93: this.secondPlayer = Optional.of(Objects.requireNonNull(player, "player"));
94: this.secondPlayerStrategy = Optional.of(Objects.requireNonNull(strategy, "secondPlayerStrategy"));
95: } else {
96: throw new GameException(String.format("More than two players are now allowed."));
97: }
98: return this;
99: }
100:
101: @Override
102: public GDGameBuilder changeMaximumComputationTimePerMove(final int newMaxComputationTimePerMove) {
103: this.maxComputationTimePerMove = newMaxComputationTimePerMove;
104: return this;
105: }
106:
107: @Override
108: public GDGameBuilder changeObserverFactoryProvider(
109: final ObserverFactoryProvider newObserverFactoryProvider) {
110: this.observerFactoryProvider = newObserverFactoryProvider;
111: return this;
112: }
113:
114: @Override
115: public Game<GDPlayer, GDState, GDMove,
116: GDStrategy> build(final int id)
117: throws GameException, InterruptedException {
118: if (!this.firstPlayer.isPresent() || !this.secondPlayer.isPresent()) {
119: throw new GameException("A Gefangenen-Dilemma game needs two players.");
120: }
121:
122: final GDStateImpl initialState = new GDStateImpl(this.firstPlayer.get(),
123: this.secondPlayer.get());
124:
125: final Map<String, GDStrategy> strategies = new LinkedHashMap<>();
126: strategies.put(initialState.getFirstPlayer().getName(), this.firstPlayerStrategy.orElseThrow());
127: strategies.put(initialState.getSecondPlayer().getName(), this.secondPlayerStrategy.orElseThrow());
128: return new DefaultGame<>(
129: id,
130: initialState,
131: strategies,
132: this.maxComputationTimePerMove,
133: AbstractGDMove.class::isInstance,
134: new GDMoveGeneratorImpl(),
135: this.observerFactoryProvider);
136: }
137: }