Skip to content

Method: HTGame(int, IHTState, Map, long, MoveChecker, ObserverFactoryProvider)

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