Skip to content

Method: static {...}

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.List;
22: import java.util.Map;
23: import java.util.Optional;
24: import java.util.Random;
25: import java.util.stream.Collectors;
26:
27: import de.fhdw.gaming.core.domain.DefaultGame;
28: import de.fhdw.gaming.core.domain.ObserverFactoryProvider;
29: import de.fhdw.gaming.othello.core.domain.OthelloField;
30: import de.fhdw.gaming.othello.core.domain.OthelloFieldState;
31: import de.fhdw.gaming.othello.core.domain.OthelloGame;
32: import de.fhdw.gaming.othello.core.domain.OthelloMoveChecker;
33: import de.fhdw.gaming.othello.core.domain.OthelloPlayer;
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.OthelloMove;
37: import de.fhdw.gaming.othello.core.moves.factory.OthelloMoveFactory;
38: import de.fhdw.gaming.othello.core.moves.impl.OthelloDefaultMoveFactory;
39:
40: /**
41: * Implements the Othello game.
42: */
43: final class OthelloGameImpl extends DefaultGame<OthelloPlayer, OthelloState, OthelloMove, OthelloStrategy>
44: implements OthelloGame {
45:
46: /**
47: * The random number generator used for generating random moves.
48: */
49: private static final Random RANDOM = new Random();
50:
51: /**
52: * The number of rows (and columns) of the board.
53: */
54: private final int boardSize;
55: /**
56: * The move factory.
57: */
58: private final OthelloMoveFactory moveFactory;
59:
60: /**
61: * Creates an Othello game.
62: *
63: * @param id The ID of this game.
64: * @param initialState The initial state of the game.
65: * @param strategies The players' strategies.
66: * @param maxComputationTimePerMove The maximum computation time per move in seconds.
67: * @param moveChecker The move checker.
68: * @param observerFactoryProvider The {@link ObserverFactoryProvider}.
69: * @throws IllegalArgumentException if the player sets do not match.
70: * @throws InterruptedException if creating the game has been interrupted.
71: */
72: OthelloGameImpl(final int id, final OthelloState initialState, final Map<String, OthelloStrategy> strategies,
73: final long maxComputationTimePerMove, final OthelloMoveChecker moveChecker,
74: final ObserverFactoryProvider observerFactoryProvider)
75: throws IllegalArgumentException, InterruptedException {
76:
77: super(id, initialState, strategies, maxComputationTimePerMove, moveChecker, observerFactoryProvider);
78: this.boardSize = initialState.getBoard().getSize();
79: this.moveFactory = new OthelloDefaultMoveFactory();
80: }
81:
82: @Override
83: public Optional<OthelloMove> chooseRandomMove(final OthelloPlayer player, final OthelloState state) {
84: final boolean usingBlackTokens = player.isUsingBlackTokens();
85: final List<OthelloField> fields = state.getBoard().getFieldsBeing(OthelloFieldState.EMPTY).values().stream()
86: .filter((final OthelloField field) -> field.isActive(usingBlackTokens)).collect(Collectors.toList());
87:
88: if (fields.isEmpty()) {
89: return Optional.of(this.moveFactory.createSkipMove(usingBlackTokens));
90: } else {
91: final int index = OthelloGameImpl.RANDOM.nextInt(fields.size());
92: final OthelloField field = fields.get(index);
93: return Optional.of(this.moveFactory.createPlaceTokenMove(usingBlackTokens, field.getPosition()));
94: }
95: }
96:
97: @Override
98: public String toString() {
99: return String.format("OthelloGame[id=%d, boardSize=%d, %s]", this.getId(), this.boardSize, this.gameToString());
100: }
101:
102: @Override
103: public int getBoardSize() {
104: return this.boardSize;
105: }
106: }