Skip to content

Method: createPlayerBuilder()

1: /*
2: * Copyright © 2020 Fachhochschule für die Wirtschaft (FHDW) Hannover
3: *
4: * This file is part of othello-core.
5: *
6: * Othello-core is free software: you can redistribute it and/or modify
7: * it under the terms of the GNU General Public License as published by
8: * the Free Software Foundation, either version 3 of the License, or
9: * (at your option) any later version.
10: *
11: * Othello-core is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: * GNU General Public License for more details.
15: *
16: * You should have received a copy of the GNU General Public License
17: * along with othello-core. If not, see <http://www.gnu.org/licenses/>.
18: */
19: package de.fhdw.gaming.othello.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.DefaultObserverFactoryProvider;
27: import de.fhdw.gaming.core.domain.GameBuilder;
28: import de.fhdw.gaming.core.domain.GameException;
29: import de.fhdw.gaming.core.domain.ObserverFactoryProvider;
30: import de.fhdw.gaming.othello.core.domain.OthelloGame;
31: import de.fhdw.gaming.othello.core.domain.OthelloGameBuilder;
32: import de.fhdw.gaming.othello.core.domain.OthelloPlayer;
33: import de.fhdw.gaming.othello.core.domain.OthelloPlayerBuilder;
34: import de.fhdw.gaming.othello.core.domain.OthelloState;
35: import de.fhdw.gaming.othello.core.domain.OthelloStrategy;
36: import de.fhdw.gaming.othello.core.moves.impl.AbstractOthelloMove;
37:
38: /**
39: * Implements {@link OthelloGameBuilder}.
40: */
41: final class OthelloGameBuilderImpl implements OthelloGameBuilder {
42:
43: /**
44: * The {@link ObserverFactoryProvider}.
45: */
46: private ObserverFactoryProvider observerFactoryProvider;
47: /**
48: * The player using black tokens.
49: */
50: private Optional<OthelloPlayer> blackPlayer;
51: /**
52: * The strategy of the player using black tokens.
53: */
54: private Optional<OthelloStrategy> blackPlayerStrategy;
55: /**
56: * The player using white tokens.
57: */
58: private Optional<OthelloPlayer> whitePlayer;
59: /**
60: * The strategy of the player using white tokens.
61: */
62: private Optional<OthelloStrategy> whitePlayerStrategy;
63: /**
64: * The maximum computation time per move in seconds.
65: */
66: private int maxComputationTimePerMove;
67: /**
68: * The number of rows (and columns) of the board.
69: */
70: private int boardSize;
71:
72: /**
73: * Creates an Othello game builder.
74: */
75: OthelloGameBuilderImpl() {
76: this.observerFactoryProvider = new DefaultObserverFactoryProvider();
77: this.blackPlayer = Optional.empty();
78: this.blackPlayerStrategy = Optional.empty();
79: this.whitePlayer = Optional.empty();
80: this.whitePlayerStrategy = Optional.empty();
81: this.maxComputationTimePerMove = GameBuilder.DEFAULT_MAX_COMPUTATION_TIME_PER_MOVE;
82: this.boardSize = OthelloGameBuilder.DEFAULT_BOARD_SIZE;
83: }
84:
85: @Override
86: public OthelloPlayerBuilder createPlayerBuilder() {
87: return new OthelloPlayerBuilderImpl();
88: }
89:
90: @Override
91: public OthelloGameBuilder addPlayer(final OthelloPlayer player, final OthelloStrategy strategy)
92: throws GameException {
93:
94: if (player.isUsingBlackTokens() && this.blackPlayer.isEmpty()) {
95: this.blackPlayer = Optional.of(Objects.requireNonNull(player, "player"));
96: this.blackPlayerStrategy = Optional.of(Objects.requireNonNull(strategy, "blackPlayerStrategy"));
97: } else if (!player.isUsingBlackTokens() && this.whitePlayer.isEmpty()) {
98: this.whitePlayer = Optional.of(Objects.requireNonNull(player, "player"));
99: this.whitePlayerStrategy = Optional.of(Objects.requireNonNull(strategy, "whitePlayerStrategy"));
100: } else {
101: throw new GameException(
102: String.format(
103: "Adding player %s is not allowed as a player using the same tokens has already been added.",
104: player));
105: }
106: return this;
107: }
108:
109: @Override
110: public OthelloGameBuilder changeMaximumComputationTimePerMove(final int newMaxComputationTimePerMove) {
111: this.maxComputationTimePerMove = newMaxComputationTimePerMove;
112: return this;
113: }
114:
115: @Override
116: public OthelloGameBuilder changeBoardSize(final int newBoardSize) {
117: this.boardSize = newBoardSize;
118: return this;
119: }
120:
121: @Override
122: public OthelloGameBuilder changeObserverFactoryProvider(final ObserverFactoryProvider newObserverFactoryProvider) {
123: this.observerFactoryProvider = newObserverFactoryProvider;
124: return this;
125: }
126:
127: @Override
128: public OthelloGame build(final int id) throws GameException, InterruptedException {
129: if (!this.blackPlayer.isPresent() || !this.whitePlayer.isPresent()) {
130: throw new GameException("An Othello game needs two players.");
131: }
132:
133: final OthelloBoardImpl board = new OthelloBoardImpl(this.boardSize);
134: final OthelloState initialState = new OthelloStateImpl(
135: board,
136: this.blackPlayer.get(),
137: this.whitePlayer.get(),
138: true);
139:
140: final Map<String, OthelloStrategy> strategies = new LinkedHashMap<>();
141: strategies.put(initialState.getBlackPlayer().getName(), this.blackPlayerStrategy.orElseThrow());
142: strategies.put(initialState.getWhitePlayer().getName(), this.whitePlayerStrategy.orElseThrow());
143: return new OthelloGameImpl(
144: id,
145: initialState,
146: strategies,
147: this.maxComputationTimePerMove,
148: AbstractOthelloMove.class::isInstance,
149: this.observerFactoryProvider);
150: }
151: }