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