Skip to content

Method: checkVertical(VGFieldState)

1: package de.fhdw.gaming.ipspiel22.vierGewinnt.domain.impl;
2:
3: import java.util.ArrayList;
4: import java.util.Collections;
5: import java.util.LinkedHashMap;
6: import java.util.List;
7: import java.util.Map;
8: import java.util.Objects;
9: import java.util.Set;
10:
11: import de.fhdw.gaming.core.domain.GameException;
12: import de.fhdw.gaming.core.domain.PlayerState;
13: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGBoard;
14: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGField;
15: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGFieldState;
16: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGPlayer;
17: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGState;
18:
19: /**
20: * Implements {@link VGState}.
21: */
22: @SuppressWarnings("PMD.GodClass")
23: public class VGStateImpl implements VGState {
24:
25: /**
26: * The board.
27: */
28: private final VGBoard board;
29: /**
30: * The first player.
31: */
32: private final VGPlayer redPlayer;
33: /**
34: * The second player.
35: */
36: private final VGPlayer yellowPlayer;
37: /**
38: * The current player.
39: */
40: private VGPlayer currentPlayer;
41:
42: /**
43: * Creates a Vier gewinnt state.
44: *
45: * @param board A board.s
46: * @param redPlayer The first player.
47: * @param yellowPlayer The second player.
48: * @param redIsNext Is red next.
49: * @throws GameException if the state cannot be created according to the rules of the game.
50: */
51: public VGStateImpl(final VGBoard board, final VGPlayer redPlayer, final VGPlayer yellowPlayer,
52: final boolean redIsNext)
53: throws GameException {
54:
55: this.board = Objects.requireNonNull(board, "board");
56: this.redPlayer = Objects.requireNonNull(redPlayer, "redPlayerBuilder");
57: this.yellowPlayer = Objects.requireNonNull(yellowPlayer, "yellowPlayerBuilder");
58: this.currentPlayer = redIsNext ? this.redPlayer : this.yellowPlayer;
59:
60: if (!this.redPlayer.isUsingRedChips()) {
61: throw new IllegalArgumentException(
62: String.format("Red player %s does not use red chips.", this.redPlayer));
63: }
64: if (this.yellowPlayer.isUsingRedChips()) {
65: throw new IllegalArgumentException(
66: String.format("Yellow player %s does not use yellow tokens.", this.yellowPlayer));
67: }
68: }
69:
70: /**
71: * Creates a Vier gewinnt state by copying an existing one.
72: *
73: * @param source The state to copy.
74: */
75: VGStateImpl(final VGStateImpl source) {
76: this.board = source.board.deepCopy();
77: this.redPlayer = source.redPlayer.deepCopy();
78: this.yellowPlayer = source.yellowPlayer.deepCopy();
79: this.currentPlayer = source.isRedPlayerCurrent() ? this.redPlayer : this.yellowPlayer;
80: }
81:
82: @Override
83: public String toString() {
84: return String.format(
85: "VGState[board=%s, redPlayer=%s, yellowPlayer=%s, currentPlayer=%s]",
86: this.board,
87: this.redPlayer,
88: this.yellowPlayer,
89: this.currentPlayer.isUsingRedChips() ? VGFieldState.RED : VGFieldState.YELLOW);
90: }
91:
92: @Override
93: public boolean equals(final Object obj) {
94: if (obj instanceof VGStateImpl) {
95: final VGStateImpl other = (VGStateImpl) obj;
96: return this.board.equals(other.board)
97: && this.redPlayer.equals(other.redPlayer)
98: && this.yellowPlayer.equals(other.yellowPlayer)
99: && this.isRedPlayerCurrent() == other.isRedPlayerCurrent();
100: }
101: return false;
102: }
103:
104: @Override
105: public VGState deepCopy() {
106: return new VGStateImpl(this);
107: }
108:
109: @Override
110: public int hashCode() {
111: return Objects.hash(this.board, this.redPlayer, this.yellowPlayer, this.isRedPlayerCurrent());
112: }
113:
114: @Override
115: public Map<String, VGPlayer> getPlayers() {
116: final Map<String, VGPlayer> result = new LinkedHashMap<>();
117: result.put(this.redPlayer.getName(), this.redPlayer);
118: result.put(this.yellowPlayer.getName(), this.yellowPlayer);
119: return result;
120: }
121:
122: @Override
123: public Set<VGPlayer> computeNextPlayers() {
124: return Collections.singleton(this.currentPlayer);
125: }
126:
127: @Override
128: public void nextTurn() {
129: final List<VGField> allFields = new ArrayList<>();
130: this.getBoard().getFields().forEach(allFields::addAll);
131:
132: if (allFields.stream().noneMatch(vgField -> vgField.getState().equals(VGFieldState.EMPTY))) {
133:
134: System.out.println("Board ist voll");
135:
136: if (this.checkWinner(this.currentPlayer)) {
137: this.currentPlayer.setState(PlayerState.WON);
138: sysoWinner();
139: this.getOtherPlayer().setState(PlayerState.LOST);
140: sysoLooser();
141: } else {
142: this.gameOver();
143: }
144: } else if (this.checkWinner(this.currentPlayer)) {
145: this.currentPlayer.setState(PlayerState.WON);
146: sysoWinner();
147: this.getOtherPlayer().setState(PlayerState.LOST);
148: sysoLooser();
149: }
150: this.currentPlayer = this.getOtherPlayer();
151: }
152:
153: /**
154: * Prints Looser.
155: */
156: private void sysoLooser() {
157: System.out.println(this.getOtherPlayer().getName() + " hat verloren, mit der Farbe: "
158: + (this.currentPlayer.isUsingRedChips()
159: ? VGFieldState.YELLOW.toString()
160: : VGFieldState.RED.toString()));
161: }
162:
163: /**
164: * Prints Winner.
165: */
166: private void sysoWinner() {
167: System.out.println(this.currentPlayer.getName() + " hat gewonnen, mit der Farbe: "
168: + (this.currentPlayer.isUsingRedChips()
169: ? VGFieldState.RED.toString()
170: : VGFieldState.YELLOW.toString()));
171: }
172:
173: /**
174: * Returns boolean, if Player has won.
175: *
176: * @param player The current player.
177: */
178: public boolean checkWinner(final VGPlayer player) {
179: VGFieldState state = VGFieldState.YELLOW;
180: if (player.isUsingRedChips()) {
181: state = VGFieldState.RED;
182: }
183: return this.checkVertical(state);
184: }
185:
186: /**
187:• * Returns boolean, if Player has won.
188:• *
189:• * @param state The chips color of the current player.
190:• */
191:• private Boolean checkVertical(final VGFieldState state) {
192:• for (int i = 0; i < this.board.getColumns(); i++) {
193: for (int j = 0; j < 3; j++) {
194: if (this.board.getFields().get(i).get(j).getState().equals(state)
195: && this.board.getFields().get(i).get(j + 1).getState().equals(state)
196: && this.board.getFields().get(i).get(j + 2).getState().equals(state)
197: && this.board.getFields().get(i).get(j + 3).getState().equals(state)) {
198: System.out.println(
199: "Vertikal: " + this.board.getFields().get(i).get(j).getPosition()
200: + this.board.getFields().get(i).get(j + 1).getPosition()
201: + this.board.getFields().get(i).get(j + 2).getPosition()
202: + this.board.getFields().get(i).get(j + 3).getPosition());
203: return true;
204: }
205: }
206: }
207: return this.checkHorizontal(state);
208: }
209:
210: /**
211: * Returns boolean, if Player has won.
212: *
213: * @param state The chips color of the current player.
214: */
215: private Boolean checkHorizontal(final VGFieldState state) {
216: for (int i = 0; i < this.board.getRows(); i++) { // Für Zeilen
217: for (int j = 0; j < 4; j++) { // Für Spalten
218: if (this.board.getFields().get(j).get(i).getState().equals(state)
219: && this.board.getFields().get(j + 1).get(i).getState().equals(state)
220: && this.board.getFields().get(j + 2).get(i).getState().equals(state)
221: && this.board.getFields().get(j + 3).get(i).getState().equals(state)) {
222: System.out.println(
223: "Horizontal: " + this.board.getFields().get(j).get(i).getPosition()
224: + this.board.getFields().get(j + 1).get(i).getPosition()
225: + this.board.getFields().get(j + 2).get(i).getPosition()
226: + this.board.getFields().get(j + 3).get(i).getPosition());
227: return true;
228: }
229: }
230: }
231: return this.checkDiagonalLeftToRight(state);
232: }
233:
234: /**
235: * Returns boolean, if Player has won.
236: *
237: * @param state The chips color of the current player.
238: */
239: private Boolean checkDiagonalLeftToRight(final VGFieldState state) {
240: for (int i = 0; i < 3; i++) {
241: for (int j = 0; j < 4; j++) {
242: if (this.board.getFields().get(j).get(i).getState().equals(state)
243: && this.board.getFields().get(j + 1).get(i + 1).getState().equals(state)
244: && this.board.getFields().get(j + 2).get(i + 2).getState().equals(state)
245: && this.board.getFields().get(j + 3).get(i + 3).getState().equals(state)) {
246: System.out.println("Diagonal von links nach rechts: "
247: + this.board.getFields().get(j).get(i).getPosition()
248: + this.board.getFields().get(j + 1).get(i + 1).getPosition()
249: + this.board.getFields().get(j + 2).get(i + 2).getPosition()
250: + this.board.getFields().get(j + 3).get(i + 3).getPosition());
251: return true;
252: }
253: }
254: }
255: return this.checkDiagonalRightToLeft(state);
256: }
257:
258: /**
259: * Returns boolean, if Player has won.
260: *
261: * @param state The chips color of the current player.
262: */
263: private Boolean checkDiagonalRightToLeft(final VGFieldState state) {
264: for (int i = 0; i < 3; i++) {
265: for (int j = 3; j < 7; j++) {
266: if (this.board.getFields().get(j).get(i).getState().equals(state)
267: && this.board.getFields().get(j - 1).get(i + 1).getState().equals(state)
268: && this.board.getFields().get(j - 2).get(i + 2).getState().equals(state)
269: && this.board.getFields().get(j - 3).get(i + 3).getState().equals(state)) {
270: System.out.println("Diagonal von links nach rechts: "
271: + this.board.getFields().get(j).get(i).getPosition()
272: + this.board.getFields().get(j - 1).get(i + 1).getPosition()
273: + this.board.getFields().get(j - 2).get(i + 2).getPosition()
274: + this.board.getFields().get(j - 3).get(i + 3).getPosition());
275: return true;
276: }
277: }
278: }
279: return false;
280: }
281:
282: @Override
283: public VGBoard getBoard() {
284: return this.board;
285: }
286:
287: @Override
288: public VGPlayer getRedPlayer() {
289: return this.redPlayer;
290: }
291:
292: @Override
293: public VGPlayer getYellowPlayer() {
294: return this.yellowPlayer;
295: }
296:
297: @Override
298: public VGPlayer getCurrentPlayer() {
299: return this.currentPlayer;
300: }
301:
302: /**
303: * Returns the currently inactive player.
304: */
305: private VGPlayer getOtherPlayer() {
306: if (this.isRedPlayerCurrent()) {
307: return this.yellowPlayer;
308: }
309: return this.redPlayer;
310: }
311:
312: /**
313: * Returns id the current player is the red Player.
314: */
315: private boolean isRedPlayerCurrent() {
316: return this.currentPlayer.equals(this.redPlayer);
317: }
318:
319: @Override
320: public void gameOver() {
321: this.redPlayer.setState(PlayerState.DRAW);
322: this.yellowPlayer.setState(PlayerState.DRAW);
323: System.out.println("Es ist untenschieden. ");
324: }
325: }