Skip to content

Method: getAllPlayableFields()

1: package de.fhdw.gaming.ipspiel21.viergewinnt.core.domain.impl;
2:
3: import java.util.ArrayList;
4: import java.util.HashMap;
5: import java.util.Iterator;
6: import java.util.LinkedHashMap;
7: import java.util.LinkedHashSet;
8: import java.util.List;
9: import java.util.ListIterator;
10: import java.util.Map;
11: import java.util.Objects;
12: import java.util.Set;
13:
14: import de.fhdw.gaming.core.domain.GameException;
15: import de.fhdw.gaming.ipspiel21.viergewinnt.core.domain.VierGewinntBoard;
16: import de.fhdw.gaming.ipspiel21.viergewinnt.core.domain.VierGewinntField;
17: import de.fhdw.gaming.ipspiel21.viergewinnt.core.domain.VierGewinntFieldState;
18: import de.fhdw.gaming.ipspiel21.viergewinnt.core.domain.VierGewinntPosition;
19: import de.fhdw.gaming.ipspiel21.viergewinnt.core.domain.VierGewinntRow;
20:
21: /**
22: * Implements {@link VierGewinntBoard}.
23: *
24: * @author Leonard Frank
25: */
26: public class VierGewinntBoardImpl implements VierGewinntBoard {
27:
28: /**
29: * Contains all rows of a {@link VierGewinntBoard}.
30: */
31: private final List<VierGewinntRow> rows;
32:
33: /**
34: * Number of rows on board.
35: */
36: private final int rowSize;
37:
38: /**
39: * Number of columns on board.
40: */
41: private final int columnSize;
42:
43: /**
44: * Creates a VierGewinntBoard.
45: *
46: * @param rowSize Number of rows on this board
47: * @param columnSize Defines the length of each row on this board
48: * @throws GameException
49: */
50: public VierGewinntBoardImpl(final int rowSize, final int columnSize)
51: throws IllegalArgumentException, GameException {
52: if (rowSize < 4 || columnSize < 4) {
53: throw new IllegalArgumentException("Row or column size is smaller than 4");
54: }
55:
56: this.columnSize = columnSize;
57: this.rowSize = rowSize;
58: this.rows = new ArrayList<>();
59: for (int rowIndex = 0; rowIndex < rowSize; rowIndex++) {
60: this.rows.add(new ViergewinntRowImpl(columnSize, rowIndex, this));
61: }
62: }
63:
64: /**
65: * Creates a board from an existing board.
66: *
67: * @param board
68: */
69: public VierGewinntBoardImpl(final VierGewinntBoard board) {
70: this.columnSize = board.getColumnSize();
71: this.rowSize = board.getRowSize();
72: this.rows = new ArrayList<>();
73: for (final VierGewinntRow row : board.getRows()) {
74: this.rows.add(row.deepCopy(this));
75: }
76: }
77:
78: @Override
79: public int getSize() {
80: return this.rowSize * this.columnSize;
81: }
82:
83: @Override
84: public int getRowSize() {
85: return this.rowSize;
86: }
87:
88: @Override
89: public int getColumnSize() {
90: return this.columnSize;
91: }
92:
93: @Override
94: public List<VierGewinntRow> getRows() {
95: return this.rows;
96: }
97:
98: @Override
99: public boolean hasFieldAt(final VierGewinntPosition position) {
100: for (final VierGewinntRow row : this.rows) {
101: for (final VierGewinntField field : row.getFields()) {
102: if (field.getPosition().equals(position)) {
103: return true;
104: }
105: }
106: }
107: return false;
108: }
109:
110: @Override
111: public VierGewinntField getFieldAt(final VierGewinntPosition position) throws IllegalArgumentException {
112: try {
113: this.rows.get(position.getRow());
114: } catch (final IndexOutOfBoundsException e) {
115: throw new IllegalArgumentException("There is no row for this position", e);
116: }
117: final VierGewinntRow requestedRow = this.rows.get(position.getRow());
118:
119: for (final VierGewinntField field : requestedRow.getFields()) {
120: if (field.getPosition().equals(position)) {
121: return field;
122: }
123: }
124: throw new IllegalArgumentException("There is no field with this position in the row");
125: }
126:
127: @Override
128: public List<VierGewinntField> getFields() {
129: final List<VierGewinntField> allFields = new ArrayList<>();
130: for (final VierGewinntRow row : this.rows) {
131: for (final VierGewinntField field : row.getFields()) {
132: allFields.add(field);
133: }
134:
135: }
136: return allFields;
137: }
138:
139: @Override
140: public Map<VierGewinntPosition, ? extends VierGewinntField>
141: getFieldsBeing(final VierGewinntFieldState fieldState) {
142: final HashMap<VierGewinntPosition, VierGewinntField> result = new HashMap<>();
143: for (final VierGewinntRow row : this.rows) {
144: for (final VierGewinntField field : row.getFields()) {
145: if (field.getState().equals(fieldState)) {
146: result.put(field.getPosition(), field);
147: }
148: }
149: }
150: return result;
151: }
152:
153: @Override
154: public Set<VierGewinntField> getAllPlayableFields() {
155: final Map<Integer, VierGewinntField> result = new LinkedHashMap<>();
156:
157: final ListIterator<VierGewinntRow> rowIt = this.rows.listIterator(this.rows.size());
158:• while (rowIt.hasPrevious()) {
159: final VierGewinntRow row = rowIt.previous();
160: final Iterator<? extends VierGewinntField> fieldIt = row.getFields().iterator();
161:• for (int column = 0; column < row.getFields().size(); ++column) {
162: final VierGewinntField field = fieldIt.next();
163:• if (result.containsKey(column)) {
164: continue;
165: }
166:• if (field.getState().equals(VierGewinntFieldState.EMPTY)) {
167: result.put(column, field);
168: }
169: }
170: }
171: return new LinkedHashSet<>(result.values());
172: }
173:
174: @Override
175: public VierGewinntBoard deepCopy() {
176: return new VierGewinntBoardImpl(this);
177: }
178:
179: @Override
180: public String toString() {
181: final StringBuilder out = new StringBuilder(" ");
182: for (final VierGewinntField field : this.rows.get(0).getFields()) {
183: out.append(field.getPosition().toString().substring(0, 1)).append(' ');
184: }
185: out.append('\n');
186: for (final VierGewinntRow row : this.rows) {
187: out.append(row.getFields().get(0).getPosition().toString().substring(1, 2)).append(' ')
188: .append(row.toString()).append('\n');
189: }
190: return out.toString();
191: }
192:
193: @Override
194: public boolean equals(final Object obj) {
195: if (obj == null || obj.getClass() != this.getClass()) {
196: return false;
197: }
198: final VierGewinntBoardImpl otherBoard = (VierGewinntBoardImpl) obj;
199: if (otherBoard.getRowSize() != this.getRowSize() || otherBoard.getColumnSize() != this.getColumnSize()) {
200: return false;
201: }
202: final Iterator<VierGewinntRow> otherRows = otherBoard.getRows().iterator();
203: final Iterator<VierGewinntRow> thisRows = this.getRows().iterator();
204: while (otherRows.hasNext() && thisRows.hasNext()) {
205: if (!(otherRows.next().equals(thisRows.next()))) {
206: return false;
207: }
208: }
209: return true;
210: }
211:
212: /**
213: * Returns HashCode of this.
214: */
215: @Override
216: public int hashCode() {
217: return Objects.hash(this.rows, this.rowSize, this.columnSize);
218: }
219:
220: }