Skip to content

Method: changeState(VierGewinntFieldState)

1: package de.fhdw.gaming.ipspiel21.viergewinnt.core.domain.impl;
2:
3: import de.fhdw.gaming.core.domain.GameException;
4: import de.fhdw.gaming.ipspiel21.viergewinnt.core.domain.VierGewinntBoard;
5: import de.fhdw.gaming.ipspiel21.viergewinnt.core.domain.VierGewinntDirection;
6: import de.fhdw.gaming.ipspiel21.viergewinnt.core.domain.VierGewinntField;
7: import de.fhdw.gaming.ipspiel21.viergewinnt.core.domain.VierGewinntFieldState;
8: import de.fhdw.gaming.ipspiel21.viergewinnt.core.domain.VierGewinntPosition;
9:
10: /**
11: * The Implementation of a VierGewinnt Field.
12: *
13: * @author Robby Rabbitman
14: *
15: */
16: public class VierGewinntFieldImpl implements VierGewinntField {
17:
18: /**
19: * State of this field.
20: *
21: */
22: private VierGewinntFieldState state;
23: /**
24: * Position of this field.
25: */
26: private final VierGewinntPosition position;
27: /**
28: * Board of this Field.
29: */
30: private final VierGewinntBoard board;
31:
32: /**
33: * Empty field.
34: *
35: * @param position
36: * @param board
37: * @throws GameException
38: */
39: public VierGewinntFieldImpl(final VierGewinntPosition position, final VierGewinntBoard board)
40: throws GameException {
41: super();
42: if (board.getFields().stream().map(VierGewinntField::getPosition).filter(p -> p.equals(position)).findAny()
43: .isPresent()) {
44: throw new GameException("Position already used!");
45: }
46: this.board = board;
47: this.position = position;
48: this.state = VierGewinntFieldState.EMPTY;
49: }
50:
51: /**
52: * Copy of source.
53: *
54: * @param source The field to copy.
55: * @param board The board to associate with this field.
56: */
57: public VierGewinntFieldImpl(final VierGewinntFieldImpl source, final VierGewinntBoard board) {
58: this.board = board;
59: this.position = source.getPosition();
60: this.state = source.getState();
61: }
62:
63: @Override
64: public VierGewinntBoard getBoard() {
65: return this.board;
66: }
67:
68: @Override
69: public VierGewinntPosition getPosition() {
70: return this.position;
71: }
72:
73: @Override
74: public VierGewinntFieldState getState() {
75: return this.state;
76: }
77:
78: @Override
79: public boolean hasNeighbour(final VierGewinntDirection direction) {
80: try {
81: this.getBoard().getFieldAt(direction.step(this.getPosition()));
82: return true;
83: } catch (final IllegalArgumentException e) {
84: return false;
85: }
86:
87: }
88:
89: @Override
90: public VierGewinntField getNeighbour(final VierGewinntDirection direction) throws IllegalArgumentException {
91: return this.getBoard().getFieldAt(direction.step(this.getPosition()));
92: }
93:
94: @Override
95: public void changeState(final VierGewinntFieldState newState) throws GameException {
96: this.state = newState;
97: }
98:
99: @Override
100: public VierGewinntField deepCopy(final VierGewinntBoard target) {
101: return new VierGewinntFieldImpl(this, target);
102: }
103:
104: @Override
105: public String toString() {
106: return "[state=" + this.state + ", position=" + this.position + "]";
107: }
108:
109: @Override
110: public boolean equals(final Object obj) {
111: if (obj == null || obj.getClass() != this.getClass()) {
112: return false;
113: } else if (obj == this) {
114: return true;
115: }
116:
117: final VierGewinntFieldImpl otherField = (VierGewinntFieldImpl) obj;
118:
119: if (otherField.getBoard().getRowSize() != this.getBoard().getRowSize()
120: || otherField.getBoard().getColumnSize() != this.getBoard().getColumnSize()) {
121: return false;
122: }
123:
124: return otherField.getPosition().equals(this.getPosition()) && otherField.getState() == this.state;
125:
126: }
127:
128: /**
129: * Hashcode of this.
130: */
131: @Override
132: public int hashCode() {
133: return this.position.hashCode();
134: }
135: }