Skip to contentMethod: toString()
      1: package de.fhdw.gaming.ipspiel22.vierGewinnt.strategy;
2: 
3: import java.util.ArrayList;
4: import java.util.Arrays;
5: import java.util.List;
6: import java.util.Objects;
7: import java.util.Optional;
8: import java.util.Random;
9: 
10: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGAnswerEnum;
11: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGField;
12: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGPlayer;
13: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGState;
14: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGStrategy;
15: import de.fhdw.gaming.ipspiel22.vierGewinnt.moves.VGMove;
16: import de.fhdw.gaming.ipspiel22.vierGewinnt.moves.factory.VGMoveFactory;
17: 
18: /**
19:  * Implements {@link VGStrategy} by choosing a random move.
20:  */
21: public class VGRandomMoveStrategy implements VGStrategy {
22:     /**
23:      * For choosing a random column.
24:      */
25:     private static final Random RANDOM = new Random();
26: 
27:     /**
28:      * Contains all possible moves.
29:      */
30:     private final List<VGMove> movesList;
31: 
32:     /**
33:      * First column move.
34:      */
35:     private final VGMove firstColumn;
36: 
37:     /**
38:      * Second column move.
39:      */
40:     private final VGMove secondColumn;
41: 
42:     /**
43:      * Third column move.
44:      */
45:     private final VGMove thirdColumn;
46: 
47:     /**
48:      * Fourth column move.
49:      */
50:     private final VGMove fourthColumn;
51: 
52:     /**
53:      * Fifth column move.
54:      */
55:     private final VGMove fifthColumn;
56: 
57:     /**
58:      * Sixth column move.
59:      */
60:     private final VGMove sixthColumn;
61: 
62:     /**
63:      * Seventh column move.
64:      */
65:     private final VGMove seventhColumn;
66:   
67:     /**
68:      * Creates an {@link VGRandomMoveStrategy}.
69:      *
70:      * @param moveFactory The factory for creating Vier gewinnt moves.
71:      */
72:     public VGRandomMoveStrategy(final VGMoveFactory moveFactory) {
73:         this.firstColumn = moveFactory.createFirstColumnMove();
74:         this.secondColumn = moveFactory.createSecondColumnMove();
75:         this.thirdColumn = moveFactory.createThirdColumnMove();
76:         this.fourthColumn = moveFactory.createFourthColumnMove();
77:         this.fifthColumn = moveFactory.createFifthColumnMove();
78:         this.sixthColumn = moveFactory.createSixthColumnMove();
79:         this.seventhColumn = moveFactory.createSeventhColumnMove();
80:         this.movesList = new ArrayList<>(Arrays.asList(this.firstColumn,
81:                 this.secondColumn,
82:                 this.thirdColumn,
83:                 this.fourthColumn,
84:                 this.fifthColumn,
85:                 this.sixthColumn,
86:                 this.seventhColumn));
87:     }
88: 
89:     @Override
90:     public Optional<VGMove> computeNextMove(final int gameId, final VGPlayer player, final VGState state) {
91:         checkFirstColumn(state.getBoard().getNextFieldInColumn(VGAnswerEnum.FIRSTCOLUMN));
92:         checkSecondColumn(state.getBoard().getNextFieldInColumn(VGAnswerEnum.SECONDCOLUMN));
93:         checkThirdColumn(state.getBoard().getNextFieldInColumn(VGAnswerEnum.THIRDCOLUMN));
94:         checkFourthColumn(state.getBoard().getNextFieldInColumn(VGAnswerEnum.FOURTHCOLUMN));
95:         checkFifthColumn(state.getBoard().getNextFieldInColumn(VGAnswerEnum.FITFHCOLUMN));
96:         checkSixthColumn(state.getBoard().getNextFieldInColumn(VGAnswerEnum.SIXTHCOLUMN));
97:         checkSeventhColumn(state.getBoard().getNextFieldInColumn(VGAnswerEnum.SEVENTHCOLUMN));
98:         final int index = RANDOM.nextInt(movesList.size());
99:         return Optional.of(movesList.get(index));
100:     }
101: 
102:     /**
103:      * Checks if first Column is full.
104:      * 
105:      * @param vgField if its null, the first column will be removed out of the possible column-list.
106:      */
107:     private void checkFirstColumn(final VGField vgField) {
108:         if (movesList.contains(this.firstColumn) && Objects.isNull(vgField)) {
109:             movesList.remove(this.firstColumn);
110:         }
111:     }
112: 
113:     /**
114:      * Checks if second Column is full.
115:      * 
116:      * @param vgField if its null, the second column will be removed out of the possible column-list.
117:      */
118:     private void checkSecondColumn(final VGField vgField) {
119:         if (movesList.contains(this.secondColumn) && Objects.isNull(vgField)) {
120:             movesList.remove(this.secondColumn);
121:         }
122:     }
123: 
124:     /**
125:      * Checks if third Column is full.
126:      * 
127:      * @param vgField if its null, the third column will be removed out of the possible column-list.
128:      */
129:     private void checkThirdColumn(final VGField vgField) {
130:         if (movesList.contains(this.thirdColumn) && Objects.isNull(vgField)) {
131:             movesList.remove(this.thirdColumn);
132:         }
133:     }
134: 
135:     /**
136:      * Checks if fourth Column is full.
137:      * 
138:      * @param vgField if its null, the fourth column will be removed out of the possible column-list.
139:      */
140:     private void checkFourthColumn(final VGField vgField) {
141:         if (movesList.contains(this.fourthColumn) && Objects.isNull(vgField)) {
142:             movesList.remove(this.fourthColumn);
143:         }
144:     }
145: 
146:     /**
147:      * Checks if fifth Column is full.
148:      * 
149:      * @param vgField if its null, the fifth column will be removed out of the possible column-list.
150:      */
151:     private void checkFifthColumn(final VGField vgField) {
152:         if (movesList.contains(this.fifthColumn) && Objects.isNull(vgField)) {
153:             movesList.remove(this.fifthColumn);
154:         }
155:     }
156: 
157:     /**
158:      * Checks if sixth Column is full.
159:      * 
160:      * @param vgField if its null, the sixth column will be removed out of the possible column-list.
161:      */
162:     private void checkSixthColumn(final VGField vgField) {
163:         if (movesList.contains(this.sixthColumn) && Objects.isNull(vgField)) {
164:             movesList.remove(this.sixthColumn);
165:         }
166:     }
167: 
168:     /**
169:      * Checks if seventh Column is full.
170:      * 
171:      * @param vgField if its null, the seventh column will be removed out of the possible column-list.
172:      */
173:     private void checkSeventhColumn(final VGField vgField) {
174:         if (movesList.contains(this.seventhColumn) && Objects.isNull(vgField)) {
175:             movesList.remove(this.seventhColumn);
176:         }
177:     }
178: 
179:     @Override
180:     public String toString() {
181:         return VGRandomMoveStrategy.class.getSimpleName();
182:     }
183: }