Skip to content

Method: getCurrentPlayer(TicTacToeState)

1: package de.fhdw.gaming.ipspiel23.tictactoe.core.domain.impl;
2:
3: import java.util.ArrayList;
4: import java.util.Collection;
5: import java.util.List;
6: import java.util.Optional;
7:
8: import de.fhdw.gaming.core.domain.Move;
9: import de.fhdw.gaming.ipspiel23.gst.domain.IKopplung;
10: import de.fhdw.gaming.ipspiel23.tictactoe.core.domain.TicTacToeField;
11: import de.fhdw.gaming.ipspiel23.tictactoe.core.domain.TicTacToeFieldState;
12: import de.fhdw.gaming.ipspiel23.tictactoe.core.domain.TicTacToePlayer;
13: import de.fhdw.gaming.ipspiel23.tictactoe.core.domain.TicTacToeState;
14: import de.fhdw.gaming.ipspiel23.tictactoe.core.evaluation.IEvaluationStrategy;
15: import de.fhdw.gaming.ipspiel23.tictactoe.core.evaluation.MixedEvaluationStrategy;
16: import de.fhdw.gaming.ipspiel23.tictactoe.core.moves.impl.TicTacToeDefaultMoveFactory;
17:
18: /**
19: * Implementation of the {@link IKopplung} interface for the Tic-Tac-Toe game.
20: * Provides methods for obtaining possible moves, evaluating the game state,
21: * and getting the current player.
22: *
23: * @author borkowitz
24: */
25: public class TicTacToeKopplungImpl implements IKopplung<TicTacToePlayer, TicTacToeState> {
26:
27: /**
28: * The evaluation strategy used to evaluate the Tic-Tac-Toe game state.
29: */
30: private final IEvaluationStrategy evaluationStrategy;
31:
32: /**
33: * Constructs a new TicTacToeKopplungImpl object with a mixed evaluation strategy.
34: */
35: public TicTacToeKopplungImpl() {
36: this.evaluationStrategy = new MixedEvaluationStrategy();
37: }
38:
39: /**
40: * Retrieves a collection of possible moves for the given Tic-Tac-Toe game state.
41: *
42: * @param state The current Tic-Tac-Toe game state.
43: * @return An optional containing the collection of possible moves.
44: */
45: @Override
46: public Optional<Collection<Move<TicTacToePlayer, TicTacToeState>>> getPossibleMoves(
47: final TicTacToeState state) {
48:
49: final List<TicTacToeField> fields = new ArrayList<>(
50: state.getBoard().getFieldsBeing(TicTacToeFieldState.EMPTY).values());
51:
52: if (fields.isEmpty()) {
53: return Optional.of(new ArrayList<Move<TicTacToePlayer, TicTacToeState>>());
54: }
55:
56: final Collection<Move<TicTacToePlayer, TicTacToeState>> possibleMoves = new ArrayList<>();
57: final TicTacToeDefaultMoveFactory factory = new TicTacToeDefaultMoveFactory();
58:
59: for (final TicTacToeField ticTacToeField : fields) {
60: possibleMoves.add(factory.createPlaceMarkMove(state.getCurrentPlayer().isUsingCrosses(),
61: ticTacToeField.getPosition()));
62:
63: }
64:
65: return Optional.of(possibleMoves);
66: }
67:
68: /**
69: * Evaluates the given Tic-Tac-Toe game state using the configured evaluation strategy.
70: *
71: * @param state The current Tic-Tac-Toe game state.
72: * @return An optional containing the evaluation result.
73: */
74: @Override
75: public Optional<Integer> evalState(final TicTacToeState state) {
76: return this.evaluationStrategy.evaluateState(state);
77: }
78:
79: /**
80: * Retrieves the current player in the given Tic-Tac-Toe game state.
81: *
82: * @param state The current Tic-Tac-Toe game state.
83: * @return An optional containing the
84: */
85: @Override
86: public Optional<TicTacToePlayer> getCurrentPlayer(final TicTacToeState state) {
87: return Optional.of(state.getCurrentPlayer());
88: }
89:
90: @Override
91: public Optional<Boolean> getIsGameOver(final TicTacToeState state) {
92: return Optional.of(state.computeNextPlayers().isEmpty());
93: }
94: }