Skip to content

Method: checkDiagonalRightToLeft(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: if (this.redPlayer.getName().equals(this.yellowPlayer.getName())) {
69: throw new IllegalArgumentException(
70: String.format("Both players have the same name '%s'.", this.redPlayer.getName()));
71: }
72: }
73:
74: /**
75: * Creates a Vier gewinnt state by copying an existing one.
76: *
77: * @param source The state to copy.
78: */
79: VGStateImpl(final VGStateImpl source) {
80: this.board = source.board.deepCopy();
81: this.redPlayer = source.redPlayer.deepCopy();
82: this.yellowPlayer = source.yellowPlayer.deepCopy();
83: this.currentPlayer = source.isRedPlayerCurrent() ? this.redPlayer : this.yellowPlayer;
84: }
85:
86: @Override
87: public String toString() {
88: return String.format(
89: "VGState[board=%s, redPlayer=%s, yellowPlayer=%s, currentPlayer=%s]",
90: this.board,
91: this.redPlayer,
92: this.yellowPlayer,
93: this.currentPlayer.isUsingRedChips() ? VGFieldState.RED : VGFieldState.YELLOW);
94: }
95:
96: @Override
97: public boolean equals(final Object obj) {
98: if (obj instanceof VGStateImpl) {
99: final VGStateImpl other = (VGStateImpl) obj;
100: return this.board.equals(other.board)
101: && this.redPlayer.equals(other.redPlayer)
102: && this.yellowPlayer.equals(other.yellowPlayer)
103: && this.isRedPlayerCurrent() == other.isRedPlayerCurrent();
104: }
105: return false;
106: }
107:
108: @Override
109: public VGState deepCopy() {
110: return new VGStateImpl(this);
111: }
112:
113: @Override
114: public int hashCode() {
115: return Objects.hash(this.board, this.redPlayer, this.yellowPlayer, this.isRedPlayerCurrent());
116: }
117:
118: @Override
119: public Map<String, VGPlayer> getPlayers() {
120: final Map<String, VGPlayer> result = new LinkedHashMap<>();
121: result.put(this.redPlayer.getName(), this.redPlayer);
122: result.put(this.yellowPlayer.getName(), this.yellowPlayer);
123: return result;
124: }
125:
126: @Override
127: public Set<VGPlayer> computeNextPlayers() {
128: return Collections.singleton(this.currentPlayer);
129: }
130:
131: @Override
132: public void nextTurn() {
133: final List<VGField> allFields = new ArrayList<>();
134: this.getBoard().getFields().forEach(allFields::addAll);
135:
136: if (allFields.stream().noneMatch(vgField -> vgField.getState().equals(VGFieldState.EMPTY))) {
137:
138: System.out.println("Board ist voll");
139:
140: if (this.checkWinner(this.currentPlayer)) {
141: this.currentPlayer.setState(PlayerState.WON);
142: sysoWinner();
143: this.getOtherPlayer().setState(PlayerState.LOST);
144: sysoLooser();
145: } else {
146: this.gameOver();
147: }
148: } else if (this.checkWinner(this.currentPlayer)) {
149: this.currentPlayer.setState(PlayerState.WON);
150: sysoWinner();
151: this.getOtherPlayer().setState(PlayerState.LOST);
152: sysoLooser();
153: }
154: this.currentPlayer = this.getOtherPlayer();
155: }
156:
157: /**
158: * Prints Looser.
159: */
160: private void sysoLooser() {
161: System.out.println(this.getOtherPlayer().getName() + " hat verloren, mit der Farbe: "
162: + (this.currentPlayer.isUsingRedChips()
163: ? VGFieldState.YELLOW.toString()
164: : VGFieldState.RED.toString()));
165: }
166:
167: /**
168: * Prints Winner.
169: */
170: private void sysoWinner() {
171: System.out.println(this.currentPlayer.getName() + " hat gewonnen, mit der Farbe: "
172: + (this.currentPlayer.isUsingRedChips()
173: ? VGFieldState.RED.toString()
174: : VGFieldState.YELLOW.toString()));
175: }
176:
177: /**
178: * Returns boolean, if Player has won.
179: *
180: * @param player The current player.
181: */
182: public boolean checkWinner(final VGPlayer player) {
183: VGFieldState state = VGFieldState.YELLOW;
184: if (player.isUsingRedChips()) {
185: state = VGFieldState.RED;
186: }
187: return this.checkVertical(state);
188: }
189:
190: /**
191: * Returns boolean, if Player has won.
192: *
193: * @param state The chips color of the current player.
194: */
195: private Boolean checkVertical(final VGFieldState state) {
196: for (int i = 0; i < this.board.getColumns(); i++) {
197: for (int j = 0; j < 3; j++) {
198: if (this.board.getFields().get(i).get(j).getState().equals(state)
199: && this.board.getFields().get(i).get(j + 1).getState().equals(state)
200: && this.board.getFields().get(i).get(j + 2).getState().equals(state)
201: && this.board.getFields().get(i).get(j + 3).getState().equals(state)) {
202: System.out.println(
203: "Vertikal: " + this.board.getFields().get(i).get(j).getPosition()
204: + this.board.getFields().get(i).get(j + 1).getPosition()
205: + this.board.getFields().get(i).get(j + 2).getPosition()
206: + this.board.getFields().get(i).get(j + 3).getPosition());
207: return true;
208: }
209: }
210: }
211: return this.checkHorizontal(state);
212: }
213:
214: /**
215: * Returns boolean, if Player has won.
216: *
217: * @param state The chips color of the current player.
218: */
219: private Boolean checkHorizontal(final VGFieldState state) {
220: for (int i = 0; i < this.board.getRows(); i++) { // Für Zeilen
221: for (int j = 0; j < 4; j++) { // Für Spalten
222: if (this.board.getFields().get(j).get(i).getState().equals(state)
223: && this.board.getFields().get(j + 1).get(i).getState().equals(state)
224: && this.board.getFields().get(j + 2).get(i).getState().equals(state)
225: && this.board.getFields().get(j + 3).get(i).getState().equals(state)) {
226: System.out.println(
227: "Horizontal: " + this.board.getFields().get(j).get(i).getPosition()
228: + this.board.getFields().get(j + 1).get(i).getPosition()
229: + this.board.getFields().get(j + 2).get(i).getPosition()
230: + this.board.getFields().get(j + 3).get(i).getPosition());
231: return true;
232: }
233: }
234: }
235: return this.checkDiagonalLeftToRight(state);
236: }
237:
238: /**
239: * Returns boolean, if Player has won.
240: *
241: * @param state The chips color of the current player.
242: */
243: private Boolean checkDiagonalLeftToRight(final VGFieldState state) {
244: for (int i = 0; i < 3; i++) {
245: for (int j = 0; j < 4; j++) {
246: if (this.board.getFields().get(j).get(i).getState().equals(state)
247: && this.board.getFields().get(j + 1).get(i + 1).getState().equals(state)
248: && this.board.getFields().get(j + 2).get(i + 2).getState().equals(state)
249: && this.board.getFields().get(j + 3).get(i + 3).getState().equals(state)) {
250: System.out.println("Diagonal von links nach rechts: "
251: + this.board.getFields().get(j).get(i).getPosition()
252: + this.board.getFields().get(j + 1).get(i + 1).getPosition()
253: + this.board.getFields().get(j + 2).get(i + 2).getPosition()
254: + this.board.getFields().get(j + 3).get(i + 3).getPosition());
255: return true;
256: }
257: }
258: }
259:• return this.checkDiagonalRightToLeft(state);
260:• }
261:•
262:• /**
263:• * Returns boolean, if Player has won.
264:• *
265: * @param state The chips color of the current player.
266: */
267: private Boolean checkDiagonalRightToLeft(final VGFieldState state) {
268: for (int i = 0; i < 3; i++) {
269: for (int j = 3; j < 7; j++) {
270: if (this.board.getFields().get(j).get(i).getState().equals(state)
271: && this.board.getFields().get(j - 1).get(i + 1).getState().equals(state)
272: && this.board.getFields().get(j - 2).get(i + 2).getState().equals(state)
273: && this.board.getFields().get(j - 3).get(i + 3).getState().equals(state)) {
274: System.out.println("Diagonal von links nach rechts: "
275: + this.board.getFields().get(j).get(i).getPosition()
276: + this.board.getFields().get(j - 1).get(i + 1).getPosition()
277: + this.board.getFields().get(j - 2).get(i + 2).getPosition()
278: + this.board.getFields().get(j - 3).get(i + 3).getPosition());
279: return true;
280: }
281: }
282: }
283: return false;
284: }
285:
286: @Override
287: public VGBoard getBoard() {
288: return this.board;
289: }
290:
291: @Override
292: public VGPlayer getRedPlayer() {
293: return this.redPlayer;
294: }
295:
296: @Override
297: public VGPlayer getYellowPlayer() {
298: return this.yellowPlayer;
299: }
300:
301: @Override
302: public VGPlayer getCurrentPlayer() {
303: return this.currentPlayer;
304: }
305:
306: /**
307: * Returns the currently inactive player.
308: */
309: private VGPlayer getOtherPlayer() {
310: if (this.isRedPlayerCurrent()) {
311: return this.yellowPlayer;
312: }
313: return this.redPlayer;
314: }
315:
316: /**
317: * Returns id the current player is the red Player.
318: */
319: private boolean isRedPlayerCurrent() {
320: return this.currentPlayer.equals(this.redPlayer);
321: }
322:
323: @Override
324: public void gameOver() {
325: this.redPlayer.setState(PlayerState.DRAW);
326: this.yellowPlayer.setState(PlayerState.DRAW);
327: System.out.println("Es ist untenschieden. ");
328: }
329: }