Skip to content

Method: changeMaximumComputationTimePerMove(int)

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.ArrayList;
19: import java.util.LinkedHashMap;
20: import java.util.List;
21: import java.util.Map;
22: import java.util.Objects;
23: import java.util.Optional;
24:
25: import de.fhdw.gaming.GefangenenDilemma.domain.GDGameBuilder;
26: import de.fhdw.gaming.GefangenenDilemma.domain.GDPlayer;
27: import de.fhdw.gaming.GefangenenDilemma.domain.GDPlayerBuilder;
28: import de.fhdw.gaming.GefangenenDilemma.domain.GDState;
29: import de.fhdw.gaming.GefangenenDilemma.domain.GDStrategy;
30: import de.fhdw.gaming.GefangenenDilemma.moves.GDMove;
31: import de.fhdw.gaming.GefangenenDilemma.moves.impl.AbstractGDMove;
32: import de.fhdw.gaming.core.domain.DefaultGame;
33: import de.fhdw.gaming.core.domain.Game;
34: import de.fhdw.gaming.core.domain.GameBuilder;
35: import de.fhdw.gaming.core.domain.GameException;
36: import de.fhdw.gaming.core.domain.Observer;
37:
38: /**
39: * Implements {@link GDGameBuilder}.
40: */
41: final class GDGameBuilderImpl implements GDGameBuilder {
42:
43: /**
44: * The {@link Observer}s to be attached to the game.
45: */
46: private final List<Observer> observers;
47: /**
48: * The player using black tokens.
49: */
50: private Optional<GDPlayer> firstPlayer;
51: /**
52: * The strategy of the player using black tokens.
53: */
54: private Optional<GDStrategy> firstPlayerStrategy;
55: /**
56: * The player using white tokens.
57: */
58: private Optional<GDPlayer> secondPlayer;
59: /**
60: * The strategy of the player using white tokens.
61: */
62: private Optional<GDStrategy> secondPlayerStrategy;
63: /**
64: * The maximum computation time per move in seconds.
65: */
66: private int maxComputationTimePerMove;
67:
68: /**
69: * Creates a Gefangenen-Dilemma game builder.
70: */
71: GDGameBuilderImpl() {
72: this.observers = new ArrayList<>();
73: this.firstPlayer = Optional.empty();
74: this.firstPlayerStrategy = Optional.empty();
75: this.secondPlayer = Optional.empty();
76: this.secondPlayerStrategy = Optional.empty();
77: this.maxComputationTimePerMove = GameBuilder.DEFAULT_MAX_COMPUTATION_TIME_PER_MOVE;
78: }
79:
80: @Override
81: public GDPlayerBuilder createPlayerBuilder() {
82: return new GDPlayerBuilderImpl();
83: }
84:
85: @Override
86: public GDGameBuilder addPlayer(final GDPlayer player,
87: final GDStrategy strategy)
88: throws GameException {
89:
90: if (this.firstPlayer.isEmpty()) {
91: this.firstPlayer = Optional.of(Objects.requireNonNull(player, "player"));
92: this.firstPlayerStrategy = Optional.of(Objects.requireNonNull(strategy, "firstPlayerStrategy"));
93: } else if (this.secondPlayer.isEmpty()) {
94: this.secondPlayer = Optional.of(Objects.requireNonNull(player, "player"));
95: this.secondPlayerStrategy = Optional.of(Objects.requireNonNull(strategy, "secondPlayerStrategy"));
96: } else {
97: throw new GameException(String.format("More than two players are now allowed."));
98: }
99: return this;
100: }
101:
102: @Override
103: public GDGameBuilder changeMaximumComputationTimePerMove(final int newMaxComputationTimePerMove) {
104: this.maxComputationTimePerMove = newMaxComputationTimePerMove;
105: return this;
106: }
107:
108: @Override
109: public GDGameBuilder addObservers(final List<Observer> newObservers) {
110: this.observers.addAll(newObservers);
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.observers);
136: }
137: }