Skip to contentMethod: isPlacingBlackToken()
      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 de.fhdw.gaming.core.domain.GameException;
22: import de.fhdw.gaming.othello.core.domain.OthelloField;
23: import de.fhdw.gaming.othello.core.domain.OthelloFieldState;
24: import de.fhdw.gaming.othello.core.domain.OthelloPlayer;
25: import de.fhdw.gaming.othello.core.domain.OthelloState;
26: 
27: /**
28:  * Represents a "move" which does nothing.
29:  * <p>
30:  * Note that this move is only allowed if the player is not able to do anything else.
31:  */
32: final class OthelloSkipMove extends AbstractOthelloMove {
33: 
34:     /**
35:      * {@code true} if a black token should be placed, and {@code false} if a white token should be placed.
36:      */
37:     private final boolean placingBlackToken;
38: 
39:     /**
40:      * Creates an {@link OthelloSkipMove} object.
41:      *
42:      * @param placingBlackToken {@code true} if a black token should be placed, and {@code false} if a white token
43:      *                          should be placed.
44:      */
45:     OthelloSkipMove(final boolean placingBlackToken) {
46:         this.placingBlackToken = placingBlackToken;
47:     }
48: 
49:     /**
50:      * Returns {@code true} if a black token should be placed, and {@code false} if a white token should be placed.
51:      */
52:     boolean isPlacingBlackToken() {
53:         return this.placingBlackToken;
54:     }
55: 
56:     @Override
57:     public void applyTo(final OthelloState state, final OthelloPlayer player) throws GameException {
58:         if (this.isPlacingBlackToken() != player.isUsingBlackTokens()) {
59:             throw new GameException(
60:                     String.format(
61:                             "Player %s cannot skip a %s move.",
62:                             player,
63:                             this.isPlacingBlackToken() ? OthelloFieldState.BLACK : OthelloFieldState.WHITE));
64:         }
65: 
66:         for (final OthelloField field : state.getBoard().getFieldsBeing(OthelloFieldState.EMPTY).values()) {
67:             if (field.isActive(this.placingBlackToken)) {
68:                 final OthelloFieldState fieldState = this.placingBlackToken ? OthelloFieldState.BLACK
69:                         : OthelloFieldState.WHITE;
70:                 throw new GameException(
71:                         String.format(
72:                                 "Illegal skip move as placing a token being %s on the field at %s is possible.",
73:                                 fieldState,
74:                                 field.getPosition()));
75:             }
76:         }
77: 
78:         state.moveCompleted(true);
79:     }
80: 
81:     @Override
82:     public String toString() {
83:         return String.format("Skipping %s move", this.placingBlackToken ? "black" : "white");
84:     }
85: }