Skip to content

Package: HTGame

HTGame

nameinstructionbranchcomplexitylinemethod
HTGame(int, IHTState, Map, long, MoveChecker, ObserverFactoryProvider)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
chooseRandomMove(IHTPlayer, IHTState)
M: 0 C: 31
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
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: 22
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.ipspiel23.ht.domain.impl;
2:
3: import java.util.List;
4: import java.util.Map;
5: import java.util.Optional;
6: import java.util.Random;
7: import java.util.function.Supplier;
8:
9: import de.fhdw.gaming.core.domain.DefaultGame;
10: import de.fhdw.gaming.core.domain.MoveChecker;
11: import de.fhdw.gaming.core.domain.ObserverFactoryProvider;
12: import de.fhdw.gaming.ipspiel23.ht.domain.IHTGame;
13: import de.fhdw.gaming.ipspiel23.ht.domain.IHTPlayer;
14: import de.fhdw.gaming.ipspiel23.ht.domain.IHTState;
15: import de.fhdw.gaming.ipspiel23.ht.moves.IHTMove;
16: import de.fhdw.gaming.ipspiel23.ht.moves.factory.HTDefaultMoveFactory;
17: import de.fhdw.gaming.ipspiel23.ht.moves.factory.IHTMoveFactory;
18: import de.fhdw.gaming.ipspiel23.ht.strategy.IHTStrategy;
19:
20: /**
21: * Represents the main game class of Heads or Tails.
22: */
23: final class HTGame extends DefaultGame<IHTPlayer, IHTState, IHTMove, IHTStrategy> implements IHTGame {
24:
25: /**
26: * The {@link Random} instance to choose random moves.
27: */
28: private static final Random RANDOM = new Random();
29:
30: /**
31: * The {@link IHTMoveFactory} to create moves.
32: */
33: private final IHTMoveFactory moveFactory = new HTDefaultMoveFactory();
34:
35: /**
36: * Creates a new instance of {@link HTGame}.
37: *
38: * @param id The id of the game.
39: * @param initialState The initial state of the game.
40: * @param strategies The strategies of the players.
41: * @param maxComputationTimePerMove The maximum computation time per move in seconds.
42: * @param moveChecker The move checker.
43: * @param observerFactoryProvider The observer factory provider.
44: * @throws IllegalArgumentException if the given id is negative.
45: * @throws InterruptedException if the current thread is interrupted.
46: */
47: public HTGame(final int id, final IHTState initialState, final Map<String, IHTStrategy> strategies,
48: final long maxComputationTimePerMove,
49: final MoveChecker<IHTPlayer, IHTState, IHTMove> moveChecker,
50: final ObserverFactoryProvider observerFactoryProvider)
51: throws IllegalArgumentException, InterruptedException {
52: super(id, initialState, strategies, maxComputationTimePerMove, moveChecker, observerFactoryProvider);
53: }
54:
55: @Override
56: public Optional<IHTMove> chooseRandomMove(final IHTPlayer player, final IHTState state) {
57: final List<Supplier<IHTMove>> moveSuppliers = List.of(
58: this.moveFactory::createHeadsMove,
59: this.moveFactory::createTailsMove,
60: this.moveFactory::createEdgeMove);
61: return Optional.of(moveSuppliers.get(RANDOM.nextInt(moveSuppliers.size())).get());
62: }
63:
64: @Override
65: public String toString() {
66: return String.format("%s[id=%d, %s]", this.getClass().getSimpleName(), this.getId(), this.gameToString());
67: }
68: }
69: