Skip to content

Package: VGRandomMoveStrategy

VGRandomMoveStrategy

nameinstructionbranchcomplexitylinemethod
VGRandomMoveStrategy(VGMoveFactory)
M: 0 C: 74
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
checkFifthColumn(VGField)
M: 9 C: 10
53%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 2 C: 2
50%
M: 0 C: 1
100%
checkFirstColumn(VGField)
M: 9 C: 10
53%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 2 C: 2
50%
M: 0 C: 1
100%
checkFourthColumn(VGField)
M: 9 C: 10
53%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 2 C: 2
50%
M: 0 C: 1
100%
checkSecondColumn(VGField)
M: 9 C: 10
53%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 2 C: 2
50%
M: 0 C: 1
100%
checkSeventhColumn(VGField)
M: 9 C: 10
53%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 2 C: 2
50%
M: 0 C: 1
100%
checkSixthColumn(VGField)
M: 9 C: 10
53%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 2 C: 2
50%
M: 0 C: 1
100%
checkThirdColumn(VGField)
M: 9 C: 10
53%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 2 C: 2
50%
M: 0 C: 1
100%
computeNextMove(int, VGPlayer, VGState)
M: 0 C: 74
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 13
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
toString()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

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