Skip to content

Method: C4Game(int, IC4State, Map, long, IC4MoveChecker, ObserverFactoryProvider)

1: package de.fhdw.gaming.ipspiel23.c4.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.ObserverFactoryProvider;
9: import de.fhdw.gaming.ipspiel23.c4.domain.IC4Game;
10: import de.fhdw.gaming.ipspiel23.c4.domain.IC4MoveChecker;
11: import de.fhdw.gaming.ipspiel23.c4.domain.IC4Player;
12: import de.fhdw.gaming.ipspiel23.c4.domain.IC4Position;
13: import de.fhdw.gaming.ipspiel23.c4.domain.IC4State;
14: import de.fhdw.gaming.ipspiel23.c4.moves.IC4Move;
15: import de.fhdw.gaming.ipspiel23.c4.moves.factory.IC4MoveFactory;
16: import de.fhdw.gaming.ipspiel23.c4.moves.impl.C4DefaultMoveFactory;
17: import de.fhdw.gaming.ipspiel23.c4.strategies.IC4Strategy;
18:
19: /**
20: * The default implementation of the {@link IC4Game} interface.
21: */
22: public class C4Game extends DefaultGame<IC4Player, IC4State, IC4Move, IC4Strategy> implements IC4Game {
23:
24: /**
25: * Your friendly neighborhood random number generator.
26: */
27: private static final Random RANDOM = new Random();
28:
29: /**
30: * The move factory.
31: */
32: private final IC4MoveFactory moveFactory;
33:
34: /**
35: * Creates a new connect four game.
36: *
37: * @param id The ID of this game.
38: * @param initialState The initial state of the game.
39: * @param strategies The players' strategies.
40: * @param maxComputationTimePerMove The maximum computation time per move in seconds.
41: * @param moveChecker The move checker.
42: * @param observerFactoryProvider The {@link ObserverFactoryProvider}.
43: * @throws IllegalArgumentException if the player sets do not match.
44: * @throws InterruptedException if creating the game has been interrupted.
45: */
46: public C4Game(final int id, final IC4State initialState, final Map<String, IC4Strategy> strategies,
47: final long maxComputationTimePerMove, final IC4MoveChecker moveChecker,
48: final ObserverFactoryProvider observerFactoryProvider)
49: throws IllegalArgumentException, InterruptedException {
50:
51: super(id, initialState, strategies, maxComputationTimePerMove, moveChecker, observerFactoryProvider);
52: this.moveFactory = new C4DefaultMoveFactory();
53: }
54:
55: @Override
56: public Optional<IC4Move> chooseRandomMove(final IC4Player player, final IC4State state) {
57: final IC4Position[] positions = state.getBoard().getLegalPositions();
58: if (positions.length == 0) {
59: return Optional.empty();
60: }
61: final int index = RANDOM.nextInt(positions.length);
62: final IC4Position position = positions[index];
63: return Optional.of(this.moveFactory.createMove(player, position));
64: }
65: }