Skip to content

Method: getPossibleMoves()

1: package de.fhdw.gaming.ipspiel22.VGStrategyEins.strategy;
2:
3: import java.util.ArrayList;
4: import java.util.Collections;
5: import java.util.List;
6: import java.util.Objects;
7:
8: import de.fhdw.gaming.core.domain.GameException;
9: import de.fhdw.gaming.core.domain.PlayerState;
10: import de.fhdw.gaming.ipspiel22.searchtree.domain.MinMaxGame;
11: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGAnswerEnum;
12: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGBoard;
13: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGField;
14: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGFieldState;
15: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGPlayer;
16: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGPosition;
17: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGState;
18: import de.fhdw.gaming.ipspiel22.vierGewinnt.moves.VGMove;
19: import de.fhdw.gaming.ipspiel22.vierGewinnt.moves.factory.VGMoveFactory;
20: import de.fhdw.gaming.ipspiel22.vierGewinnt.moves.impl.VG1ColumnMove;
21: import de.fhdw.gaming.ipspiel22.vierGewinnt.moves.impl.VG2ColumnMove;
22: import de.fhdw.gaming.ipspiel22.vierGewinnt.moves.impl.VG3ColumnMove;
23: import de.fhdw.gaming.ipspiel22.vierGewinnt.moves.impl.VG4ColumnMove;
24: import de.fhdw.gaming.ipspiel22.vierGewinnt.moves.impl.VG5ColumnMove;
25: import de.fhdw.gaming.ipspiel22.vierGewinnt.moves.impl.VG6ColumnMove;
26: import de.fhdw.gaming.ipspiel22.vierGewinnt.moves.impl.VG7ColumnMove;
27:
28: /**
29: * Bewertungsfunktion.
30: *
31: * @author DonPablo
32: *
33: */
34: public class Bewertung implements MinMaxGame<VGPlayer, VGState, VGMove> {
35:
36: /**
37: * Spieler.
38: */
39: private final VGPlayer player;
40:
41: /**
42: * Spielstatus.
43: */
44: private VGState state;
45:
46: /**
47: * mf.
48: */
49: private final VGMoveFactory movesFactory;
50:
51: /**
52: * First column move.
53: */
54: private final VGMove firstColumn;
55:
56: /**
57: * Second column move.
58: */
59: private final VGMove secondColumn;
60:
61: /**
62: * Third column move.
63: */
64: private final VGMove thirdColumn;
65:
66: /**
67: * Fourth column move.
68: */
69: private final VGMove fourthColumn;
70:
71: /**
72: * Fifth column move.
73: */
74: private final VGMove fifthColumn;
75:
76: /**
77: * Sixth column move.
78: */
79: private final VGMove sixthColumn;
80:
81: /**
82: * Seventh column move.
83: */
84: private final VGMove seventhColumn;
85:
86: /**
87: * Represents the states which are common in the minMaxAlgorithm.
88: */
89: private final List<VGState> states = new ArrayList<>();
90:
91:
92: /**
93: * Bewertungsfunktion konstruktor.
94: *
95: * @param player
96: * @param state
97: * @param movesFactory
98: */
99: public Bewertung(final VGPlayer player, final VGState state, final VGMoveFactory movesFactory) {
100: this.player = player;
101: this.state = state;
102: this.movesFactory = movesFactory;
103: this.firstColumn = movesFactory.createFirstColumnMove();
104: this.secondColumn = movesFactory.createSecondColumnMove();
105: this.thirdColumn = movesFactory.createThirdColumnMove();
106: this.fourthColumn = movesFactory.createFourthColumnMove();
107: this.fifthColumn = movesFactory.createFifthColumnMove();
108: this.sixthColumn = movesFactory.createSixthColumnMove();
109: this.seventhColumn = movesFactory.createSeventhColumnMove();
110: }
111:
112: @Override
113: public double evaluateStateful() {
114: final VGPlayer currentPlayer = state.getCurrentPlayer();
115:
116: final VGBoard board = state.getBoard();
117:
118: final List<List<? extends VGField>> fields = board.getFields();
119:
120: // Die ermittelten Felder
121: final List<VGField> fieldWithRed = new ArrayList<>();
122: final List<VGField> fieldWithYellow = new ArrayList<>();
123:
124: /**
125: * for-schleife zum evaluieren der besetzten Felder
126: */
127: for (final List<? extends VGField> i : fields) {
128: for (final VGField m : i) {
129: if (m.getState() == VGFieldState.RED) {
130: fieldWithRed.add(m);
131: } else if (m.getState() == VGFieldState.YELLOW) {
132: fieldWithYellow.add(m);
133: }
134: }
135: }
136:
137: final int redPlayerScore = calcPlayerPoints(fieldWithRed);
138: final int yellowPlayerScore = calcPlayerPoints(fieldWithRed);
139:
140: System.out.println("Red Player: " + redPlayerScore);
141: System.out.println("Yellow Player: " + yellowPlayerScore);
142:
143: if (currentPlayer.isUsingRedChips()) {
144: System.out.println("current player " + state.getCurrentPlayer().getName() + " Score: " + redPlayerScore);
145: return redPlayerScore; // - yellowPlayerScore;
146: } else {
147: System.out.println("current player " + state.getCurrentPlayer().getName() + " Score: " + yellowPlayerScore);
148: return yellowPlayerScore; // - redPlayerScore;
149: }
150: }
151:
152: /**
153: * Calculate Player points.
154: * @param positions position with a Token.
155: * @return
156: */
157: private int calcPlayerPoints(final List<VGField> positions) {
158: int score = 0;
159: for (final VGField vgField : positions) {
160: score += checkIfPositionWorth13(vgField);
161: }
162: return score;
163: }
164:
165: /**
166: * Check if the given position is worth 13 points.
167: * @param vgField the position to check.
168: * @return score.
169: */
170: private int checkIfPositionWorth13(final VGField vgField) {
171: final List<VGPosition> punkteDreizehn = new ArrayList<>();
172: punkteDreizehn.add(new VGPosition(3, 2));
173: punkteDreizehn.add(new VGPosition(3, 3));
174: if (punkteDreizehn.contains(vgField.getPosition())) {
175: return 13;
176: } else {
177: return checkIfPositionWorth11(vgField);
178: }
179: }
180:
181: /**
182: * Check if the given position is worth 11 points.
183: * @param vgField the position to check.
184: * @return score.
185: */
186: private int checkIfPositionWorth11(final VGField vgField) {
187: final List<VGPosition> punkteElf = new ArrayList<>();
188: punkteElf.add(new VGPosition(2, 3));
189: punkteElf.add(new VGPosition(2, 2));
190: punkteElf.add(new VGPosition(4, 2));
191: punkteElf.add(new VGPosition(4, 3));
192:
193: if (punkteElf.contains(vgField.getPosition())) {
194: return 11;
195: } else {
196: return checkIfPositionWorth8(vgField);
197: }
198: }
199:
200: /**
201: * Check if the given position is worth 8 points.
202: * @param vgField the position to check.
203: * @return score.
204: */
205: private int checkIfPositionWorth8(final VGField vgField) {
206: final List<VGPosition> punkteAcht = new ArrayList<>();
207: punkteAcht.add(new VGPosition(3, 4));
208: punkteAcht.add(new VGPosition(3, 1));
209:
210: if (punkteAcht.contains(vgField.getPosition())) {
211: return 8;
212: } else {
213: return checkIfPositionWorth7(vgField);
214: }
215: }
216:
217: /**
218: * Check if the given position is worth 7 points.
219: * @param vgField the position to check.
220: * @return score.
221: */
222: private int checkIfPositionWorth7(final VGField vgField) {
223: final List<VGPosition> punkteSieben = new ArrayList<>();
224: punkteSieben.add(new VGPosition(1, 2));
225: punkteSieben.add(new VGPosition(1, 3));
226: punkteSieben.add(new VGPosition(2, 1));
227: punkteSieben.add(new VGPosition(2, 4));
228: punkteSieben.add(new VGPosition(3, 5));
229: punkteSieben.add(new VGPosition(3, 0));
230: punkteSieben.add(new VGPosition(4, 4));
231: punkteSieben.add(new VGPosition(4, 1));
232: punkteSieben.add(new VGPosition(5, 3));
233: punkteSieben.add(new VGPosition(5, 2));
234:
235: if (punkteSieben.contains(vgField.getPosition())) {
236: return 7;
237: } else {
238: return checkIfPositionWorth6(vgField);
239: }
240: }
241:
242: /**
243: * Check if the given position is worth 6 points.
244: * @param vgField the position to check.
245: * @return score.
246: */
247: private int checkIfPositionWorth6(final VGField vgField) {
248: final List<VGPosition> punkteSechs = new ArrayList<>();
249: punkteSechs.add(new VGPosition(1, 1));
250: punkteSechs.add(new VGPosition(1, 4));
251: punkteSechs.add(new VGPosition(5, 1));
252: punkteSechs.add(new VGPosition(5, 4));
253:
254: if (punkteSechs.contains(vgField.getPosition())) {
255: return 6;
256: } else {
257: return checkIfPositionWorth5(vgField);
258: }
259: }
260:
261: /**
262: * Check if the given position is worth 5 points.
263: * @param vgField the position to check.
264: * @return score.
265: */
266: private int checkIfPositionWorth5(final VGField vgField) {
267: final List<VGPosition> punkteFuenf = new ArrayList<>();
268: punkteFuenf.add(new VGPosition(0, 3));
269: punkteFuenf.add(new VGPosition(0, 2));
270: punkteFuenf.add(new VGPosition(2, 0));
271: punkteFuenf.add(new VGPosition(2, 5));
272: punkteFuenf.add(new VGPosition(4, 0));
273: punkteFuenf.add(new VGPosition(4, 5));
274: punkteFuenf.add(new VGPosition(6, 2));
275: punkteFuenf.add(new VGPosition(6, 3));
276:
277: if (punkteFuenf.contains(vgField.getPosition())) {
278: return 5;
279: } else {
280: return checkIfPositionWorth4(vgField);
281: }
282: }
283:
284: /**
285: * Check if the given position is worth 4 points.
286: * @param vgField the position to check.
287: * @return score.
288: */
289: private int checkIfPositionWorth4(final VGField vgField) {
290: final List<VGPosition> punkteVier = new ArrayList<>();
291: punkteVier.add(new VGPosition(0, 1));
292: punkteVier.add(new VGPosition(0, 4));
293: punkteVier.add(new VGPosition(1, 0));
294: punkteVier.add(new VGPosition(1, 5));
295: punkteVier.add(new VGPosition(5, 5));
296: punkteVier.add(new VGPosition(5, 0));
297: punkteVier.add(new VGPosition(6, 4));
298: punkteVier.add(new VGPosition(6, 1));
299:
300: if (punkteVier.contains(vgField.getPosition())) {
301: return 4;
302: } else {
303: return checkIfPositionWorth3(vgField);
304: }
305: }
306:
307: /**
308: * Check if the given position is worth 3 points else return null.
309: * @param vgField the position to check.
310: * @return score.
311: */
312: private int checkIfPositionWorth3(final VGField vgField) {
313: final List<VGPosition> punkteDrei = new ArrayList<>();
314: punkteDrei.add(new VGPosition(0, 0));
315: punkteDrei.add(new VGPosition(0, 5));
316: punkteDrei.add(new VGPosition(6, 0));
317: punkteDrei.add(new VGPosition(6, 5));
318:
319: if (punkteDrei.contains(vgField.getPosition())) {
320: return 3;
321: } else {
322: return 0;
323: }
324: }
325:
326:
327: @Override
328: public boolean isGameOver() {
329: return player.getState() != PlayerState.PLAYING;
330: }
331:
332: @Override
333: public List<VGMove> getPossibleMoves() {
334: final List<VGMove> possibleMoves = new ArrayList<>();
335:
336:• if (Objects.nonNull(state.getBoard().getNextFieldInColumn(VGAnswerEnum.FIRSTCOLUMN))) {
337: possibleMoves.add(new VG1ColumnMove());
338: }
339:• if (Objects.nonNull(state.getBoard().getNextFieldInColumn(VGAnswerEnum.SECONDCOLUMN))) {
340: possibleMoves.add(new VG2ColumnMove());
341: }
342:• if (Objects.nonNull(state.getBoard().getNextFieldInColumn(VGAnswerEnum.THIRDCOLUMN))) {
343: possibleMoves.add(new VG3ColumnMove());
344: }
345:• if (Objects.nonNull(state.getBoard().getNextFieldInColumn(VGAnswerEnum.FOURTHCOLUMN))) {
346: possibleMoves.add(new VG4ColumnMove());
347: }
348:• if (Objects.nonNull(state.getBoard().getNextFieldInColumn(VGAnswerEnum.FITFHCOLUMN))) {
349: possibleMoves.add(new VG5ColumnMove());
350: }
351:• if (Objects.nonNull(state.getBoard().getNextFieldInColumn(VGAnswerEnum.SIXTHCOLUMN))) {
352: possibleMoves.add(new VG6ColumnMove());
353: }
354:• if (Objects.nonNull(state.getBoard().getNextFieldInColumn(VGAnswerEnum.SEVENTHCOLUMN))) {
355: possibleMoves.add(new VG7ColumnMove());
356: }
357:
358: Collections.shuffle(possibleMoves);
359: return possibleMoves;
360: }
361:
362: @Override
363: public void commitMove(final VGMove move) throws GameException {
364: states.add(this.state);
365: // move.applyTo(this.state, this.player);
366:
367: if (move.equals(firstColumn)) {
368: // movesFactory.createFirstColumnMove();
369: this.movesFactory.createTokenMove(player.isUsingRedChips(),
370: state.getBoard().getNextFieldInColumn(VGAnswerEnum.FIRSTCOLUMN).getPosition());
371:
372: } else if (move.equals(secondColumn)) {
373: // movesFactory.createSecondColumnMove();
374: this.movesFactory.createTokenMove(player.isUsingRedChips(),
375: state.getBoard().getNextFieldInColumn(VGAnswerEnum.SECONDCOLUMN).getPosition());
376:
377: } else if (move.equals(thirdColumn)) {
378: // movesFactory.createThirdColumnMove();
379: this.movesFactory.createTokenMove(player.isUsingRedChips(),
380: state.getBoard().getNextFieldInColumn(VGAnswerEnum.THIRDCOLUMN).getPosition());
381:
382: } else if (move.equals(fourthColumn)) {
383: // movesFactory.createFourthColumnMove();
384: this.movesFactory.createTokenMove(player.isUsingRedChips(),
385: state.getBoard().getNextFieldInColumn(VGAnswerEnum.FOURTHCOLUMN).getPosition());
386:
387: } else if (move.equals(fifthColumn)) {
388: // movesFactory.createFifthColumnMove();
389: this.movesFactory.createTokenMove(player.isUsingRedChips(),
390: state.getBoard().getNextFieldInColumn(VGAnswerEnum.FITFHCOLUMN).getPosition());
391:
392: } else if (move.equals(sixthColumn)) {
393: // movesFactory.createSixthColumnMove();
394: this.movesFactory.createTokenMove(player.isUsingRedChips(),
395: state.getBoard().getNextFieldInColumn(VGAnswerEnum.SIXTHCOLUMN).getPosition());
396:
397: } else if (move.equals(seventhColumn)) {
398: // movesFactory.createSeventhColumnMove();
399: this.movesFactory.createTokenMove(player.isUsingRedChips(),
400: state.getBoard().getNextFieldInColumn(VGAnswerEnum.SEVENTHCOLUMN).getPosition());
401:
402: }
403:
404: }
405:
406: @Override
407: public void rollbackMove(final VGMove move) {
408: state = states.get(0);
409: }
410:
411: /**
412: * Added with pull. Keine Ahnung wer das hinzugefügt hat
413: */
414: @Override
415: public VGMove saveFirstMoves(final VGMove move) {
416: return null;
417: }
418:
419: }