Skip to content

Method: getBoardSize()

1: /*
2: * Copyright © 2021 Fachhochschule für die Wirtschaft (FHDW) Hannover
3: *
4: * This file is part of ipspiel22-tictactoe-core.
5: *
6: * ipspiel22-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: * ipspiel22-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: * ipspiel22-tictactoe-core. If not, see <http://www.gnu.org/licenses/>.
18: */
19: package de.fhdw.gaming.ipspiel22.tictactoe.core.domain.impl;
20:
21: import java.util.ArrayList;
22: import java.util.List;
23: import java.util.Map;
24: import java.util.Optional;
25: import java.util.Random;
26:
27: import de.fhdw.gaming.core.domain.DefaultGame;
28: import de.fhdw.gaming.core.domain.ObserverFactoryProvider;
29: import de.fhdw.gaming.ipspiel22.tictactoe.core.domain.TicTacToeField;
30: import de.fhdw.gaming.ipspiel22.tictactoe.core.domain.TicTacToeFieldState;
31: import de.fhdw.gaming.ipspiel22.tictactoe.core.domain.TicTacToeGame;
32: import de.fhdw.gaming.ipspiel22.tictactoe.core.domain.TicTacToeMoveChecker;
33: import de.fhdw.gaming.ipspiel22.tictactoe.core.domain.TicTacToePlayer;
34: import de.fhdw.gaming.ipspiel22.tictactoe.core.domain.TicTacToeState;
35: import de.fhdw.gaming.ipspiel22.tictactoe.core.domain.TicTacToeStrategy;
36: import de.fhdw.gaming.ipspiel22.tictactoe.core.moves.TicTacToeMove;
37: import de.fhdw.gaming.ipspiel22.tictactoe.core.moves.factory.TicTacToeMoveFactory;
38: import de.fhdw.gaming.ipspiel22.tictactoe.core.moves.impl.TicTacToeDefaultMoveFactory;
39:
40: /**
41: * Implements the Tic Tac Toe game.
42: */
43: final class TicTacToeGameImpl extends DefaultGame<TicTacToePlayer, TicTacToeState, TicTacToeMove, TicTacToeStrategy>
44: implements TicTacToeGame {
45:
46: /**
47: * The random number generator.
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 TicTacToeMoveFactory moveFactory;
59:
60: /**
61: * Creates a TicTacToe 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: TicTacToeGameImpl(final int id, final TicTacToeState initialState, final Map<String, TicTacToeStrategy> strategies,
73: final long maxComputationTimePerMove, final TicTacToeMoveChecker 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 TicTacToeDefaultMoveFactory();
80: }
81:
82: @Override
83: public Optional<TicTacToeMove> chooseRandomMove(final TicTacToePlayer player, final TicTacToeState state) {
84: final List<TicTacToeField> fields = new ArrayList<>(
85: state.getBoard().getFieldsBeing(TicTacToeFieldState.EMPTY).values());
86:
87: if (fields.isEmpty()) {
88: return Optional.empty();
89: }
90: final int index = RANDOM.nextInt(fields.size());
91: final TicTacToeField field = fields.get(index);
92: return Optional.of(this.moveFactory.createPlaceMarkMove(player.isUsingCrosses(), field.getPosition()));
93: }
94:
95: @Override
96: public String toString() {
97: return String
98: .format("TicTacToeGame[id=%d, boardSize=%d, %s]", this.getId(), this.boardSize, this.gameToString());
99: }
100:
101: @Override
102: public int getBoardSize() {
103: return this.boardSize;
104: }
105: }