Skip to content

Package: VGBoardImpl

VGBoardImpl

nameinstructionbranchcomplexitylinemethod
VGBoardImpl()
M: 0 C: 88
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 18
100%
M: 0 C: 1
100%
VGBoardImpl(VGBoardImpl)
M: 0 C: 108
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 21
100%
M: 0 C: 1
100%
deepCopy()
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
equals(Object)
M: 2 C: 12
86%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 3
75%
M: 0 C: 1
100%
fieldChangedState(VGFieldImpl, VGFieldState)
M: 0 C: 21
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getColumns()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getFieldAt(VGPosition)
M: 12 C: 15
56%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 2
67%
M: 0 C: 1
100%
getFields()
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getFieldsBeing(VGFieldState)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getNextField(Integer)
M: 0 C: 25
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
getNextFieldInColumn(VGAnswerEnum)
M: 0 C: 59
100%
M: 0 C: 12
100%
M: 0 C: 7
100%
M: 0 C: 13
100%
M: 0 C: 1
100%
getRows()
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
hasFieldAt(VGPosition)
M: 0 C: 26
100%
M: 3 C: 5
63%
M: 3 C: 2
40%
M: 0 C: 5
100%
M: 0 C: 1
100%
hashCode()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$getFields$0(List, List)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
toString()
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

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.ArrayList;
22: import java.util.Collections;
23: import java.util.LinkedHashMap;
24: import java.util.List;
25: import java.util.Map;
26: import java.util.Objects;
27:
28: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGAnswerEnum;
29: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGBoard;
30: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGField;
31: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGFieldState;
32: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGPosition;
33:
34: /**
35: * Implements {@link VGBoard}.
36: */
37: public final class VGBoardImpl implements VGBoard {
38:
39: /**
40: * The number of columns.
41: */
42: private static final int BOARD_COLUMNS = 7;
43:
44: /**
45: * The number of rows.
46: */
47: private static final int BOARD_ROWS = 6;
48:
49: /**
50: * The fields of the board. Column by Column
51: */
52: private final List<List<VGFieldImpl>> fields;
53: /**
54: * The fields sorted by state.
55: */
56: private final Map<VGFieldState, Map<VGPosition, VGFieldImpl>> fieldsByState;
57:
58: /**
59: * Creates an VG board.
60: *
61: * @throws IllegalArgumentException if the size does not meet the requirements.
62: */
63: public VGBoardImpl() throws IllegalArgumentException {
64: final int rows = BOARD_ROWS;
65: final int columns = BOARD_COLUMNS;
66:
67: this.fieldsByState = new LinkedHashMap<>();
68: final Map<VGPosition, VGFieldImpl> emptyFields = new LinkedHashMap<>();
69: this.fieldsByState.put(VGFieldState.EMPTY, emptyFields);
70: this.fieldsByState.put(VGFieldState.RED, new LinkedHashMap<>());
71: this.fieldsByState.put(VGFieldState.YELLOW, new LinkedHashMap<>());
72:
73: this.fields = new ArrayList<>(columns);
74:• for (int columnIndex = 0; columnIndex < columns; ++columnIndex) {
75: final List<VGFieldImpl> column = new ArrayList<>(rows);
76:• for (int rowIndex = 0; rowIndex < rows; ++rowIndex) {
77: final VGPosition position = VGPosition.of(columnIndex, rowIndex);
78: final VGFieldImpl field = new VGFieldImpl(this, position, VGFieldState.EMPTY);
79: column.add(field);
80: emptyFields.put(position, field);
81: }
82: this.fields.add(column);
83: }
84: }
85:
86: /**
87: * Copies an VG board.
88: *
89: * @param source The board to copy.
90: */
91: public VGBoardImpl(final VGBoardImpl source) {
92: Objects.requireNonNull(source, "source");
93:
94: this.fieldsByState = new LinkedHashMap<>();
95: this.fieldsByState.put(VGFieldState.EMPTY, new LinkedHashMap<>());
96: this.fieldsByState.put(VGFieldState.RED, new LinkedHashMap<>());
97: this.fieldsByState.put(VGFieldState.YELLOW, new LinkedHashMap<>());
98:
99: final int rows = source.getRows();
100: final int columns = source.getColumns();
101: this.fields = new ArrayList<>(columns);
102:• for (int columnIndex = 0; columnIndex < columns; ++columnIndex) {
103: final List<VGFieldImpl> originColumn = source.fields.get(columnIndex);
104: final List<VGFieldImpl> column = new ArrayList<>(rows);
105:• for (int rowIndex = 0; rowIndex < rows; ++rowIndex) {
106: final VGFieldImpl originField = originColumn.get(rowIndex);
107: final VGFieldImpl field = new VGFieldImpl(
108: this,
109: VGPosition.of(columnIndex, rowIndex),
110: originField.getState());
111: column.add(field);
112: this.fieldsByState.get(field.getState()).put(field.getPosition(), field);
113: }
114: this.fields.add(column);
115: }
116: }
117:
118: @Override
119: public String toString() {
120: return String.format("VGBoard[size=%d, fields=%s]", this.fields.size(), this.fields);
121: }
122:
123: @Override
124: public boolean equals(final Object obj) {
125:• if (obj instanceof VGBoardImpl) {
126: final VGBoardImpl other = (VGBoardImpl) obj;
127: return this.fields.equals(other.fields);
128: }
129: return false;
130: }
131:
132: @Override
133: public int hashCode() {
134: return this.fields.hashCode();
135: }
136:
137: @Override
138: public boolean hasFieldAt(final VGPosition position) {
139: final int rowSize = this.getRows();
140: final int columnSize = this.getColumns();
141: final int row = position.getRow();
142: final int column = position.getColumn();
143:• return row >= 0 && row < rowSize && column >= 0 && column < columnSize;
144: }
145:
146: @Override
147: public VGField getFieldAt(final VGPosition position) {
148:• if (!this.hasFieldAt(position)) {
149: throw new IllegalArgumentException(String.format("Position %s out of range.", position));
150: }
151: return this.fields.get(position.getColumn()).get(position.getRow());
152: }
153:
154: @Override
155: public List<List<? extends VGField>> getFields() {
156: final List<List<? extends VGField>> result = new ArrayList<>();
157: this.fields.forEach((final List<VGFieldImpl> column) -> result.add(new ArrayList<>(column)));
158: return result;
159: }
160:
161: @Override
162: public Map<VGPosition, VGField> getFieldsBeing(final VGFieldState fieldState) {
163: return Collections.unmodifiableMap(this.fieldsByState.get(fieldState));
164: }
165:
166: @Override
167: public VGBoardImpl deepCopy() {
168: return new VGBoardImpl(this);
169: }
170:
171: /**
172: * This operation is called by a {@link VGFieldImpl} when a field changes its state.
173: *
174: * @param field The field that changed its state.
175: * @param oldState The old state of the field.
176: */
177: void fieldChangedState(final VGFieldImpl field, final VGFieldState oldState) {
178: this.fieldsByState.get(oldState).remove(field.getPosition());
179: this.fieldsByState.get(field.getState()).put(field.getPosition(), field);
180: }
181:
182: @Override
183: public int getRows() {
184: return this.fields.get(0).size();
185: }
186:
187: @Override
188: public int getColumns() {
189: return this.fields.size();
190: }
191:
192: @Override
193: public VGField getNextFieldInColumn(final VGAnswerEnum column) {
194:• if (column.equals(VGAnswerEnum.FIRSTCOLUMN)) {
195: return getNextField(0);
196:• } else if (column.equals(VGAnswerEnum.SECONDCOLUMN)) {
197: return getNextField(1);
198:• } else if (column.equals(VGAnswerEnum.THIRDCOLUMN)) {
199: return getNextField(2);
200:• } else if (column.equals(VGAnswerEnum.FOURTHCOLUMN)) {
201: return getNextField(3);
202:• } else if (column.equals(VGAnswerEnum.FITFHCOLUMN)) {
203: return getNextField(4);
204:• } else if (column.equals(VGAnswerEnum.SIXTHCOLUMN)) {
205: return getNextField(5);
206: } else {
207: return getNextField(6);
208: }
209: }
210:
211: /**
212: * This operation is called in getNextFieldInColumn.
213: *
214: * @param indexInList Index der Spalte der fields-Liste.
215: */
216: private VGField getNextField(final Integer indexInList) {
217:• for (final VGFieldImpl field : fields.get(indexInList)) {
218:• if (field.getState().equals(VGFieldState.EMPTY)) {
219: return field;
220: }
221: }
222: return null;
223: }
224: }