Skip to content

Package: VierConnectsRandomMoveStrategy

VierConnectsRandomMoveStrategy

nameinstructionbranchcomplexitylinemethod
VierConnectsRandomMoveStrategy(VierConnectsMoveFactory)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
computeNextMove(int, VierConnectsPlayer, VierConnectsState, long)
M: 34 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
toString()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright © 2021-2023 Fachhochschule für die Wirtschaft (FHDW) Hannover
3: *
4: * This file is part of ipspiel24-VierConnects-strategy-random.
5: *
6: * ipspiel24-VierConnects-strategy-random is free software: you can redistribute it and/or modify it under
7: * the terms of the GNU General Public License as published by the Free Software
8: * Foundation, either version 3 of the License, or (at your option) any later
9: * version.
10: *
11: * ipspiel24-VierConnects-strategy-random is distributed in the hope that it will be useful, but WITHOUT
12: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14: * details.
15: *
16: * You should have received a copy of the GNU General Public License along with
17: * ipspiel24-VierConnects-strategy-random. If not, see <http://www.gnu.org/licenses/>.
18: */
19: package de.fhdw.gaming.ipspiel24.VierConnects.strategy.random;
20:
21: import java.util.ArrayList;
22: import java.util.List;
23: import java.util.Optional;
24: import java.util.Random;
25:
26: import de.fhdw.gaming.core.domain.GameException;
27: import de.fhdw.gaming.ipspiel24.VierConnects.core.domain.VierConnectsField;
28: import de.fhdw.gaming.ipspiel24.VierConnects.core.domain.VierConnectsFieldState;
29: import de.fhdw.gaming.ipspiel24.VierConnects.core.domain.VierConnectsPlayer;
30: import de.fhdw.gaming.ipspiel24.VierConnects.core.domain.VierConnectsState;
31: import de.fhdw.gaming.ipspiel24.VierConnects.core.domain.VierConnectsStrategy;
32: import de.fhdw.gaming.ipspiel24.VierConnects.core.moves.VierConnectsMove;
33: import de.fhdw.gaming.ipspiel24.VierConnects.core.moves.factory.VierConnectsMoveFactory;
34:
35: /**
36: * Implements {@link VierConnectsStrategy} by randomly choosing the next possible move.
37: */
38: public final class VierConnectsRandomMoveStrategy implements VierConnectsStrategy {
39:
40: /**
41: * The random number generator.
42: */
43: private static final Random RANDOM = new Random();
44:
45: /**
46: * The factory for creating VierConnects moves.
47: */
48: private final VierConnectsMoveFactory moveFactory;
49:
50: /**
51: * Creates an {@link VierConnectsRandomMoveStrategy}.
52: *
53: * @param moveFactory The factory for creating VierConnects moves.
54: */
55: VierConnectsRandomMoveStrategy(final VierConnectsMoveFactory moveFactory) {
56: this.moveFactory = moveFactory;
57: }
58:
59: /**
60: * Chooses some random move.
61: */
62: @Override
63: public Optional<VierConnectsMove> computeNextMove(final int gameId, final VierConnectsPlayer player,
64: final VierConnectsState state, final long maxComputationTimePerMove) throws GameException {
65:
66: final List<VierConnectsField> fields = new ArrayList<>(
67: state.getBoard().getFieldsBeing(VierConnectsFieldState.EMPTY).values());
68:
69:• if (fields.isEmpty()) {
70: return Optional.empty();
71: }
72: final int index = RANDOM.nextInt(fields.size());
73: final VierConnectsField field = fields.get(index);
74: return Optional
75: .of(this.moveFactory.createPlaceMarkMove(player.isUsingCrosses(), field.getPosition().getColumn()));
76: }
77:
78: @Override
79: public String toString() {
80: return VierConnectsRandomMoveStrategy.class.getSimpleName();
81: }
82: }