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-VierConnects-core.
5: *
6: * ipspiel24-VierConnects-core is free software: you can redistribute it and/or modify it under
7: * the terms of the GNU General Public License as published by the Free Software
8: * Foundation, either version 3 of the License, or (at your option) any later
9: * version.
10: *
11: * ipspiel24-VierConnects-core is distributed in the hope that it will be useful, but WITHOUT
12: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14: * details.
15: *
16: * You should have received a copy of the GNU General Public License along with
17: * ipspiel24-VierConnects-core. If not, see <http://www.gnu.org/licenses/>.
18: */
19: package de.fhdw.gaming.ipspiel24.VierConnects.core.domain.impl;
20:
21: import java.util.ArrayList;
22: import java.util.LinkedHashMap;
23: import java.util.List;
24: import java.util.Map;
25: import java.util.Objects;
26: import java.util.Optional;
27:
28: import de.fhdw.gaming.core.domain.DefaultGame;
29: import de.fhdw.gaming.core.domain.Game;
30: import de.fhdw.gaming.core.domain.GameBuilder;
31: import de.fhdw.gaming.core.domain.GameException;
32: import de.fhdw.gaming.core.domain.Observer;
33: import de.fhdw.gaming.ipspiel24.VierConnects.core.domain.VierConnectsGameBuilder;
34: import de.fhdw.gaming.ipspiel24.VierConnects.core.domain.VierConnectsPlayer;
35: import de.fhdw.gaming.ipspiel24.VierConnects.core.domain.VierConnectsPlayerBuilder;
36: import de.fhdw.gaming.ipspiel24.VierConnects.core.domain.VierConnectsState;
37: import de.fhdw.gaming.ipspiel24.VierConnects.core.domain.VierConnectsStrategy;
38: import de.fhdw.gaming.ipspiel24.VierConnects.core.moves.VierConnectsMove;
39: import de.fhdw.gaming.ipspiel24.VierConnects.core.moves.impl.AbstractVierConnectsMove;
40:
41: /**
42: * Implements {@link VierConnectsGameBuilder}.
43: */
44: final class VierConnectsGameBuilderImpl implements VierConnectsGameBuilder {
45:
46: /**
47: * The {@link Observer}s to be attached to the game.
48: */
49: private final List<Observer> observers;
50:
51: /**
52: * The player using crosses.
53: */
54: private Optional<VierConnectsPlayer> crossesPlayer;
55:
56: /**
57: * The strategy of the player using crosses.
58: */
59: private Optional<VierConnectsStrategy> crossesPlayerStrategy;
60:
61: /**
62: * The player using noughts.
63: */
64: private Optional<VierConnectsPlayer> noughtsPlayer;
65:
66: /**
67: * The strategy of the player using noughts.
68: */
69: private Optional<VierConnectsStrategy> noughtsPlayerStrategy;
70:
71: /**
72: * The maximum computation time per move in seconds.
73: */
74: private int maxComputationTimePerMove;
75:
76: /**
77: * The number of rows of the board.
78: */
79: private int nrOfRows;
80:
81: /**
82: * The number of columns of the board.
83: */
84: private int nrOfColumns;
85:
86: /**
87: * Creates an VierConnects game builder.
88: */
89: VierConnectsGameBuilderImpl() {
90: this.observers = new ArrayList<>();
91: this.crossesPlayer = Optional.empty();
92: this.crossesPlayerStrategy = Optional.empty();
93: this.noughtsPlayer = Optional.empty();
94: this.noughtsPlayerStrategy = Optional.empty();
95: this.maxComputationTimePerMove = GameBuilder.DEFAULT_MAX_COMPUTATION_TIME_PER_MOVE;
96: this.nrOfRows = VierConnectsGameBuilder.DEFAULT_NR_OF_ROWS;
97: this.nrOfColumns = VierConnectsGameBuilder.DEFAULT_NR_OF_COLUMNS;
98: }
99:
100: @Override
101: public VierConnectsPlayerBuilder createPlayerBuilder() {
102: return new VierConnectsPlayerBuilderImpl();
103: }
104:
105: @Override
106: public VierConnectsGameBuilder addPlayer(final VierConnectsPlayer player, final VierConnectsStrategy strategy)
107: throws GameException {
108:
109: if (player.isUsingCrosses() && this.crossesPlayer.isEmpty()) {
110: this.crossesPlayer = Optional.of(Objects.requireNonNull(player, "player"));
111: this.crossesPlayerStrategy = Optional.of(Objects.requireNonNull(strategy, "crossesPlayerStrategy"));
112: } else if (!player.isUsingCrosses() && this.noughtsPlayer.isEmpty()) {
113: this.noughtsPlayer = Optional.of(Objects.requireNonNull(player, "player"));
114: this.noughtsPlayerStrategy = Optional.of(Objects.requireNonNull(strategy, "noughtsPlayerStrategy"));
115: } else {
116: throw new GameException(
117: String.format(
118: "Adding player %s is not allowed as a player using the same type of marks "
119: + "has already been added.",
120: player));
121: }
122: return this;
123: }
124:
125: @Override
126: public VierConnectsGameBuilder changeMaximumComputationTimePerMove(final int newMaxComputationTimePerMove) {
127: this.maxComputationTimePerMove = newMaxComputationTimePerMove;
128: return this;
129: }
130:
131: @Override
132: public VierConnectsGameBuilder changeBoardSize(final int newNrOfRows, final int newNrOfColumns) {
133: this.nrOfRows = newNrOfRows;
134: this.nrOfColumns = newNrOfColumns;
135: return this;
136: }
137:
138: @Override
139: public VierConnectsGameBuilder addObservers(final List<Observer> newObservers) {
140: this.observers.addAll(newObservers);
141: return this;
142: }
143:
144: @Override
145: public Game<VierConnectsPlayer, VierConnectsState, VierConnectsMove, VierConnectsStrategy> build(final int id)
146: throws GameException, InterruptedException {
147: if (!this.crossesPlayer.isPresent() || !this.noughtsPlayer.isPresent()) {
148: throw new GameException("An Vier Connects game needs two players.");
149: }
150:
151: final VierConnectsBoardImpl board = new VierConnectsBoardImpl(this.nrOfRows, this.nrOfColumns);
152: final VierConnectsState initialState = new VierConnectsStateImpl(
153: board,
154: this.crossesPlayer.get(),
155: this.noughtsPlayer.get(),
156: true);
157:
158: final Map<String, VierConnectsStrategy> strategies = new LinkedHashMap<>();
159: strategies.put(initialState.getCrossesPlayer().getName(), this.crossesPlayerStrategy.orElseThrow());
160: strategies.put(initialState.getNoughtsPlayer().getName(), this.noughtsPlayerStrategy.orElseThrow());
161: return new DefaultGame<>(
162: id,
163: initialState,
164: strategies,
165: this.maxComputationTimePerMove,
166: AbstractVierConnectsMove.class::isInstance,
167: new VierConnectsMoveGeneratorImpl(),
168: this.observers);
169: }
170: }