Skip to contentMethod: C4SolutionEvaluator(C4BoardSlim)
      1: package de.fhdw.gaming.ipspiel23.c4.domain.impl.evaluation;
2: 
3: import java.util.HashSet;
4: import java.util.Set;
5: 
6: import de.fhdw.gaming.ipspiel23.c4.domain.IC4SolutionSlim;
7: import de.fhdw.gaming.ipspiel23.c4.domain.impl.C4BoardSlim;
8: 
9: /**
10:  * A state-less class that evaluates a given board for solutions.
11:  * <p>
12:  * Note: This is an internal API that may be subject to incompatible changes in future releases.
13:  * </p>
14:  */
15: public class C4SolutionEvaluator {
16:     
17:     /**
18:      * The evaluators for every direction: horizontal, vertical, diagonal left, diagonal right.
19:      */
20:     private final C4SolutionAnalyzer[] directionEvaluators;
21:     
22:     /**
23:      * Creates a new solution evaluator for the specified board.
24:      * @param board the board
25:      */
26:     public C4SolutionEvaluator(final C4BoardSlim board) {
27:         this.directionEvaluators = new C4SolutionAnalyzer[] {
28:             new C4SolutionAnalyzerHorizontal(board),
29:             new C4SolutionAnalyzerVertical(board),
30:             new C4SolutionAnalyzerDiagonalRight(board),
31:             new C4SolutionAnalyzerDiagonalLeft(board)
32:         };
33:     }
34:     
35:     /**
36:      * Resets the analyzer cache.
37:      */
38:     public void resetAnalyzerCaches() {
39:         for (final C4SolutionAnalyzer analyzer : directionEvaluators) {
40:             analyzer.resetCache();
41:         }
42:     }
43: 
44:     /**
45:      * Lazily identifies the first solution, if any.
46:      * @param updateCache Whether to update the solution cache, preventing unsolvable lines from being scanned again.
47:      * @return the first solution or {@code null} if no solution was found
48:      * @apiNote This is an internal API that may be subject to incompatible changes in future releases.
49:      */
50:     public IC4SolutionSlim tryFindFirstSolution(final boolean updateCache) {
51:         IC4SolutionSlim solution = null;
52:         for (final C4SolutionAnalyzer analyzer : this.directionEvaluators) {
53:             solution = analyzer.tryFindFirstSolution(solution, updateCache);
54:         }
55:         return solution;
56:     }
57:     
58:     /**
59:      * Eagerly identifies all solutions on the board.
60:      * @param updateCache Whether to update the solution cache, preventing unsolvable lines from being scanned again.
61:      * @return the set of all solutions.
62:      * @apiNote This is an internal API that may be subject to incompatible changes in future releases.
63:      */
64:     public Set<IC4SolutionSlim> findAllSolutions(final boolean updateCache) {
65:         final Set<IC4SolutionSlim> solutions = new HashSet<>();
66:         for (final C4SolutionAnalyzer analyzer : this.directionEvaluators) {
67:             analyzer.findAllSolutions(solutions, updateCache);
68:         }
69:         return solutions;
70:     }
71: }