Package: TicTacToeGameBuilderImpl
TicTacToeGameBuilderImpl
| name | instruction | branch | complexity | line | method | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| TicTacToeGameBuilderImpl() | 
  | 
  | 
  | 
  | 
  | 
||||||||||||||||||||
| addPlayer(TicTacToePlayer, TicTacToeStrategy) | 
  | 
  | 
  | 
  | 
  | 
||||||||||||||||||||
| build(int) | 
  | 
  | 
  | 
  | 
  | 
||||||||||||||||||||
| changeBoardSize(int) | 
  | 
  | 
  | 
  | 
  | 
||||||||||||||||||||
| changeMaximumComputationTimePerMove(int) | 
  | 
  | 
  | 
  | 
  | 
||||||||||||||||||||
| changeObserverFactoryProvider(ObserverFactoryProvider) | 
  | 
  | 
  | 
  | 
  | 
||||||||||||||||||||
| createPlayerBuilder() | 
  | 
  | 
  | 
  | 
  | 
||||||||||||||||||||
Coverage
1: /*
2:  * Copyright © 2021-2023 Fachhochschule für die Wirtschaft (FHDW) Hannover
3:  *
4:  * This file is part of ipspiel24-tictactoe-core.
5:  *
6:  * ipspiel24-tictactoe-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-tictactoe-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-tictactoe-core. If not, see <http://www.gnu.org/licenses/>.
18:  */
19: package de.fhdw.gaming.ipspiel24.tictactoe.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.tictactoe.core.domain.TicTacToeGameBuilder;
33: import de.fhdw.gaming.ipspiel24.tictactoe.core.domain.TicTacToePlayer;
34: import de.fhdw.gaming.ipspiel24.tictactoe.core.domain.TicTacToePlayerBuilder;
35: import de.fhdw.gaming.ipspiel24.tictactoe.core.domain.TicTacToeState;
36: import de.fhdw.gaming.ipspiel24.tictactoe.core.domain.TicTacToeStrategy;
37: import de.fhdw.gaming.ipspiel24.tictactoe.core.moves.TicTacToeMove;
38: import de.fhdw.gaming.ipspiel24.tictactoe.core.moves.impl.AbstractTicTacToeMove;
39: 
40: /**
41:  * Implements {@link TicTacToeGameBuilder}.
42:  */
43: final class TicTacToeGameBuilderImpl implements TicTacToeGameBuilder {
44: 
45:     /**
46:      * The {@link ObserverFactoryProvider}.
47:      */
48:     private ObserverFactoryProvider observerFactoryProvider;
49: 
50:     /**
51:      * The player using crosses.
52:      */
53:     private Optional<TicTacToePlayer> crossesPlayer;
54: 
55:     /**
56:      * The strategy of the player using crosses.
57:      */
58:     private Optional<TicTacToeStrategy> crossesPlayerStrategy;
59: 
60:     /**
61:      * The player using noughts.
62:      */
63:     private Optional<TicTacToePlayer> noughtsPlayer;
64: 
65:     /**
66:      * The strategy of the player using noughts.
67:      */
68:     private Optional<TicTacToeStrategy> noughtsPlayerStrategy;
69: 
70:     /**
71:      * The maximum computation time per move in seconds.
72:      */
73:     private int maxComputationTimePerMove;
74: 
75:     /**
76:      * The number of rows (and columns) of the board.
77:      */
78:     private int boardSize;
79: 
80:     /**
81:      * Creates an TicTacToe game builder.
82:      */
83:     TicTacToeGameBuilderImpl() {
84:         this.observerFactoryProvider = new DefaultObserverFactoryProvider();
85:         this.crossesPlayer = Optional.empty();
86:         this.crossesPlayerStrategy = Optional.empty();
87:         this.noughtsPlayer = Optional.empty();
88:         this.noughtsPlayerStrategy = Optional.empty();
89:         this.maxComputationTimePerMove = GameBuilder.DEFAULT_MAX_COMPUTATION_TIME_PER_MOVE;
90:         this.boardSize = TicTacToeGameBuilder.DEFAULT_BOARD_SIZE;
91:     }
92: 
93:     @Override
94:     public TicTacToePlayerBuilder createPlayerBuilder() {
95:         return new TicTacToePlayerBuilderImpl();
96:     }
97: 
98:     @Override
99:     public TicTacToeGameBuilder addPlayer(final TicTacToePlayer player, final TicTacToeStrategy strategy)
100:             throws GameException {
101: 
102:•        if (player.isUsingCrosses() && this.crossesPlayer.isEmpty()) {
103:             this.crossesPlayer = Optional.of(Objects.requireNonNull(player, "player"));
104:             this.crossesPlayerStrategy = Optional.of(Objects.requireNonNull(strategy, "crossesPlayerStrategy"));
105:•        } else if (!player.isUsingCrosses() && this.noughtsPlayer.isEmpty()) {
106:             this.noughtsPlayer = Optional.of(Objects.requireNonNull(player, "player"));
107:             this.noughtsPlayerStrategy = Optional.of(Objects.requireNonNull(strategy, "noughtsPlayerStrategy"));
108:         } else {
109:             throw new GameException(
110:                     String.format(
111:                             "Adding player %s is not allowed as a player using the same type of marks "
112:                                     + "has already been added.",
113:                             player));
114:         }
115:         return this;
116:     }
117: 
118:     @Override
119:     public TicTacToeGameBuilder changeMaximumComputationTimePerMove(final int newMaxComputationTimePerMove) {
120:         this.maxComputationTimePerMove = newMaxComputationTimePerMove;
121:         return this;
122:     }
123: 
124:     @Override
125:     public TicTacToeGameBuilder changeBoardSize(final int newBoardSize) {
126:         this.boardSize = newBoardSize;
127:         return this;
128:     }
129: 
130:     @Override
131:     public TicTacToeGameBuilder changeObserverFactoryProvider(
132:             final ObserverFactoryProvider newObserverFactoryProvider) {
133:         this.observerFactoryProvider = newObserverFactoryProvider;
134:         return this;
135:     }
136: 
137:     @Override
138:     public Game<TicTacToePlayer, TicTacToeState, TicTacToeMove, TicTacToeStrategy> build(final int id)
139:             throws GameException, InterruptedException {
140:•        if (!this.crossesPlayer.isPresent() || !this.noughtsPlayer.isPresent()) {
141:             throw new GameException("An Tic Tac Toe game needs two players.");
142:         }
143: 
144:         final TicTacToeBoardImpl board = new TicTacToeBoardImpl(this.boardSize);
145:         final TicTacToeState initialState = new TicTacToeStateImpl(
146:                 board,
147:                 this.crossesPlayer.get(),
148:                 this.noughtsPlayer.get(),
149:                 true);
150: 
151:         final Map<String, TicTacToeStrategy> strategies = new LinkedHashMap<>();
152:         strategies.put(initialState.getCrossesPlayer().getName(), this.crossesPlayerStrategy.orElseThrow());
153:         strategies.put(initialState.getNoughtsPlayer().getName(), this.noughtsPlayerStrategy.orElseThrow());
154:         return new DefaultGame<>(
155:                 id,
156:                 initialState,
157:                 strategies,
158:                 this.maxComputationTimePerMove,
159:                 AbstractTicTacToeMove.class::isInstance,
160:                 new TicTacToeMoveGeneratorImpl(),
161:                 this.observerFactoryProvider);
162:     }
163: }