summaryrefslogtreecommitdiffstats
path: root/src/jrummikub
diff options
context:
space:
mode:
Diffstat (limited to 'src/jrummikub')
-rw-r--r--src/jrummikub/control/AIUtil.java128
1 files changed, 57 insertions, 71 deletions
diff --git a/src/jrummikub/control/AIUtil.java b/src/jrummikub/control/AIUtil.java
index 2205318..393aa95 100644
--- a/src/jrummikub/control/AIUtil.java
+++ b/src/jrummikub/control/AIUtil.java
@@ -18,22 +18,20 @@ import jrummikub.util.Pair;
*
*/
public class AIUtil {
-
- GameSettings settings;
- List<StoneColor> stoneColors;
+ private GameSettings settings;
+ private List<StoneColor> stoneColors;
/**
* The utility class's methods calculate their results based on the game
* settings
*
* @param settings
- * the underlying game settings
+ * the underlying game settings
*/
public AIUtil(GameSettings settings) {
this.settings = settings;
- stoneColors = new ArrayList<StoneColor>(Arrays.asList(StoneColor
- .values()));
+ stoneColors = new ArrayList<StoneColor>(Arrays.asList(StoneColor.values()));
stoneColors.retainAll(settings.getStoneColors());
}
@@ -43,8 +41,7 @@ public class AIUtil {
int jokerCount;
SearchState(int pointsMissing,
- TreeMap<Pair<Integer, StoneColor>, Integer> stoneCounts,
- int jokerCount) {
+ TreeMap<Pair<Integer, StoneColor>, Integer> stoneCounts, int jokerCount) {
this.pointsMissing = pointsMissing;
this.stoneCounts = stoneCounts;
this.jokerCount = jokerCount;
@@ -65,22 +62,21 @@ public class AIUtil {
}
/**
- * Tries to find sets with a certain total of points. If only lower totals
- * are found, returns the sets with the highest cumulated point total.
+ * Tries to find sets with a certain total of points. If only lower totals are
+ * found, returns the sets with the highest cumulated point total.
*
* @param pointsMissing
- * the desired number of points
+ * the desired number of points
* @param stoneCounts
- * the number of each stone
+ * the number of each stone
* @param jokerCount
- * the total number of jokers in the game
+ * the total number of jokers in the game
* @return the sets that have the desired point total or the highest found
*/
@SuppressWarnings("unchecked")
public Pair<List<StoneSet>, Integer> findSetsWithTotalPoints(
int pointsMissing,
- TreeMap<Pair<Integer, StoneColor>, Integer> stoneCounts,
- int jokerCount) {
+ TreeMap<Pair<Integer, StoneColor>, Integer> stoneCounts, int jokerCount) {
Pair<List<StoneSet>, Integer> emptyResult = new Pair<List<StoneSet>, Integer>(
Collections.<StoneSet> emptyList(), 0);
if (pointsMissing <= 0)
@@ -89,9 +85,9 @@ public class AIUtil {
stoneCounts = (TreeMap<Pair<Integer, StoneColor>, Integer>) stoneCounts
.clone();
- SearchHelper searchHelper = new SearchHelper(pointsMissing,
- new Pair<List<StoneSet>, Integer>(
- Collections.<StoneSet> emptyList(), 0));
+ SearchHelper searchHelper = new SearchHelper(
+ pointsMissing,
+ new Pair<List<StoneSet>, Integer>(Collections.<StoneSet> emptyList(), 0));
try {
for (int value = settings.getHighestValue(); value >= 1; value--) {
for (StoneColor color : stoneColors) {
@@ -99,26 +95,24 @@ public class AIUtil {
value, color);
if (stoneCounts.containsKey(stone)) {
- SearchState searchState = new SearchState(pointsMissing
- - value, stoneCounts, jokerCount);
+ SearchState searchState = new SearchState(pointsMissing - value,
+ stoneCounts, jokerCount);
decrementStoneCount(stoneCounts, stone);
- searchHelper.checkResult(findRunsWithTotalPoints(
- searchState, stone, 1));
+ searchHelper.checkResult(findRunsWithTotalPoints(searchState,
+ stone, 1));
- searchHelper.checkResult(findGroupsWithTotalPoints(
- searchState, value,
- Collections.singletonList(color), color));
+ searchHelper.checkResult(findGroupsWithTotalPoints(searchState,
+ value, Collections.singletonList(color), color));
}
if (jokerCount > 0) {
- SearchState searchState = new SearchState(pointsMissing
- - value, stoneCounts, jokerCount - 1);
- searchHelper.checkResult(findRunsWithTotalPoints(
- searchState, stone, 1));
-
- searchHelper.checkResult(findGroupsWithTotalPoints(
- searchState, value,
- Collections.singletonList(color), color));
+ SearchState searchState = new SearchState(pointsMissing - value,
+ stoneCounts, jokerCount - 1);
+ searchHelper.checkResult(findRunsWithTotalPoints(searchState,
+ stone, 1));
+
+ searchHelper.checkResult(findGroupsWithTotalPoints(searchState,
+ value, Collections.singletonList(color), color));
}
}
}
@@ -156,32 +150,29 @@ public class AIUtil {
StoneColor testedColor) {
StoneColor nextColor = getNextColor(testedColor);
- Pair<Integer, StoneColor> nextStone = new Pair<Integer, StoneColor>(
- value, nextColor);
+ Pair<Integer, StoneColor> nextStone = new Pair<Integer, StoneColor>(value,
+ nextColor);
- SearchHelper searchHelper = new SearchHelper(searchState.pointsMissing,
- new Pair<List<StoneSet>, Integer>(
- Collections.<StoneSet> emptyList(), 0));
+ SearchHelper searchHelper = new SearchHelper(
+ searchState.pointsMissing,
+ new Pair<List<StoneSet>, Integer>(Collections.<StoneSet> emptyList(), 0));
try {
if (nextColor != null) {
- List<StoneColor> newColors = new ArrayList<StoneColor>(
- chosenColors);
+ List<StoneColor> newColors = new ArrayList<StoneColor>(chosenColors);
newColors.add(nextColor);
if (searchState.stoneCounts.containsKey(nextStone)) {
decrementStoneCount(searchState.stoneCounts, nextStone);
- searchHelper.checkResult(findGroupsWithTotalPoints(
- searchState, value, newColors, nextColor));
+ searchHelper.checkResult(findGroupsWithTotalPoints(searchState,
+ value, newColors, nextColor));
incrementStoneCount(searchState.stoneCounts, nextStone);
}
if (searchState.jokerCount > 0) {
- searchHelper.checkResult(findGroupsWithTotalPoints(
- new SearchState(searchState.pointsMissing,
- searchState.stoneCounts,
- searchState.jokerCount - 1), value,
- newColors, nextColor));
+ searchHelper.checkResult(findGroupsWithTotalPoints(new SearchState(
+ searchState.pointsMissing, searchState.stoneCounts,
+ searchState.jokerCount - 1), value, newColors, nextColor));
}
- searchHelper.checkResult(findGroupsWithTotalPoints(searchState,
- value, chosenColors, nextColor));
+ searchHelper.checkResult(findGroupsWithTotalPoints(searchState, value,
+ chosenColors, nextColor));
}
if (chosenColors.size() >= 3) {
@@ -198,14 +189,13 @@ public class AIUtil {
SearchState searchState, int value, List<StoneColor> chosenColors) {
int groupPoints = chosenColors.size() * value;
Pair<List<StoneSet>, Integer> result = findSetsWithTotalPoints(
- searchState.pointsMissing - groupPoints,
- searchState.stoneCounts, searchState.jokerCount);
+ searchState.pointsMissing - groupPoints, searchState.stoneCounts,
+ searchState.jokerCount);
List<Stone> newStones = new ArrayList<Stone>();
for (StoneColor color : chosenColors) {
newStones.add(new Stone(value, color));
}
- List<StoneSet> newResultList = new ArrayList<StoneSet>(
- result.getFirst());
+ List<StoneSet> newResultList = new ArrayList<StoneSet>(result.getFirst());
newResultList.add(new StoneSet(newStones));
result = new Pair<List<StoneSet>, Integer>(newResultList,
result.getSecond() + groupPoints);
@@ -216,9 +206,9 @@ public class AIUtil {
SearchState searchState, Pair<Integer, StoneColor> testedStone,
int runLength) {
- SearchHelper searchHelper = new SearchHelper(searchState.pointsMissing,
- new Pair<List<StoneSet>, Integer>(
- Collections.<StoneSet> emptyList(), 0));
+ SearchHelper searchHelper = new SearchHelper(
+ searchState.pointsMissing,
+ new Pair<List<StoneSet>, Integer>(Collections.<StoneSet> emptyList(), 0));
try {
Pair<Integer, StoneColor> nextStone = null;
if (testedStone.getFirst() > 1) {
@@ -228,24 +218,22 @@ public class AIUtil {
if (searchState.stoneCounts.containsKey(nextStone)) {
decrementStoneCount(searchState.stoneCounts, nextStone);
- searchHelper.checkResult(findRunsWithTotalPoints(
- searchState, nextStone, runLength + 1));
+ searchHelper.checkResult(findRunsWithTotalPoints(searchState,
+ nextStone, runLength + 1));
incrementStoneCount(searchState.stoneCounts, nextStone);
}
if (searchState.jokerCount > 0) {
- searchHelper.checkResult(findRunsWithTotalPoints(
- new SearchState(searchState.pointsMissing,
- searchState.stoneCounts,
- searchState.jokerCount - 1), nextStone,
- runLength + 1));
+ searchHelper.checkResult(findRunsWithTotalPoints(new SearchState(
+ searchState.pointsMissing, searchState.stoneCounts,
+ searchState.jokerCount - 1), nextStone, runLength + 1));
}
}
if (runLength >= 3) {
- searchHelper.checkResult(handleFoundRun(searchState,
- testedStone, runLength));
+ searchHelper.checkResult(handleFoundRun(searchState, testedStone,
+ runLength));
}
} catch (EnoughPoints enoughPoints) {
return enoughPoints.getResult();
@@ -253,9 +241,8 @@ public class AIUtil {
return searchHelper.getBestResult();
}
- private Pair<List<StoneSet>, Integer> handleFoundRun(
- SearchState searchState, Pair<Integer, StoneColor> testedStone,
- int runLength) {
+ private Pair<List<StoneSet>, Integer> handleFoundRun(SearchState searchState,
+ Pair<Integer, StoneColor> testedStone, int runLength) {
int below = testedStone.getFirst() - 1;
int high = below + runLength;
int runPoints = (high * (high + 1)) / 2 - (below * (below + 1)) / 2;
@@ -269,8 +256,7 @@ public class AIUtil {
newStones.add(new Stone(i + testedStone.getFirst(), testedStone
.getSecond()));
}
- List<StoneSet> newResultList = new ArrayList<StoneSet>(
- result.getFirst());
+ List<StoneSet> newResultList = new ArrayList<StoneSet>(result.getFirst());
newResultList.add(new StoneSet(newStones));
result = new Pair<List<StoneSet>, Integer>(newResultList,
result.getSecond() + runPoints);
@@ -326,7 +312,7 @@ public class AIUtil {
* Counts the numbers of stones
*
* @param stones
- * the stones to count
+ * the stones to count
* @return the numbers for all stones
*/
public static Pair<TreeMap<Pair<Integer, StoneColor>, Integer>, Integer> countStones(