Skip to content

Method: getYellowPlayer()

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