Skip to content

Package: OthelloRandomMoveGenerator

OthelloRandomMoveGenerator

nameinstructionbranchcomplexitylinemethod
OthelloRandomMoveGenerator()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
generate(OthelloPlayer, OthelloState)
M: 6 C: 37
86%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 7
88%
M: 0 C: 1
100%
lambda$generate$0(boolean, OthelloField)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
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%

Coverage

1: /*
2: * Copyright © 2020-2023 Fachhochschule für die Wirtschaft (FHDW) Hannover
3: *
4: * This file is part of othello-core.
5: *
6: * Othello-core is free software: you can redistribute it and/or modify
7: * it under the terms of the GNU General Public License as published by
8: * the Free Software Foundation, either version 3 of the License, or
9: * (at your option) any later version.
10: *
11: * Othello-core is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: * GNU General Public License for more details.
15: *
16: * You should have received a copy of the GNU General Public License
17: * along with othello-core. If not, see <http://www.gnu.org/licenses/>.
18: */
19: package de.fhdw.gaming.othello.core.domain.impl;
20:
21: import java.util.List;
22: import java.util.Optional;
23: import java.util.Random;
24: import java.util.stream.Collectors;
25:
26: import de.fhdw.gaming.othello.core.domain.OthelloField;
27: import de.fhdw.gaming.othello.core.domain.OthelloFieldState;
28: import de.fhdw.gaming.othello.core.domain.OthelloMoveGenerator;
29: import de.fhdw.gaming.othello.core.domain.OthelloPlayer;
30: import de.fhdw.gaming.othello.core.domain.OthelloState;
31: import de.fhdw.gaming.othello.core.moves.OthelloMove;
32: import de.fhdw.gaming.othello.core.moves.factory.OthelloMoveFactory;
33: import de.fhdw.gaming.othello.core.moves.impl.OthelloDefaultMoveFactory;
34:
35: /**
36: * Generates random but valid Othello moves.
37: */
38: final class OthelloRandomMoveGenerator implements OthelloMoveGenerator {
39:
40: /**
41: * The random number generator used for generating random moves.
42: */
43: private static final Random RANDOM = new Random();
44:
45: /**
46: * The move factory.
47: */
48: private final OthelloMoveFactory moveFactory = new OthelloDefaultMoveFactory();
49:
50: @Override
51: public Optional<OthelloMove> generate(final OthelloPlayer player, final OthelloState state) {
52: final boolean usingBlackTokens = player.isUsingBlackTokens();
53: final List<OthelloField> fields = state.getBoard().getFieldsBeing(OthelloFieldState.EMPTY).values().stream()
54: .filter((final OthelloField field) -> field.isActive(usingBlackTokens)).collect(Collectors.toList());
55:
56:• if (fields.isEmpty()) {
57: return Optional.of(this.moveFactory.createSkipMove(usingBlackTokens));
58: } else {
59: final int index = OthelloRandomMoveGenerator.RANDOM.nextInt(fields.size());
60: final OthelloField field = fields.get(index);
61: return Optional.of(this.moveFactory.createPlaceTokenMove(usingBlackTokens, field.getPosition()));
62: }
63: }
64: }