Skip to contentMethod: createPlayerBuilder()
      1: /*
2:  * Copyright © 2021-2023 Fachhochschule für die Wirtschaft (FHDW) Hannover
3:  *
4:  * This file is part of ipspiel23-demo.
5:  *
6:  * Ipspiel23-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:  * Ipspiel23-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 ipspiel23-demo. If not, see
14:  * <http://www.gnu.org/licenses/>.
15:  */
16: package de.fhdw.gaming.ipspiel23.demo.domain.impl;
17: 
18: import java.util.LinkedHashMap;
19: import java.util.Map;
20: import java.util.Objects;
21: import java.util.Optional;
22: 
23: import de.fhdw.gaming.core.domain.DefaultObserverFactoryProvider;
24: import de.fhdw.gaming.core.domain.GameBuilder;
25: import de.fhdw.gaming.core.domain.GameException;
26: import de.fhdw.gaming.core.domain.ObserverFactoryProvider;
27: import de.fhdw.gaming.ipspiel23.demo.domain.DemoGame;
28: import de.fhdw.gaming.ipspiel23.demo.domain.DemoGameBuilder;
29: import de.fhdw.gaming.ipspiel23.demo.domain.DemoPlayer;
30: import de.fhdw.gaming.ipspiel23.demo.domain.DemoPlayerBuilder;
31: import de.fhdw.gaming.ipspiel23.demo.domain.DemoStrategy;
32: import de.fhdw.gaming.ipspiel23.demo.moves.impl.AbstractDemoMove;
33: 
34: /**
35:  * Implements {@link DemoGameBuilder}.
36:  */
37: final class DemoGameBuilderImpl implements DemoGameBuilder {
38: 
39:     /**
40:      * The {@link ObserverFactoryProvider}.
41:      */
42:     private ObserverFactoryProvider observerFactoryProvider;
43:     /**
44:      * The player using black tokens.
45:      */
46:     private Optional<DemoPlayer> firstPlayer;
47:     /**
48:      * The strategy of the player using black tokens.
49:      */
50:     private Optional<DemoStrategy> firstPlayerStrategy;
51:     /**
52:      * The player using white tokens.
53:      */
54:     private Optional<DemoPlayer> secondPlayer;
55:     /**
56:      * The strategy of the player using white tokens.
57:      */
58:     private Optional<DemoStrategy> secondPlayerStrategy;
59:     /**
60:      * The maximum computation time per move in seconds.
61:      */
62:     private int maxComputationTimePerMove;
63: 
64:     /**
65:      * Creates a Demo game builder.
66:      */
67:     DemoGameBuilderImpl() {
68:         this.observerFactoryProvider = new DefaultObserverFactoryProvider();
69:         this.firstPlayer = Optional.empty();
70:         this.firstPlayerStrategy = Optional.empty();
71:         this.secondPlayer = Optional.empty();
72:         this.secondPlayerStrategy = Optional.empty();
73:         this.maxComputationTimePerMove = GameBuilder.DEFAULT_MAX_COMPUTATION_TIME_PER_MOVE;
74:     }
75: 
76:     @Override
77:     public DemoPlayerBuilder createPlayerBuilder() {
78:         return new DemoPlayerBuilderImpl();
79:     }
80: 
81:     @Override
82:     public DemoGameBuilder addPlayer(final DemoPlayer player, final DemoStrategy strategy)
83:             throws GameException {
84: 
85:         if (this.firstPlayer.isEmpty()) {
86:             this.firstPlayer = Optional.of(Objects.requireNonNull(player, "player"));
87:             this.firstPlayerStrategy = Optional.of(Objects.requireNonNull(strategy, "firstPlayerStrategy"));
88:         } else if (this.secondPlayer.isEmpty()) {
89:             this.secondPlayer = Optional.of(Objects.requireNonNull(player, "player"));
90:             this.secondPlayerStrategy = Optional.of(Objects.requireNonNull(strategy, "secondPlayerStrategy"));
91:         } else {
92:             throw new GameException(String.format("More than two players are now allowed."));
93:         }
94:         return this;
95:     }
96: 
97:     @Override
98:     public DemoGameBuilder changeMaximumComputationTimePerMove(final int newMaxComputationTimePerMove) {
99:         this.maxComputationTimePerMove = newMaxComputationTimePerMove;
100:         return this;
101:     }
102: 
103:     @Override
104:     public DemoGameBuilder changeObserverFactoryProvider(final ObserverFactoryProvider newObserverFactoryProvider) {
105:         this.observerFactoryProvider = newObserverFactoryProvider;
106:         return this;
107:     }
108: 
109:     @Override
110:     public DemoGame build(final int id) throws GameException, InterruptedException {
111:         if (!this.firstPlayer.isPresent() || !this.secondPlayer.isPresent()) {
112:             throw new GameException("A Demo game needs two players.");
113:         }
114: 
115:         final DemoStateImpl initialState = new DemoStateImpl(this.firstPlayer.get(), this.secondPlayer.get());
116: 
117:         final Map<String, DemoStrategy> strategies = new LinkedHashMap<>();
118:         strategies.put(initialState.getFirstPlayer().getName(), this.firstPlayerStrategy.orElseThrow());
119:         strategies.put(initialState.getSecondPlayer().getName(), this.secondPlayerStrategy.orElseThrow());
120:         return new DemoGameImpl(
121:             id,
122:             initialState,
123:             strategies,
124:             this.maxComputationTimePerMove,
125:             AbstractDemoMove.class::isInstance,
126:             this.observerFactoryProvider);
127:     }
128: }