Skip to content

Method: WinEvaluationStrategy()

1: package de.fhdw.gaming.ipspiel23.tictactoe.core.evaluation;
2:
3: import java.util.Optional;
4: import java.util.Set;
5: import de.fhdw.gaming.ipspiel23.tictactoe.core.domain.TicTacToeFieldState;
6: import de.fhdw.gaming.ipspiel23.tictactoe.core.domain.TicTacToePlayer;
7: import de.fhdw.gaming.ipspiel23.tictactoe.core.domain.TicTacToeRow;
8: import de.fhdw.gaming.ipspiel23.tictactoe.core.domain.TicTacToeState;
9:
10: /**
11: * A win evaluation strategy for evaluating the state of a Tic-Tac-Toe game.
12: * This strategy focuses on detecting if there are any rows on the board that are uniformly marked by a player.
13: * If such rows exist, a predefined score is assigned to the game state based on the marking player.
14: *
15: * This strategy assigns a high positive score if the current player has uniformly marked rows,
16: * and a high negative score if the opponent player has uniformly marked rows.
17: *
18: * @see IEvaluationStrategy
19: * @see TicTacToeState
20: *
21: */
22: public class WinEvaluationStrategy implements IEvaluationStrategy {
23:
24: /**
25: * Evaluates the given Tic-Tac-Toe game state using the win evaluation strategy.
26: * This strategy focuses on detecting uniformly marked rows on the board.
27: * If uniformly marked rows exist, a predefined score is assigned based on the marking player.
28: *
29: * Too complex, but not easily simplifyable. Warning suppressed.
30: *
31: * @param state The Tic-Tac-Toe game state to be evaluated.
32: * @return An optional containing the evaluation score as an integer value.
33: */
34: @Override
35: @SuppressWarnings({ "checkstyle:CyclomaticComplexity", "PMD.CyclomaticComplexity" })
36: public Optional<Integer> evaluateState(final TicTacToeState state) {
37: final Set<TicTacToeRow> fullRows = state.getBoard().getRowsUniformlyMarked();
38:
39: if (fullRows.isEmpty()) {
40:
41: return Optional.of(0);
42: }
43:
44: Integer returnValue = 0;
45: final TicTacToePlayer currentPlayer = state.getCurrentPlayer();
46:
47: for (final TicTacToeRow r : fullRows) {
48: if (r.getState().get().equals(TicTacToeFieldState.CROSS) && currentPlayer.isUsingCrosses()) {
49: returnValue += 1200;
50: } else if (r.getState().get().equals(TicTacToeFieldState.CROSS)
51: && !currentPlayer.isUsingCrosses()) {
52: returnValue -= 1200;
53: } else if (r.getState().get().equals(TicTacToeFieldState.NOUGHT)
54: && currentPlayer.isUsingCrosses()) {
55: returnValue -= 1200;
56: } else if (r.getState().get().equals(TicTacToeFieldState.NOUGHT) && !currentPlayer.isUsingCrosses()) {
57: returnValue += 1200;
58: }
59: }
60:
61: return Optional.of(returnValue);
62: }
63: }