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-demo.
5: *
6: * Ipspiel24-demo 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-demo 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-demo. If not, see
14: * <http://www.gnu.org/licenses/>.
15: */
16: package de.fhdw.gaming.ipspiel24.demo.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.core.domain.DefaultGame;
26: import de.fhdw.gaming.core.domain.Game;
27: import de.fhdw.gaming.core.domain.GameBuilder;
28: import de.fhdw.gaming.core.domain.GameException;
29: import de.fhdw.gaming.core.domain.Observer;
30: import de.fhdw.gaming.ipspiel24.demo.domain.DemoGameBuilder;
31: import de.fhdw.gaming.ipspiel24.demo.domain.DemoPlayer;
32: import de.fhdw.gaming.ipspiel24.demo.domain.DemoPlayerBuilder;
33: import de.fhdw.gaming.ipspiel24.demo.domain.DemoState;
34: import de.fhdw.gaming.ipspiel24.demo.domain.DemoStrategy;
35: import de.fhdw.gaming.ipspiel24.demo.moves.DemoMove;
36: import de.fhdw.gaming.ipspiel24.demo.moves.impl.AbstractDemoMove;
37:
38: /**
39: * Implements {@link DemoGameBuilder}.
40: */
41: final class DemoGameBuilderImpl implements DemoGameBuilder {
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<DemoPlayer> firstPlayer;
51: /**
52: * The strategy of the player using black tokens.
53: */
54: private Optional<DemoStrategy> firstPlayerStrategy;
55: /**
56: * The player using white tokens.
57: */
58: private Optional<DemoPlayer> secondPlayer;
59: /**
60: * The strategy of the player using white tokens.
61: */
62: private Optional<DemoStrategy> secondPlayerStrategy;
63: /**
64: * The maximum computation time per move in seconds.
65: */
66: private int maxComputationTimePerMove;
67:
68: /**
69: * Creates a Demo game builder.
70: */
71: DemoGameBuilderImpl() {
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 DemoPlayerBuilder createPlayerBuilder() {
82: return new DemoPlayerBuilderImpl();
83: }
84:
85: @Override
86: public DemoGameBuilder addPlayer(final DemoPlayer player, final DemoStrategy 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 DemoGameBuilder changeMaximumComputationTimePerMove(final int newMaxComputationTimePerMove) {
103: this.maxComputationTimePerMove = newMaxComputationTimePerMove;
104: return this;
105: }
106:
107: @Override
108: public DemoGameBuilder addObservers(final List<Observer> newObservers) {
109: this.observers.addAll(newObservers);
110: return this;
111: }
112:
113: @Override
114: public Game<DemoPlayer, DemoState, DemoMove, DemoStrategy> build(final int id)
115: throws GameException, InterruptedException {
116: if (!this.firstPlayer.isPresent() || !this.secondPlayer.isPresent()) {
117: throw new GameException("A Demo game needs two players.");
118: }
119:
120: final DemoStateImpl initialState = new DemoStateImpl(this.firstPlayer.get(), this.secondPlayer.get());
121:
122: final Map<String, DemoStrategy> strategies = new LinkedHashMap<>();
123: strategies.put(initialState.getFirstPlayer().getName(), this.firstPlayerStrategy.orElseThrow());
124: strategies.put(initialState.getSecondPlayer().getName(), this.secondPlayerStrategy.orElseThrow());
125: return new DefaultGame<>(
126: id,
127: initialState,
128: strategies,
129: this.maxComputationTimePerMove,
130: AbstractDemoMove.class::isInstance,
131: new DemoMoveGeneratorImpl(),
132: this.observers);
133: }
134: }