Skip to content

Package: Strategy

Strategy

nameinstructionbranchcomplexitylinemethod
abortRequested(int)
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isInteractive()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
reset()
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * Copyright © 2020 Fachhochschule für die Wirtschaft (FHDW) Hannover
3: *
4: * This file is part of gaming-core.
5: *
6: * Gaming-core 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: * Gaming-core 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: * gaming-core. If not, see <http://www.gnu.org/licenses/>.
18: */
19: package de.fhdw.gaming.core.domain;
20:
21: import java.util.Optional;
22:
23: /**
24: * Represents a strategy. A strategy is bound to a {@link Player player} and determines her next move.
25: *
26: * @param <P> The type of allowed players.
27: * @param <S> The type of allowed states.
28: * @param <M> The type of allowed moves.
29: */
30: public interface Strategy<P extends Player<P>, S extends State<P, S>, M extends Move<P, S>> {
31:
32: /**
33: * Computes the next move.
34: *
35: * @param gameId The ID of the game.
36: * @param player The player this strategy is bound to.
37: * @param state The game state. This is a copy of the game state. The game strategy is allowed to modify it at
38: * will, e.g. for computing possible move sequences in advance. However, changes have no effects
39: * whatsoever with regard to the game state.
40: * @return The next move to finally apply to the game state. If {@link Optional#empty()} is returned, the strategy
41: * did not succeed in finding an appropriate move for the player. This causes the player to resign
42: * immediately.
43: * <p>
44: * Note that it is also possible to throw a {@link GameException} instead of returning an empty
45: * {@link Optional} (see below). The difference is that returning an empty {@link Optional} conveys the
46: * meaning of "resigning", whereas throwing a {@link GameException} signalises an unexpected error in the
47: * strategy's algorithm.
48: * @throws GameException if the strategy did not succeed in finding an appropriate move for the player due to
49: * some unexpected game state. This causes the player to lose the game immediately.
50: * @throws InterruptedException if choosing the move has been interrupted.
51: */
52: Optional<M> computeNextMove(int gameId, P player, S state) throws GameException, InterruptedException;
53:
54: /**
55: * Returns {@code true} iff this strategy requires user interaction. Such a strategy is ignored when collecting
56: * candidates for a game competition.
57: * <p>
58: * The default implementation returns {@code false}-
59: */
60: default boolean isInteractive() {
61: return false;
62: }
63:
64: /**
65: * Requests the strategy to abort the move search being currently in-progress. Please note that the call is
66: * typically done by some other (non-gaming) thread (e.g. the GUI), so take measures to avoid race conditions.
67: *
68: * @param gameId The ID of the game.
69: */
70: default void abortRequested(final int gameId) {
71: // do nothing
72: }
73:
74: /**
75: * Requests the strategy to reset its internal state due to a new game being started. Each strategy may decide on
76: * its own which state shall be reset and which state shall be retained. The latter allows self learning algorithms.
77: */
78: default void reset() {
79: // do nothing
80: }
81: }