Skip to content

Method: build(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.LinkedHashMap;
22: import java.util.Map;
23: import java.util.Objects;
24: import java.util.Optional;
25:
26: import de.fhdw.gaming.core.domain.DefaultGame;
27: import de.fhdw.gaming.core.domain.DefaultObserverFactoryProvider;
28: import de.fhdw.gaming.core.domain.Game;
29: import de.fhdw.gaming.core.domain.GameBuilder;
30: import de.fhdw.gaming.core.domain.GameException;
31: import de.fhdw.gaming.core.domain.ObserverFactoryProvider;
32: import de.fhdw.gaming.ipspiel24.VierConnects.core.domain.VierConnectsGameBuilder;
33: import de.fhdw.gaming.ipspiel24.VierConnects.core.domain.VierConnectsPlayer;
34: import de.fhdw.gaming.ipspiel24.VierConnects.core.domain.VierConnectsPlayerBuilder;
35: import de.fhdw.gaming.ipspiel24.VierConnects.core.domain.VierConnectsState;
36: import de.fhdw.gaming.ipspiel24.VierConnects.core.domain.VierConnectsStrategy;
37: import de.fhdw.gaming.ipspiel24.VierConnects.core.moves.VierConnectsMove;
38: import de.fhdw.gaming.ipspiel24.VierConnects.core.moves.impl.AbstractVierConnectsMove;
39:
40: /**
41: * Implements {@link VierConnectsGameBuilder}.
42: */
43: final class VierConnectsGameBuilderImpl implements VierConnectsGameBuilder {
44:
45: /**
46: * The {@link ObserverFactoryProvider}.
47: */
48: private ObserverFactoryProvider observerFactoryProvider;
49:
50: /**
51: * The player using crosses.
52: */
53: private Optional<VierConnectsPlayer> crossesPlayer;
54:
55: /**
56: * The strategy of the player using crosses.
57: */
58: private Optional<VierConnectsStrategy> crossesPlayerStrategy;
59:
60: /**
61: * The player using noughts.
62: */
63: private Optional<VierConnectsPlayer> noughtsPlayer;
64:
65: /**
66: * The strategy of the player using noughts.
67: */
68: private Optional<VierConnectsStrategy> noughtsPlayerStrategy;
69:
70: /**
71: * The maximum computation time per move in seconds.
72: */
73: private int maxComputationTimePerMove;
74:
75: /**
76: * The number of rows of the board.
77: */
78: private int nrOfRows;
79:
80: /**
81: * The number of columns of the board.
82: */
83: private int nrOfColumns;
84:
85: /**
86: * Creates an VierConnects game builder.
87: */
88: VierConnectsGameBuilderImpl() {
89: this.observerFactoryProvider = new DefaultObserverFactoryProvider();
90: this.crossesPlayer = Optional.empty();
91: this.crossesPlayerStrategy = Optional.empty();
92: this.noughtsPlayer = Optional.empty();
93: this.noughtsPlayerStrategy = Optional.empty();
94: this.maxComputationTimePerMove = GameBuilder.DEFAULT_MAX_COMPUTATION_TIME_PER_MOVE;
95: this.nrOfRows = VierConnectsGameBuilder.DEFAULT_NR_OF_ROWS;
96: this.nrOfColumns = VierConnectsGameBuilder.DEFAULT_NR_OF_COLUMNS;
97: }
98:
99: @Override
100: public VierConnectsPlayerBuilder createPlayerBuilder() {
101: return new VierConnectsPlayerBuilderImpl();
102: }
103:
104: @Override
105: public VierConnectsGameBuilder addPlayer(final VierConnectsPlayer player, final VierConnectsStrategy strategy)
106: throws GameException {
107:
108: if (player.isUsingCrosses() && this.crossesPlayer.isEmpty()) {
109: this.crossesPlayer = Optional.of(Objects.requireNonNull(player, "player"));
110: this.crossesPlayerStrategy = Optional.of(Objects.requireNonNull(strategy, "crossesPlayerStrategy"));
111: } else if (!player.isUsingCrosses() && this.noughtsPlayer.isEmpty()) {
112: this.noughtsPlayer = Optional.of(Objects.requireNonNull(player, "player"));
113: this.noughtsPlayerStrategy = Optional.of(Objects.requireNonNull(strategy, "noughtsPlayerStrategy"));
114: } else {
115: throw new GameException(
116: String.format(
117: "Adding player %s is not allowed as a player using the same type of marks "
118: + "has already been added.",
119: player));
120: }
121: return this;
122: }
123:
124: @Override
125: public VierConnectsGameBuilder changeMaximumComputationTimePerMove(final int newMaxComputationTimePerMove) {
126: this.maxComputationTimePerMove = newMaxComputationTimePerMove;
127: return this;
128: }
129:
130: @Override
131: public VierConnectsGameBuilder changeBoardSize(final int newNrOfRows, final int newNrOfColumns) {
132: this.nrOfRows = newNrOfRows;
133: this.nrOfColumns = newNrOfColumns;
134: return this;
135: }
136:
137: @Override
138: public VierConnectsGameBuilder changeObserverFactoryProvider(
139: final ObserverFactoryProvider newObserverFactoryProvider) {
140: this.observerFactoryProvider = newObserverFactoryProvider;
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.observerFactoryProvider);
169: }
170: }