Skip to contentMethod: toFieldState(boolean)
      1: /*
2:  * Copyright © 2021-2023 Fachhochschule für die Wirtschaft (FHDW) Hannover
3:  *
4:  * This file is part of ipspiel24-tictactoe-core.
5:  *
6:  * ipspiel24-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:  * ipspiel24-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:  * ipspiel24-tictactoe-core. If not, see <http://www.gnu.org/licenses/>.
18:  */
19: package de.fhdw.gaming.ipspiel24.tictactoe.core.moves.impl;
20: 
21: import java.util.Objects;
22: 
23: import de.fhdw.gaming.core.domain.GameException;
24: import de.fhdw.gaming.ipspiel24.tictactoe.core.domain.TicTacToeFieldState;
25: import de.fhdw.gaming.ipspiel24.tictactoe.core.domain.TicTacToePlayer;
26: import de.fhdw.gaming.ipspiel24.tictactoe.core.domain.TicTacToePosition;
27: import de.fhdw.gaming.ipspiel24.tictactoe.core.domain.TicTacToeState;
28: 
29: /**
30:  * Represents a move which places a mark on a field on the board.
31:  * <p>
32:  * Note that this move is only allowed if the field is empty.
33:  */
34: final class TicTacToePlaceMarkMove extends AbstractTicTacToeMove {
35: 
36:     /**
37:      * {@code true} if a cross is to be made, {@code false} chooses a nought instead.
38:      */
39:     private final boolean cross;
40:     /**
41:      * The position of the token placed on the board.
42:      */
43:     private final TicTacToePosition tokenPosition;
44: 
45:     /**
46:      * Creates an {@link TicTacToePlaceMarkMove} object.
47:      *
48:      * @param cross         {@code true} if a cross is to be made, {@code false} chooses a nought instead.
49:      * @param tokenPosition The position of the field having its state changed. It must point to an empty field.
50:      */
51:     TicTacToePlaceMarkMove(final boolean cross, final TicTacToePosition tokenPosition) {
52:         this.cross = cross;
53:         this.tokenPosition = Objects.requireNonNull(tokenPosition, "tokenPosition");
54:     }
55: 
56:     /**
57:      * Returns {@code true} if a cross is to be made, and {@code false} if a nought is to be made instead.
58:      */
59:     boolean isCross() {
60:         return this.cross;
61:     }
62: 
63:     /**
64:      * Returns the position of the token placed on the board.
65:      */
66:     TicTacToePosition getTokenPosition() {
67:         return this.tokenPosition;
68:     }
69: 
70:     @Override
71:     public void applyTo(final TicTacToeState state, final TicTacToePlayer player) throws GameException {
72:         if (this.cross != player.isUsingCrosses()) {
73:             throw new GameException(
74:                     String.format(
75:                             "Player %s cannot make %s on field at %s.",
76:                             player,
77:                             toFieldState(this.cross),
78:                             this.tokenPosition));
79:         }
80: 
81:         state.getBoard().getFieldAt(this.tokenPosition).changeState(toFieldState(this.cross));
82:         state.moveCompleted();
83:     }
84: 
85:     @Override
86:     public String toString() {
87:         return String.format("%s on field at %s", toFieldState(this.cross), this.tokenPosition);
88:     }
89: 
90:     /**
91:      * Maps a cross/nought boolean to a target {@link TicTacToeFieldState}.
92:      *
93:      * @param cross {@code true} for a cross and {@code false} for a nought.
94:      */
95:     private static TicTacToeFieldState toFieldState(final boolean cross) {
96:•        return cross ? TicTacToeFieldState.CROSS : TicTacToeFieldState.NOUGHT;
97:     }
98: }