Skip to content

Method: OthelloPlaceTokenMove(boolean, OthelloPosition)

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 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: * Othello-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: * othello-core. If not, see <http://www.gnu.org/licenses/>.
18: */
19: package de.fhdw.gaming.othello.core.moves.impl;
20:
21: import java.util.Objects;
22:
23: import de.fhdw.gaming.core.domain.GameException;
24: import de.fhdw.gaming.othello.core.domain.OthelloFieldState;
25: import de.fhdw.gaming.othello.core.domain.OthelloPlayer;
26: import de.fhdw.gaming.othello.core.domain.OthelloPosition;
27: import de.fhdw.gaming.othello.core.domain.OthelloState;
28:
29: /**
30: * Represents a move which places a token on the board.
31: * <p>
32: * Note that this move is only allowed if the colour of the token placed belongs to the corresponding player, and if the
33: * move leads to at least one other token to be flipped.
34: */
35: final class OthelloPlaceTokenMove extends AbstractOthelloMove {
36:
37: /**
38: * {@code true} if a black token is placed, and {@code false} if a white token is placed.
39: */
40: private final boolean placingBlackToken;
41: /**
42: * The position of the token placed on the board.
43: */
44: private final OthelloPosition tokenPosition;
45:
46: /**
47: * Creates an {@link OthelloPlaceTokenMove} object.
48: *
49: * @param placingBlackToken {@code true} if a black token is placed, and {@code false} if a white token is placed.
50: * @param tokenPosition The position of the token placed on the board.
51: */
52: OthelloPlaceTokenMove(final boolean placingBlackToken, final OthelloPosition tokenPosition) {
53: this.placingBlackToken = placingBlackToken;
54: this.tokenPosition = Objects.requireNonNull(tokenPosition, "tokenPosition");
55: }
56:
57: /**
58: * Returns {@code true} if a black token is placed, and {@code false} if a white token is placed.
59: */
60: boolean isPlacingBlackToken() {
61: return this.placingBlackToken;
62: }
63:
64: /**
65: * Returns the position of the token placed on the board.
66: */
67: OthelloPosition getTokenPosition() {
68: return this.tokenPosition;
69: }
70:
71: @Override
72: public void applyTo(final OthelloState state, final OthelloPlayer player) throws GameException {
73: if (this.isPlacingBlackToken() != player.isUsingBlackTokens()) {
74: throw new GameException(
75: String.format(
76: "Player %s cannot place a %s token.",
77: player,
78: this.isPlacingBlackToken() ? OthelloFieldState.BLACK : OthelloFieldState.WHITE));
79: }
80:
81: state.getBoard().getFieldAt(this.tokenPosition).placeToken(this.placingBlackToken);
82: state.moveCompleted(false);
83: }
84:
85: @Override
86: public String toString() {
87: return String.format(
88: "Placing %s token on field at %s",
89: this.placingBlackToken ? "black" : "white",
90: this.tokenPosition);
91: }
92: }