Skip to content

Method: VierConnectsInteractiveStrategy(VierConnectsMoveFactory)

1: /*
2: * Copyright © 2021-2023 Fachhochschule für die Wirtschaft (FHDW) Hannover
3: *
4: * This file is part of ipspiel24-VierConnects-gui.
5: *
6: * ipspiel24-VierConnects-gui is free software: you can redistribute it and/or modify it under
7: * the terms of the GNU General Public License as published by the Free Software
8: * Foundation, either version 3 of the License, or (at your option) any later
9: * version.
10: *
11: * ipspiel24-VierConnects-gui is distributed in the hope that it will be useful, but WITHOUT
12: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14: * details.
15: *
16: * You should have received a copy of the GNU General Public License along with
17: * ipspiel24-VierConnects-gui. If not, see <http://www.gnu.org/licenses/>.
18: */
19: package de.fhdw.gaming.ipspiel24.VierConnects.gui.impl;
20:
21: import java.util.Optional;
22: import java.util.concurrent.atomic.AtomicReference;
23:
24: import de.fhdw.gaming.core.domain.GameException;
25: import de.fhdw.gaming.ipspiel24.VierConnects.core.domain.VierConnectsPlayer;
26: import de.fhdw.gaming.ipspiel24.VierConnects.core.domain.VierConnectsState;
27: import de.fhdw.gaming.ipspiel24.VierConnects.core.domain.VierConnectsStrategy;
28: import de.fhdw.gaming.ipspiel24.VierConnects.core.moves.VierConnectsMove;
29: import de.fhdw.gaming.ipspiel24.VierConnects.core.moves.factory.VierConnectsMoveFactory;
30: import de.fhdw.gaming.ipspiel24.VierConnects.gui.VierConnectsBoardEventProvider;
31:
32: /**
33: * Implements {@link VierConnectsStrategy} by asking the user for a move.
34: */
35: public final class VierConnectsInteractiveStrategy implements VierConnectsStrategy {
36:
37: /**
38: * The factory for creating VierConnects moves.
39: */
40: private final VierConnectsMoveFactory moveFactory;
41:
42: /**
43: * Creates an {@link VierConnectsInteractiveStrategy}.
44: *
45: * @param moveFactory The factory for creating VierConnects moves.
46: */
47: VierConnectsInteractiveStrategy(final VierConnectsMoveFactory moveFactory) {
48: this.moveFactory = moveFactory;
49: }
50:
51: /**
52: * Asks the user to choose a move.
53: */
54: @Override
55: public Optional<VierConnectsMove> computeNextMove(final int gameId, final VierConnectsPlayer player,
56: final VierConnectsState state, final long maxComputationTimePerMove) throws GameException {
57:
58: final Optional<VierConnectsBoardEventProvider> provider = VierConnectsGuiObserverImpl.getEventProvider(gameId);
59: if (provider.isEmpty()) {
60: return Optional.empty();
61: }
62:
63: final AtomicReference<VierConnectsMove> move = new AtomicReference<>();
64: provider.get().waitForEvent(player, state).accept(event -> move.setPlain(
65: this.moveFactory.createPlaceMarkMove(player.isUsingCrosses(), event.getFieldPosition().getColumn())));
66:
67: return Optional.ofNullable(move.getPlain());
68: }
69:
70: @Override
71: public String toString() {
72: return VierConnectsInteractiveStrategy.class.getSimpleName();
73: }
74:
75: @Override
76: public boolean isInteractive() {
77: return true;
78: }
79:
80: @Override
81: public void abortRequested(final int gameId) {
82: final Optional<VierConnectsBoardEventProvider> provider = VierConnectsGuiObserverImpl.getEventProvider(gameId);
83: if (!provider.isEmpty()) {
84: provider.get().cancelWaiting();
85: }
86: }
87: }