Skip to content

Method: placeToken(boolean)

1: /*
2: * Copyright © 2020 Fachhochschule für die Wirtschaft (FHDW) Hannover
3: *
4: * This file is part of VG-core.
5: *
6: * VG-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: * VG-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 VG-core. If not, see <http://www.gnu.org/licenses/>.
18: */
19: package de.fhdw.gaming.ipspiel22.vierGewinnt.domain.impl;
20:
21: import java.util.Objects;
22:
23: import de.fhdw.gaming.core.domain.GameException;
24: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGBoard;
25: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGField;
26: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGFieldState;
27: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGPosition;
28:
29: /**
30: * Implements {@link VGField}.
31: */
32: final class VGFieldImpl implements VGField {
33:
34: /**
35: * The board this field belongs to.
36: */
37: private final VGBoardImpl board;
38: /**
39: * The position at which this field is placed on the board.
40: */
41: private final VGPosition position;
42: /**
43: * The state of this field.
44: */
45: private VGFieldState state;
46:
47: /**
48: * Creates an VG field.
49: *
50: * @param board The board this field belongs to.
51: * @param position The position at which this field is placed on the board.
52: * @param state The state of this field.
53: */
54: VGFieldImpl(final VGBoardImpl board, final VGPosition position, final VGFieldState state) {
55: this.board = Objects.requireNonNull(board, "board");
56: this.position = Objects.requireNonNull(position, "position");
57: this.state = state;
58: }
59:
60: @Override
61: public String toString() {
62: return String.format("VGField[position=%s, state=%s]", this.position, this.state);
63: }
64:
65: /**
66: * {@inheritDoc}
67: * <p>
68: * Does not compare the boards the fields belong to, respectively.
69: */
70: @Override
71: public boolean equals(final Object obj) {
72: if (obj instanceof VGFieldImpl) {
73: final VGFieldImpl other = (VGFieldImpl) obj;
74: return this.position.equals(other.position) && this.state.equals(other.state);
75: }
76: return false;
77: }
78:
79: @Override
80: public int hashCode() {
81: return Objects.hash(this.position, this.state);
82: }
83:
84: @Override
85: public VGBoard getBoard() {
86: return this.board;
87: }
88:
89: @Override
90: public VGPosition getPosition() {
91: return this.position;
92: }
93:
94: @Override
95: public VGFieldState getState() {
96: return this.state;
97: }
98:
99: /**
100: * Sets the current state of this field. Does not change the states of neighbour fields.
101: *
102: * @param newState The new state.
103: * @throws IllegalArgumentException if the new state is {@link VGFieldState#EMPTY} and different from the
104: * previous state.
105: */
106: @Override
107: public void setState(final VGFieldState newState) throws IllegalArgumentException {
108: final VGFieldState oldState = this.state;
109: if (oldState.equals(newState)) {
110: // nothing to do
111: return;
112: }
113:
114: // final boolean wasEmpty = oldState.equals(VGFieldState.EMPTY);
115: // final boolean isEmpty = newState.equals(VGFieldState.EMPTY);
116: //
117: // if (!wasEmpty && isEmpty) {
118: // throw new IllegalArgumentException(
119: // String.format("The field at %s cannot become empty again.", this.position));
120: // }
121:
122: this.state = newState;
123: this.board.fieldChangedState(this, oldState);
124: }
125:
126: @Override
127: public void placeToken(final boolean redChip) throws GameException {
128:• final VGFieldState newState = redChip ? VGFieldState.RED : VGFieldState.YELLOW;
129: this.setState(newState);
130: }
131: }