summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control/AIUtil.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/jrummikub/control/AIUtil.java')
-rw-r--r--src/jrummikub/control/AIUtil.java38
1 files changed, 26 insertions, 12 deletions
diff --git a/src/jrummikub/control/AIUtil.java b/src/jrummikub/control/AIUtil.java
index bf6a078..1501483 100644
--- a/src/jrummikub/control/AIUtil.java
+++ b/src/jrummikub/control/AIUtil.java
@@ -7,18 +7,33 @@ import java.util.Comparator;
import java.util.List;
import java.util.TreeMap;
+import jrummikub.model.GameSettings;
import jrummikub.model.Stone;
import jrummikub.model.StoneColor;
import jrummikub.model.StoneSet;
import jrummikub.util.Pair;
/**
- * A collection of several AI utility methods
+ * A collection of several AI utility methods with given game settings
*
*/
public class AIUtil {
- private AIUtil() {
+ GameSettings settings;
+ List<StoneColor> stoneColors;
+
+ /**
+ * The utility class's methods calculate their results based on the game
+ * settings
+ *
+ * @param settings
+ * the underlying game settings
+ */
+ public AIUtil(GameSettings settings) {
+ this.settings = settings;
+
+ stoneColors = new ArrayList<StoneColor>(Arrays.asList(StoneColor.values()));
+ stoneColors.retainAll(settings.getStoneColors());
}
private static class SearchState {
@@ -47,7 +62,7 @@ public class AIUtil {
* @return the sets that have the desired point total or the highest found
*/
@SuppressWarnings("unchecked")
- public static Pair<List<StoneSet>, Integer> findSetsWithTotalPoints(
+ public Pair<List<StoneSet>, Integer> findSetsWithTotalPoints(
int pointsMissing,
TreeMap<Pair<Integer, StoneColor>, Integer> stoneCounts, int jokerCount) {
@@ -63,14 +78,13 @@ public class AIUtil {
bestResult = new Pair<List<StoneSet>, Integer>(
Collections.<StoneSet> emptyList(), 0);
- for (int value = 13; value >= 1; value--) {
- for (StoneColor color : StoneColor.values()) {
+ for (int value = settings.getHighestValue(); value >= 1; value--) {
+ for (StoneColor color : stoneColors) {
Pair<Integer, StoneColor> stone = new Pair<Integer, StoneColor>(value,
color);
if (stoneCounts.containsKey(stone)) {
decrementStoneCount(stoneCounts, stone);
-
result = findRunsWithTotalPoints(new SearchState(pointsMissing
- value, stoneCounts, jokerCount), stone, 1);
if (result.getSecond() >= pointsMissing)
@@ -109,7 +123,7 @@ public class AIUtil {
return bestResult;
}
- private static Pair<List<StoneSet>, Integer> findGroupsWithTotalPoints(
+ private Pair<List<StoneSet>, Integer> findGroupsWithTotalPoints(
SearchState searchState, int value, List<StoneColor> chosenColors,
StoneColor testedColor) {
@@ -175,7 +189,7 @@ public class AIUtil {
return bestResult;
}
- private static Pair<List<StoneSet>, Integer> findRunsWithTotalPoints(
+ private Pair<List<StoneSet>, Integer> findRunsWithTotalPoints(
SearchState searchState, Pair<Integer, StoneColor> testedStone,
int runLength) {
@@ -262,12 +276,12 @@ public class AIUtil {
}
}
- static StoneColor getNextColor(StoneColor color) {
- int index = Arrays.binarySearch(StoneColor.values(), color) + 1;
- if (index >= StoneColor.values().length) {
+ StoneColor getNextColor(StoneColor color) {
+ int index = stoneColors.indexOf(color) + 1;
+ if (index >= stoneColors.size()) {
return null;
}
- return StoneColor.values()[index];
+ return stoneColors.get(index);
}
private final static Comparator<Pair<Integer, StoneColor>> comparator = new Comparator<Pair<Integer, StoneColor>>() {