Skip to content

Method: evaluateState(TicTacToeState)

1: package de.fhdw.gaming.ipspiel23.tictactoe.core.evaluation;
2:
3: import java.util.Optional;
4:
5: import de.fhdw.gaming.ipspiel23.tictactoe.core.domain.TicTacToeState;
6:
7: /**
8: * A mixed evaluation strategy for evaluating the state of a Tic-Tac-Toe game.
9: * This strategy combines the evaluations from two different
10: * evaluation strategies: WinEvaluationStrategy and SimpleEvaluationStrategy.
11: * The evaluation result is the sum of the evaluations from both strategies.
12: *
13: * The MixedEvaluationStrategy is initialized with instances of WinEvaluationStrategy and SimpleEvaluationStrategy.
14: *
15: * @see IEvaluationStrategy
16: * @see WinEvaluationStrategy
17: * @see SimpleEvaluationStrategy
18: * @see TicTacToeState
19: * @see java.util.Optional
20: *
21: */
22: public class MixedEvaluationStrategy implements IEvaluationStrategy {
23:
24: /**
25: * The evaluation strategy for win states.
26: */
27: private final IEvaluationStrategy winStrategy;
28:
29: /**
30: * The evaluation strategy for non-win states.
31: */
32: private final IEvaluationStrategy simpleStrategy;
33:
34: /**
35: * Constructs a new MixedEvaluationStrategy object with instances
36: * of WinEvaluationStrategy and SimpleEvaluationStrategy.
37: */
38: public MixedEvaluationStrategy() {
39: this.winStrategy = new WinEvaluationStrategy();
40: this.simpleStrategy = new SimpleEvaluationStrategy();
41: }
42:
43: /**
44: * Evaluates the given Tic-Tac-Toe game state using the mixed evaluation strategy.
45: * The evaluation result is the sum of the evaluations from the win strategy and the simple strategy.
46: *
47: * @param state The Tic-Tac-Toe game state to be evaluated.
48: * @return An optional containing the evaluation result as an integer value.
49: */
50: @Override
51: public Optional<Integer> evaluateState(final TicTacToeState state) {
52:
53: try {
54:
55: final int winEval = this.winStrategy.evaluateState(state).get();
56: final int simpleEval = this.simpleStrategy.evaluateState(state).get();
57: final int totalEval = winEval + simpleEval;
58:
59: return Optional.of(totalEval);
60:
61: } catch (Exception e) {
62: return Optional.empty();
63: }
64: }
65: }