Skip to contentMethod: evaluateStateful()
      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:•        if (currentPlayer.isUsingRedChips()) {
141:             return redPlayerScore; // - yellowPlayerScore;
142:         } else {
143:             return yellowPlayerScore; // - redPlayerScore;
144:         }
145:     }
146:     
147:     /**
148:      * Calculate Player points.
149:      * @param positions position with a Token.
150:      * @return
151:      */
152:     private int calcPlayerPoints(final List<VGField> positions) {
153:         int score = 0; 
154:         for (final VGField vgField : positions) {
155:             score += checkIfPositionWorth13(vgField);
156:         }
157:         return score;
158:     }
159:     
160:     /**
161:      * Check if the given position is worth 13 points.
162:      * @param vgField the position to check.
163:      * @return score.
164:      */
165:     private int checkIfPositionWorth13(final VGField vgField) {
166:         final List<VGPosition> punkteDreizehn = new ArrayList<>();
167:         punkteDreizehn.add(new VGPosition(3, 2));
168:         punkteDreizehn.add(new VGPosition(3, 3));
169:         if (punkteDreizehn.contains(vgField.getPosition())) {
170:             return 13;
171:         } else {
172:             return checkIfPositionWorth11(vgField);
173:         }
174:     }
175:     
176:     /**
177:      * Check if the given position is worth 11 points.
178:      * @param vgField the position to check.
179:      * @return score.
180:      */
181:     private int checkIfPositionWorth11(final VGField vgField) {
182:         final List<VGPosition> punkteElf = new ArrayList<>();
183:         punkteElf.add(new VGPosition(2, 3));
184:         punkteElf.add(new VGPosition(2, 2));
185:         punkteElf.add(new VGPosition(4, 2));
186:         punkteElf.add(new VGPosition(4, 3));
187:         
188:         if (punkteElf.contains(vgField.getPosition())) {
189:             return 11;
190:         } else {
191:             return checkIfPositionWorth8(vgField);
192:         }
193:     }
194:     
195:     /**
196:      * Check if the given position is worth 8 points.
197:      * @param vgField the position to check.
198:      * @return score.
199:      */
200:     private int checkIfPositionWorth8(final VGField vgField) {
201:         final List<VGPosition> punkteAcht = new ArrayList<>();
202:         punkteAcht.add(new VGPosition(3, 4));
203:         punkteAcht.add(new VGPosition(3, 1));
204:         
205:         if (punkteAcht.contains(vgField.getPosition())) {
206:             return 8;
207:         } else {
208:             return checkIfPositionWorth7(vgField);
209:         }  
210:     }
211:     
212:     /**
213:      * Check if the given position is worth 7 points.
214:      * @param vgField the position to check.
215:      * @return score.
216:      */
217:     private int checkIfPositionWorth7(final VGField vgField) {
218:         final List<VGPosition> punkteSieben = new ArrayList<>();
219:         punkteSieben.add(new VGPosition(1, 2));
220:         punkteSieben.add(new VGPosition(1, 3));
221:         punkteSieben.add(new VGPosition(2, 1));
222:         punkteSieben.add(new VGPosition(2, 4));
223:         punkteSieben.add(new VGPosition(3, 5));
224:         punkteSieben.add(new VGPosition(3, 0));
225:         punkteSieben.add(new VGPosition(4, 4));
226:         punkteSieben.add(new VGPosition(4, 1));
227:         punkteSieben.add(new VGPosition(5, 3));
228:         punkteSieben.add(new VGPosition(5, 2));
229:         
230:         if (punkteSieben.contains(vgField.getPosition())) {
231:             return 7;
232:         } else {
233:             return checkIfPositionWorth6(vgField);
234:         } 
235:     }
236:     
237:     /**
238:      * Check if the given position is worth 6 points.
239:      * @param vgField the position to check.
240:      * @return score.
241:      */
242:     private int checkIfPositionWorth6(final VGField vgField) {
243:         final List<VGPosition> punkteSechs = new ArrayList<>();
244:         punkteSechs.add(new VGPosition(1, 1));
245:         punkteSechs.add(new VGPosition(1, 4));
246:         punkteSechs.add(new VGPosition(5, 1));
247:         punkteSechs.add(new VGPosition(5, 4));
248:         
249:         if (punkteSechs.contains(vgField.getPosition())) {
250:             return 6;
251:         } else {
252:             return checkIfPositionWorth5(vgField);
253:         }  
254:     }
255:     
256:     /**
257:      * Check if the given position is worth 5 points.
258:      * @param vgField the position to check.
259:      * @return score.
260:      */
261:     private int checkIfPositionWorth5(final VGField vgField) {
262:         final List<VGPosition> punkteFuenf = new ArrayList<>();
263:         punkteFuenf.add(new VGPosition(0, 3));
264:         punkteFuenf.add(new VGPosition(0, 2));
265:         punkteFuenf.add(new VGPosition(2, 0));
266:         punkteFuenf.add(new VGPosition(2, 5));
267:         punkteFuenf.add(new VGPosition(4, 0));
268:         punkteFuenf.add(new VGPosition(4, 5));
269:         punkteFuenf.add(new VGPosition(6, 2));
270:         punkteFuenf.add(new VGPosition(6, 3));
271:         
272:         if (punkteFuenf.contains(vgField.getPosition())) {
273:             return 5;
274:         } else {
275:             return checkIfPositionWorth4(vgField);
276:         }  
277:     }
278:     
279:     /**
280:      * Check if the given position is worth 4 points.
281:      * @param vgField the position to check.
282:      * @return score.
283:      */
284:     private int checkIfPositionWorth4(final VGField vgField) {
285:         final List<VGPosition> punkteVier = new ArrayList<>();
286:         punkteVier.add(new VGPosition(0, 1));
287:         punkteVier.add(new VGPosition(0, 4));
288:         punkteVier.add(new VGPosition(1, 0));
289:         punkteVier.add(new VGPosition(1, 5));
290:         punkteVier.add(new VGPosition(5, 5));
291:         punkteVier.add(new VGPosition(5, 0));
292:         punkteVier.add(new VGPosition(6, 4));
293:         punkteVier.add(new VGPosition(6, 1));
294:         
295:         if (punkteVier.contains(vgField.getPosition())) {
296:             return 4;
297:         } else {
298:             return checkIfPositionWorth3(vgField);
299:         }  
300:     }
301:     
302:     /**
303:      * Check if the given position is worth 3 points else return null.
304:      * @param vgField the position to check.
305:      * @return score.
306:      */
307:     private int checkIfPositionWorth3(final VGField vgField) {
308:         final List<VGPosition> punkteDrei = new ArrayList<>();
309:         punkteDrei.add(new VGPosition(0, 0));
310:         punkteDrei.add(new VGPosition(0, 5));
311:         punkteDrei.add(new VGPosition(6, 0));
312:         punkteDrei.add(new VGPosition(6, 5));
313:         
314:         if (punkteDrei.contains(vgField.getPosition())) {
315:             return 3;
316:         } else {
317:             return 0;
318:         }  
319:     }
320: 
321: 
322:     @Override
323:     public boolean isGameOver() {
324:         return player.getState() != PlayerState.PLAYING;
325:     }
326: 
327:     @Override
328:     public List<VGMove> getPossibleMoves() {
329:         final List<VGMove> possibleMoves = new ArrayList<>();
330:         
331:         if (Objects.nonNull(state.getBoard().getNextFieldInColumn(VGAnswerEnum.FIRSTCOLUMN))) {
332:             possibleMoves.add(new VG1ColumnMove());
333:         }
334:         if (Objects.nonNull(state.getBoard().getNextFieldInColumn(VGAnswerEnum.SECONDCOLUMN))) {
335:             possibleMoves.add(new VG2ColumnMove());
336:         }
337:         if (Objects.nonNull(state.getBoard().getNextFieldInColumn(VGAnswerEnum.THIRDCOLUMN))) {
338:             possibleMoves.add(new VG3ColumnMove());
339:         }
340:         if (Objects.nonNull(state.getBoard().getNextFieldInColumn(VGAnswerEnum.FOURTHCOLUMN))) {
341:             possibleMoves.add(new VG4ColumnMove());
342:         }
343:         if (Objects.nonNull(state.getBoard().getNextFieldInColumn(VGAnswerEnum.FITFHCOLUMN))) {
344:             possibleMoves.add(new VG5ColumnMove());
345:         }
346:         if (Objects.nonNull(state.getBoard().getNextFieldInColumn(VGAnswerEnum.SIXTHCOLUMN))) {
347:             possibleMoves.add(new VG6ColumnMove());
348:         }
349:         if (Objects.nonNull(state.getBoard().getNextFieldInColumn(VGAnswerEnum.SEVENTHCOLUMN))) {
350:             possibleMoves.add(new VG7ColumnMove());
351:         }
352:         
353:         Collections.shuffle(possibleMoves);
354:         return possibleMoves;
355:     }
356: 
357:     @Override
358:     public void commitMove(final VGMove move) throws GameException {
359:         states.add(this.state);
360: //        move.applyTo(this.state, this.player);
361: 
362:         if (move.equals(firstColumn)) {
363: //            movesFactory.createFirstColumnMove();
364:             this.movesFactory.createTokenMove(player.isUsingRedChips(),
365:                     state.getBoard().getNextFieldInColumn(VGAnswerEnum.FIRSTCOLUMN).getPosition());
366: 
367:         } else if (move.equals(secondColumn)) {
368: //            movesFactory.createSecondColumnMove();
369:             this.movesFactory.createTokenMove(player.isUsingRedChips(),
370:                     state.getBoard().getNextFieldInColumn(VGAnswerEnum.SECONDCOLUMN).getPosition());
371: 
372:         } else if (move.equals(thirdColumn)) {
373: //            movesFactory.createThirdColumnMove();
374:             this.movesFactory.createTokenMove(player.isUsingRedChips(),
375:                     state.getBoard().getNextFieldInColumn(VGAnswerEnum.THIRDCOLUMN).getPosition());
376: 
377:         } else if (move.equals(fourthColumn)) {
378: //            movesFactory.createFourthColumnMove();
379:             this.movesFactory.createTokenMove(player.isUsingRedChips(),
380:                     state.getBoard().getNextFieldInColumn(VGAnswerEnum.FOURTHCOLUMN).getPosition());
381: 
382:         } else if (move.equals(fifthColumn)) {
383: //            movesFactory.createFifthColumnMove();
384:             this.movesFactory.createTokenMove(player.isUsingRedChips(),
385:                     state.getBoard().getNextFieldInColumn(VGAnswerEnum.FITFHCOLUMN).getPosition());
386: 
387:         } else if (move.equals(sixthColumn)) {
388: //            movesFactory.createSixthColumnMove();
389:             this.movesFactory.createTokenMove(player.isUsingRedChips(),
390:                     state.getBoard().getNextFieldInColumn(VGAnswerEnum.SIXTHCOLUMN).getPosition());
391: 
392:         } else if (move.equals(seventhColumn)) {
393: //            movesFactory.createSeventhColumnMove();
394:             this.movesFactory.createTokenMove(player.isUsingRedChips(),
395:                     state.getBoard().getNextFieldInColumn(VGAnswerEnum.SEVENTHCOLUMN).getPosition());
396: 
397:         }
398: 
399:     }
400: 
401:     @Override
402:     public void rollbackMove(final VGMove move) {
403:         state = states.get(0);
404:     }
405: }