findSetsWithTotalPoints now finds sets (and total points)

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@341 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Bennet Gerlach 2011-05-31 03:45:21 +02:00
parent eea3cb2188
commit 278edc37a9
2 changed files with 171 additions and 61 deletions

View file

@ -1,12 +1,15 @@
package jrummikub.control; package jrummikub.control;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.TreeMap; import java.util.TreeMap;
import jrummikub.model.Stone; import jrummikub.model.Stone;
import jrummikub.model.StoneColor; import jrummikub.model.StoneColor;
import jrummikub.model.StoneSet;
import jrummikub.util.Pair; import jrummikub.util.Pair;
/** /**
@ -18,8 +21,22 @@ public class AIUtil {
private AIUtil() { private AIUtil() {
} }
private static class SearchState {
int pointsMissing;
TreeMap<Pair<Integer, StoneColor>, Integer> stoneCounts;
int jokerCount;
SearchState(int pointsMissing,
TreeMap<Pair<Integer, StoneColor>, Integer> stoneCounts, int jokerCount) {
this.pointsMissing = pointsMissing;
this.stoneCounts = stoneCounts;
this.jokerCount = jokerCount;
}
}
/** /**
* Finds sets with a certain total of points * 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 * @param pointsMissing
* the desired number of points * the desired number of points
@ -27,19 +44,25 @@ public class AIUtil {
* the number of each stone * the number of each stone
* @param jokerCount * @param jokerCount
* the total number of jokers in the game * the total number of jokers in the game
* @return whether such sets exist * @return the sets that have the desired point total or the highest found
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static boolean findSetsWithTotalPoints(int pointsMissing, public static Pair<List<StoneSet>, Integer> findSetsWithTotalPoints(
int pointsMissing,
TreeMap<Pair<Integer, StoneColor>, Integer> stoneCounts, int jokerCount) { TreeMap<Pair<Integer, StoneColor>, Integer> stoneCounts, int jokerCount) {
if (pointsMissing <= 0) { if (pointsMissing <= 0) {
return true; return new Pair<List<StoneSet>, Integer>(
Collections.<StoneSet> emptyList(), 0);
} }
stoneCounts = (TreeMap<Pair<Integer, StoneColor>, Integer>) stoneCounts stoneCounts = (TreeMap<Pair<Integer, StoneColor>, Integer>) stoneCounts
.clone(); .clone();
Pair<List<StoneSet>, Integer> result, bestResult;
bestResult = new Pair<List<StoneSet>, Integer>(
Collections.<StoneSet> emptyList(), 0);
for (int value = 13; value >= 1; value--) { for (int value = 13; value >= 1; value--) {
for (StoneColor color : StoneColor.values()) { for (StoneColor color : StoneColor.values()) {
Pair<Integer, StoneColor> stone = new Pair<Integer, StoneColor>(value, Pair<Integer, StoneColor> stone = new Pair<Integer, StoneColor>(value,
@ -48,90 +71,172 @@ public class AIUtil {
if (stoneCounts.containsKey(stone)) { if (stoneCounts.containsKey(stone)) {
decrementStoneCount(stoneCounts, stone); decrementStoneCount(stoneCounts, stone);
if (findRunsWithTotalPoints(pointsMissing - value, stoneCounts, result = findRunsWithTotalPoints(new SearchState(pointsMissing
jokerCount, stone, 1)) - value, stoneCounts, jokerCount), stone, 1);
return true; if (result.getSecond() >= pointsMissing)
if (findGroupsWithTotalPoints(pointsMissing - value, stoneCounts, return result;
jokerCount, stone, 1)) if (result.getSecond() > bestResult.getSecond())
return true; bestResult = result;
result = findGroupsWithTotalPoints(new SearchState(pointsMissing
- value, stoneCounts, jokerCount), value,
Collections.singletonList(color), color);
if (result.getSecond() >= pointsMissing)
return result;
if (result.getSecond() > bestResult.getSecond())
bestResult = result;
} }
if (jokerCount > 0) { if (jokerCount > 0) {
if (findRunsWithTotalPoints(pointsMissing - value, stoneCounts, result = findRunsWithTotalPoints(new SearchState(pointsMissing
jokerCount - 1, stone, 1)) - value, stoneCounts, jokerCount - 1), stone, 1);
return true; if (result.getSecond() >= pointsMissing)
if (findGroupsWithTotalPoints(pointsMissing - value, stoneCounts, return result;
jokerCount - 1, stone, 1)) if (result.getSecond() > bestResult.getSecond())
return true; bestResult = result;
result = findGroupsWithTotalPoints(new SearchState(pointsMissing
- value, stoneCounts, jokerCount - 1), value,
Collections.singletonList(color), color);
if (result.getSecond() >= pointsMissing)
return result;
if (result.getSecond() > bestResult.getSecond())
bestResult = result;
} }
} }
} }
return false; return bestResult;
} }
static boolean findGroupsWithTotalPoints(int pointsMissing, private static Pair<List<StoneSet>, Integer> findGroupsWithTotalPoints(
TreeMap<Pair<Integer, StoneColor>, Integer> stoneCounts, int jokerCount, SearchState searchState, int value, List<StoneColor> chosenColors,
Pair<Integer, StoneColor> stone, int groupSize) { StoneColor testedColor) {
StoneColor nextColor = getNextColor(stone.getSecond()); StoneColor nextColor = getNextColor(testedColor);
Pair<Integer, StoneColor> nextStone = new Pair<Integer, StoneColor>( Pair<Integer, StoneColor> nextStone = new Pair<Integer, StoneColor>(value,
stone.getFirst(), nextColor); nextColor);
Pair<List<StoneSet>, Integer> result, bestResult;
bestResult = new Pair<List<StoneSet>, Integer>(
Collections.<StoneSet> emptyList(), 0);
if (nextColor != null) { if (nextColor != null) {
if (stoneCounts.containsKey(nextStone)) { if (searchState.stoneCounts.containsKey(nextStone)) {
decrementStoneCount(stoneCounts, nextStone); decrementStoneCount(searchState.stoneCounts, nextStone);
if (findGroupsWithTotalPoints(pointsMissing - stone.getFirst(), List<StoneColor> newColors = new ArrayList<StoneColor>(chosenColors);
stoneCounts, jokerCount, nextStone, groupSize + 1)) newColors.add(nextColor);
return true; result = findGroupsWithTotalPoints(new SearchState(
incrementStoneCount(stoneCounts, nextStone); searchState.pointsMissing, searchState.stoneCounts,
searchState.jokerCount), value, newColors, nextColor);
if (result.getSecond() >= searchState.pointsMissing)
return result;
if (result.getSecond() > bestResult.getSecond())
bestResult = result;
incrementStoneCount(searchState.stoneCounts, nextStone);
} }
if (jokerCount > 0) { if (searchState.jokerCount > 0) {
if (findGroupsWithTotalPoints(pointsMissing - stone.getFirst(), List<StoneColor> newColors = new ArrayList<StoneColor>(chosenColors);
stoneCounts, jokerCount - 1, nextStone, groupSize + 1)) newColors.add(nextColor);
return true; result = findGroupsWithTotalPoints(new SearchState(
searchState.pointsMissing, searchState.stoneCounts,
searchState.jokerCount - 1), value, newColors, nextColor);
if (result.getSecond() >= searchState.pointsMissing)
return result;
if (result.getSecond() > bestResult.getSecond())
bestResult = result;
} }
if (findGroupsWithTotalPoints(pointsMissing, stoneCounts, jokerCount, result = findGroupsWithTotalPoints(new SearchState(
nextStone, groupSize)) searchState.pointsMissing, searchState.stoneCounts,
return true; searchState.jokerCount), value, chosenColors, nextColor);
if (result.getSecond() >= searchState.pointsMissing)
return result;
if (result.getSecond() > bestResult.getSecond())
bestResult = result;
} }
if (groupSize >= 3) { if (chosenColors.size() >= 3) {
if (findSetsWithTotalPoints(pointsMissing, stoneCounts, jokerCount)) int groupPoints = chosenColors.size() * value;
return true; result = findSetsWithTotalPoints(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());
newResultList.add(new StoneSet(newStones));
result = new Pair<List<StoneSet>, Integer>(newResultList,
result.getSecond() + groupPoints);
if (result.getSecond() >= searchState.pointsMissing)
return result;
if (result.getSecond() > bestResult.getSecond())
bestResult = result;
} }
return false; return bestResult;
} }
static boolean findRunsWithTotalPoints(int pointsMissing, private static Pair<List<StoneSet>, Integer> findRunsWithTotalPoints(
TreeMap<Pair<Integer, StoneColor>, Integer> stoneCounts, int jokerCount, SearchState searchState, Pair<Integer, StoneColor> testedStone,
Pair<Integer, StoneColor> stone, int runLength) { int runLength) {
Pair<List<StoneSet>, Integer> result, bestResult;
bestResult = new Pair<List<StoneSet>, Integer>(
Collections.<StoneSet> emptyList(), 0);
Pair<Integer, StoneColor> nextStone = null; Pair<Integer, StoneColor> nextStone = null;
if (stone.getFirst() > 1) { if (testedStone.getFirst() > 1) {
int nextValue = stone.getFirst() - 1; int nextValue = testedStone.getFirst() - 1;
nextStone = new Pair<Integer, StoneColor>(nextValue, stone.getSecond()); nextStone = new Pair<Integer, StoneColor>(nextValue,
testedStone.getSecond());
if (stoneCounts.containsKey(nextStone)) { if (searchState.stoneCounts.containsKey(nextStone)) {
decrementStoneCount(stoneCounts, nextStone); decrementStoneCount(searchState.stoneCounts, nextStone);
if (findRunsWithTotalPoints(pointsMissing - nextValue, stoneCounts, result = findRunsWithTotalPoints(new SearchState(
jokerCount, nextStone, runLength + 1)) searchState.pointsMissing, searchState.stoneCounts,
return true; searchState.jokerCount), nextStone, runLength + 1);
incrementStoneCount(stoneCounts, nextStone); if (result.getSecond() >= searchState.pointsMissing)
return result;
if (result.getSecond() > bestResult.getSecond())
bestResult = result;
incrementStoneCount(searchState.stoneCounts, nextStone);
} }
if (jokerCount > 0) { if (searchState.jokerCount > 0) {
if (findRunsWithTotalPoints(pointsMissing - nextValue, stoneCounts, result = findRunsWithTotalPoints(new SearchState(
jokerCount - 1, nextStone, runLength + 1)) searchState.pointsMissing, searchState.stoneCounts,
return true; searchState.jokerCount - 1), nextStone, runLength + 1);
if (result.getSecond() >= searchState.pointsMissing)
return result;
if (result.getSecond() > bestResult.getSecond())
bestResult = result;
} }
} }
if (runLength >= 3) { if (runLength >= 3) {
if (findSetsWithTotalPoints(pointsMissing, stoneCounts, jokerCount))
return true; int below = testedStone.getFirst() - 1;
int high = below + runLength;
int runPoints = (high * (high + 1)) / 2 - (below * (below + 1)) / 2;
result = findSetsWithTotalPoints(searchState.pointsMissing - runPoints,
searchState.stoneCounts, searchState.jokerCount);
List<Stone> newStones = new ArrayList<Stone>();
for (int i = 0; i < runLength; i++) {
newStones.add(new Stone(i + testedStone.getFirst(), testedStone
.getSecond()));
}
List<StoneSet> newResultList = new ArrayList<StoneSet>(result.getFirst());
newResultList.add(new StoneSet(newStones));
result = new Pair<List<StoneSet>, Integer>(newResultList,
result.getSecond() + runPoints);
if (result.getSecond() >= searchState.pointsMissing)
return result;
if (result.getSecond() > bestResult.getSecond())
bestResult = result;
} }
return false; return bestResult;
} }
static void incrementStoneCount( static void incrementStoneCount(

View file

@ -99,8 +99,13 @@ public class Hand extends StoneTray<Stone> implements IHand {
Pair<TreeMap<Pair<Integer, StoneColor>, Integer>, Integer> stoneCounts = AIUtil Pair<TreeMap<Pair<Integer, StoneColor>, Integer>, Integer> stoneCounts = AIUtil
.countStones(stones); .countStones(stones);
return AIUtil.findSetsWithTotalPoints(settings.getInitialMeldThreshold(), Pair<List<StoneSet>, Integer> result = AIUtil.findSetsWithTotalPoints(
stoneCounts.getFirst(), stoneCounts.getSecond()); settings.getInitialMeldThreshold(), stoneCounts.getFirst(),
stoneCounts.getSecond());
System.out.println(result);
return (result.getSecond() >= settings.getInitialMeldThreshold());
} }
@Override @Override