Skip to content

Method: hasNeighbour(TicTacToeDirection)

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