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-2023 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
38: * to modify it at
39: * will, e.g. for computing possible move sequences in advance. However, changes
40: * have no effects
41: * whatsoever with regard to the game state.
42: * @param maxComputationTimePerMove The maximum computation time per move in seconds.
43: * @return The next move to finally apply to the game state. If {@link Optional#empty()} is returned, the strategy
44: * did not succeed in finding an appropriate move for the player. This causes the player to resign
45: * immediately.
46: * <p>
47: * Note that it is also possible to throw a {@link GameException} instead of returning an empty
48: * {@link Optional} (see below). The difference is that returning an empty {@link Optional} conveys the
49: * meaning of "resigning", whereas throwing a {@link GameException} signalises an unexpected error in the
50: * strategy's algorithm.
51: * @throws GameException if the strategy did not succeed in finding an appropriate move for the player due to
52: * some unexpected game state. This causes the player to lose the game immediately.
53: * @throws InterruptedException if choosing the move has been interrupted.
54: */
55: Optional<M> computeNextMove(
56: int gameId,
57: P player,
58: S state,
59: long maxComputationTimePerMove) throws GameException, InterruptedException;
60:
61: /**
62: * Returns {@code true} iff this strategy requires user interaction. Such a strategy is ignored when collecting
63: * candidates for a game competition.
64: * <p>
65: * The default implementation returns {@code false}-
66: */
67: default boolean isInteractive() {
68: return false;
69: }
70:
71: /**
72: * Requests the strategy to abort the move search being currently in-progress. Please note that the call is
73: * typically done by some other (non-gaming) thread (e.g. the GUI), so take measures to avoid race conditions.
74: *
75: * @param gameId The ID of the game.
76: */
77: default void abortRequested(final int gameId) {
78: // do nothing
79: }
80:
81: /**
82: * Requests the strategy to reset its internal state due to a new game being started. Each strategy may decide on
83: * its own which state shall be reset and which state shall be retained. The latter allows self learning algorithms.
84: */
85: default void reset() {
86: // do nothing
87: }
88: }