AIUtil: Fix attribute visibility
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@369 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
f343e6dc5e
commit
996892670d
1 changed files with 56 additions and 70 deletions
|
@ -18,22 +18,20 @@ import jrummikub.util.Pair;
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class AIUtil {
|
public class AIUtil {
|
||||||
|
private GameSettings settings;
|
||||||
GameSettings settings;
|
private List<StoneColor> stoneColors;
|
||||||
List<StoneColor> stoneColors;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The utility class's methods calculate their results based on the game
|
* The utility class's methods calculate their results based on the game
|
||||||
* settings
|
* settings
|
||||||
*
|
*
|
||||||
* @param settings
|
* @param settings
|
||||||
* the underlying game settings
|
* the underlying game settings
|
||||||
*/
|
*/
|
||||||
public AIUtil(GameSettings settings) {
|
public AIUtil(GameSettings settings) {
|
||||||
this.settings = settings;
|
this.settings = settings;
|
||||||
|
|
||||||
stoneColors = new ArrayList<StoneColor>(Arrays.asList(StoneColor
|
stoneColors = new ArrayList<StoneColor>(Arrays.asList(StoneColor.values()));
|
||||||
.values()));
|
|
||||||
stoneColors.retainAll(settings.getStoneColors());
|
stoneColors.retainAll(settings.getStoneColors());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,8 +41,7 @@ public class AIUtil {
|
||||||
int jokerCount;
|
int jokerCount;
|
||||||
|
|
||||||
SearchState(int pointsMissing,
|
SearchState(int pointsMissing,
|
||||||
TreeMap<Pair<Integer, StoneColor>, Integer> stoneCounts,
|
TreeMap<Pair<Integer, StoneColor>, Integer> stoneCounts, int jokerCount) {
|
||||||
int jokerCount) {
|
|
||||||
this.pointsMissing = pointsMissing;
|
this.pointsMissing = pointsMissing;
|
||||||
this.stoneCounts = stoneCounts;
|
this.stoneCounts = stoneCounts;
|
||||||
this.jokerCount = jokerCount;
|
this.jokerCount = jokerCount;
|
||||||
|
@ -65,22 +62,21 @@ public class AIUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tries to find sets with a certain total of points. If only lower totals
|
* Tries to find sets with a certain total of points. If only lower totals are
|
||||||
* are found, returns the sets with the highest cumulated point total.
|
* found, returns the sets with the highest cumulated point total.
|
||||||
*
|
*
|
||||||
* @param pointsMissing
|
* @param pointsMissing
|
||||||
* the desired number of points
|
* the desired number of points
|
||||||
* @param stoneCounts
|
* @param stoneCounts
|
||||||
* 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 the sets that have the desired point total or the highest found
|
* @return the sets that have the desired point total or the highest found
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public Pair<List<StoneSet>, Integer> findSetsWithTotalPoints(
|
public Pair<List<StoneSet>, Integer> findSetsWithTotalPoints(
|
||||||
int pointsMissing,
|
int pointsMissing,
|
||||||
TreeMap<Pair<Integer, StoneColor>, Integer> stoneCounts,
|
TreeMap<Pair<Integer, StoneColor>, Integer> stoneCounts, int jokerCount) {
|
||||||
int jokerCount) {
|
|
||||||
Pair<List<StoneSet>, Integer> emptyResult = new Pair<List<StoneSet>, Integer>(
|
Pair<List<StoneSet>, Integer> emptyResult = new Pair<List<StoneSet>, Integer>(
|
||||||
Collections.<StoneSet> emptyList(), 0);
|
Collections.<StoneSet> emptyList(), 0);
|
||||||
if (pointsMissing <= 0)
|
if (pointsMissing <= 0)
|
||||||
|
@ -89,9 +85,9 @@ public class AIUtil {
|
||||||
stoneCounts = (TreeMap<Pair<Integer, StoneColor>, Integer>) stoneCounts
|
stoneCounts = (TreeMap<Pair<Integer, StoneColor>, Integer>) stoneCounts
|
||||||
.clone();
|
.clone();
|
||||||
|
|
||||||
SearchHelper searchHelper = new SearchHelper(pointsMissing,
|
SearchHelper searchHelper = new SearchHelper(
|
||||||
new Pair<List<StoneSet>, Integer>(
|
pointsMissing,
|
||||||
Collections.<StoneSet> emptyList(), 0));
|
new Pair<List<StoneSet>, Integer>(Collections.<StoneSet> emptyList(), 0));
|
||||||
try {
|
try {
|
||||||
for (int value = settings.getHighestValue(); value >= 1; value--) {
|
for (int value = settings.getHighestValue(); value >= 1; value--) {
|
||||||
for (StoneColor color : stoneColors) {
|
for (StoneColor color : stoneColors) {
|
||||||
|
@ -99,26 +95,24 @@ public class AIUtil {
|
||||||
value, color);
|
value, color);
|
||||||
|
|
||||||
if (stoneCounts.containsKey(stone)) {
|
if (stoneCounts.containsKey(stone)) {
|
||||||
SearchState searchState = new SearchState(pointsMissing
|
SearchState searchState = new SearchState(pointsMissing - value,
|
||||||
- value, stoneCounts, jokerCount);
|
stoneCounts, jokerCount);
|
||||||
decrementStoneCount(stoneCounts, stone);
|
decrementStoneCount(stoneCounts, stone);
|
||||||
searchHelper.checkResult(findRunsWithTotalPoints(
|
searchHelper.checkResult(findRunsWithTotalPoints(searchState,
|
||||||
searchState, stone, 1));
|
stone, 1));
|
||||||
|
|
||||||
searchHelper.checkResult(findGroupsWithTotalPoints(
|
searchHelper.checkResult(findGroupsWithTotalPoints(searchState,
|
||||||
searchState, value,
|
value, Collections.singletonList(color), color));
|
||||||
Collections.singletonList(color), color));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (jokerCount > 0) {
|
if (jokerCount > 0) {
|
||||||
SearchState searchState = new SearchState(pointsMissing
|
SearchState searchState = new SearchState(pointsMissing - value,
|
||||||
- value, stoneCounts, jokerCount - 1);
|
stoneCounts, jokerCount - 1);
|
||||||
searchHelper.checkResult(findRunsWithTotalPoints(
|
searchHelper.checkResult(findRunsWithTotalPoints(searchState,
|
||||||
searchState, stone, 1));
|
stone, 1));
|
||||||
|
|
||||||
searchHelper.checkResult(findGroupsWithTotalPoints(
|
searchHelper.checkResult(findGroupsWithTotalPoints(searchState,
|
||||||
searchState, value,
|
value, Collections.singletonList(color), color));
|
||||||
Collections.singletonList(color), color));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -156,32 +150,29 @@ public class AIUtil {
|
||||||
StoneColor testedColor) {
|
StoneColor testedColor) {
|
||||||
|
|
||||||
StoneColor nextColor = getNextColor(testedColor);
|
StoneColor nextColor = getNextColor(testedColor);
|
||||||
Pair<Integer, StoneColor> nextStone = new Pair<Integer, StoneColor>(
|
Pair<Integer, StoneColor> nextStone = new Pair<Integer, StoneColor>(value,
|
||||||
value, nextColor);
|
nextColor);
|
||||||
|
|
||||||
SearchHelper searchHelper = new SearchHelper(searchState.pointsMissing,
|
SearchHelper searchHelper = new SearchHelper(
|
||||||
new Pair<List<StoneSet>, Integer>(
|
searchState.pointsMissing,
|
||||||
Collections.<StoneSet> emptyList(), 0));
|
new Pair<List<StoneSet>, Integer>(Collections.<StoneSet> emptyList(), 0));
|
||||||
try {
|
try {
|
||||||
if (nextColor != null) {
|
if (nextColor != null) {
|
||||||
List<StoneColor> newColors = new ArrayList<StoneColor>(
|
List<StoneColor> newColors = new ArrayList<StoneColor>(chosenColors);
|
||||||
chosenColors);
|
|
||||||
newColors.add(nextColor);
|
newColors.add(nextColor);
|
||||||
if (searchState.stoneCounts.containsKey(nextStone)) {
|
if (searchState.stoneCounts.containsKey(nextStone)) {
|
||||||
decrementStoneCount(searchState.stoneCounts, nextStone);
|
decrementStoneCount(searchState.stoneCounts, nextStone);
|
||||||
searchHelper.checkResult(findGroupsWithTotalPoints(
|
searchHelper.checkResult(findGroupsWithTotalPoints(searchState,
|
||||||
searchState, value, newColors, nextColor));
|
value, newColors, nextColor));
|
||||||
incrementStoneCount(searchState.stoneCounts, nextStone);
|
incrementStoneCount(searchState.stoneCounts, nextStone);
|
||||||
}
|
}
|
||||||
if (searchState.jokerCount > 0) {
|
if (searchState.jokerCount > 0) {
|
||||||
searchHelper.checkResult(findGroupsWithTotalPoints(
|
searchHelper.checkResult(findGroupsWithTotalPoints(new SearchState(
|
||||||
new SearchState(searchState.pointsMissing,
|
searchState.pointsMissing, searchState.stoneCounts,
|
||||||
searchState.stoneCounts,
|
searchState.jokerCount - 1), value, newColors, nextColor));
|
||||||
searchState.jokerCount - 1), value,
|
|
||||||
newColors, nextColor));
|
|
||||||
}
|
}
|
||||||
searchHelper.checkResult(findGroupsWithTotalPoints(searchState,
|
searchHelper.checkResult(findGroupsWithTotalPoints(searchState, value,
|
||||||
value, chosenColors, nextColor));
|
chosenColors, nextColor));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (chosenColors.size() >= 3) {
|
if (chosenColors.size() >= 3) {
|
||||||
|
@ -198,14 +189,13 @@ public class AIUtil {
|
||||||
SearchState searchState, int value, List<StoneColor> chosenColors) {
|
SearchState searchState, int value, List<StoneColor> chosenColors) {
|
||||||
int groupPoints = chosenColors.size() * value;
|
int groupPoints = chosenColors.size() * value;
|
||||||
Pair<List<StoneSet>, Integer> result = findSetsWithTotalPoints(
|
Pair<List<StoneSet>, Integer> result = findSetsWithTotalPoints(
|
||||||
searchState.pointsMissing - groupPoints,
|
searchState.pointsMissing - groupPoints, searchState.stoneCounts,
|
||||||
searchState.stoneCounts, searchState.jokerCount);
|
searchState.jokerCount);
|
||||||
List<Stone> newStones = new ArrayList<Stone>();
|
List<Stone> newStones = new ArrayList<Stone>();
|
||||||
for (StoneColor color : chosenColors) {
|
for (StoneColor color : chosenColors) {
|
||||||
newStones.add(new Stone(value, color));
|
newStones.add(new Stone(value, color));
|
||||||
}
|
}
|
||||||
List<StoneSet> newResultList = new ArrayList<StoneSet>(
|
List<StoneSet> newResultList = new ArrayList<StoneSet>(result.getFirst());
|
||||||
result.getFirst());
|
|
||||||
newResultList.add(new StoneSet(newStones));
|
newResultList.add(new StoneSet(newStones));
|
||||||
result = new Pair<List<StoneSet>, Integer>(newResultList,
|
result = new Pair<List<StoneSet>, Integer>(newResultList,
|
||||||
result.getSecond() + groupPoints);
|
result.getSecond() + groupPoints);
|
||||||
|
@ -216,9 +206,9 @@ public class AIUtil {
|
||||||
SearchState searchState, Pair<Integer, StoneColor> testedStone,
|
SearchState searchState, Pair<Integer, StoneColor> testedStone,
|
||||||
int runLength) {
|
int runLength) {
|
||||||
|
|
||||||
SearchHelper searchHelper = new SearchHelper(searchState.pointsMissing,
|
SearchHelper searchHelper = new SearchHelper(
|
||||||
new Pair<List<StoneSet>, Integer>(
|
searchState.pointsMissing,
|
||||||
Collections.<StoneSet> emptyList(), 0));
|
new Pair<List<StoneSet>, Integer>(Collections.<StoneSet> emptyList(), 0));
|
||||||
try {
|
try {
|
||||||
Pair<Integer, StoneColor> nextStone = null;
|
Pair<Integer, StoneColor> nextStone = null;
|
||||||
if (testedStone.getFirst() > 1) {
|
if (testedStone.getFirst() > 1) {
|
||||||
|
@ -228,24 +218,22 @@ public class AIUtil {
|
||||||
|
|
||||||
if (searchState.stoneCounts.containsKey(nextStone)) {
|
if (searchState.stoneCounts.containsKey(nextStone)) {
|
||||||
decrementStoneCount(searchState.stoneCounts, nextStone);
|
decrementStoneCount(searchState.stoneCounts, nextStone);
|
||||||
searchHelper.checkResult(findRunsWithTotalPoints(
|
searchHelper.checkResult(findRunsWithTotalPoints(searchState,
|
||||||
searchState, nextStone, runLength + 1));
|
nextStone, runLength + 1));
|
||||||
incrementStoneCount(searchState.stoneCounts, nextStone);
|
incrementStoneCount(searchState.stoneCounts, nextStone);
|
||||||
|
|
||||||
}
|
}
|
||||||
if (searchState.jokerCount > 0) {
|
if (searchState.jokerCount > 0) {
|
||||||
searchHelper.checkResult(findRunsWithTotalPoints(
|
searchHelper.checkResult(findRunsWithTotalPoints(new SearchState(
|
||||||
new SearchState(searchState.pointsMissing,
|
searchState.pointsMissing, searchState.stoneCounts,
|
||||||
searchState.stoneCounts,
|
searchState.jokerCount - 1), nextStone, runLength + 1));
|
||||||
searchState.jokerCount - 1), nextStone,
|
|
||||||
runLength + 1));
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (runLength >= 3) {
|
if (runLength >= 3) {
|
||||||
searchHelper.checkResult(handleFoundRun(searchState,
|
searchHelper.checkResult(handleFoundRun(searchState, testedStone,
|
||||||
testedStone, runLength));
|
runLength));
|
||||||
}
|
}
|
||||||
} catch (EnoughPoints enoughPoints) {
|
} catch (EnoughPoints enoughPoints) {
|
||||||
return enoughPoints.getResult();
|
return enoughPoints.getResult();
|
||||||
|
@ -253,9 +241,8 @@ public class AIUtil {
|
||||||
return searchHelper.getBestResult();
|
return searchHelper.getBestResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Pair<List<StoneSet>, Integer> handleFoundRun(
|
private Pair<List<StoneSet>, Integer> handleFoundRun(SearchState searchState,
|
||||||
SearchState searchState, Pair<Integer, StoneColor> testedStone,
|
Pair<Integer, StoneColor> testedStone, int runLength) {
|
||||||
int runLength) {
|
|
||||||
int below = testedStone.getFirst() - 1;
|
int below = testedStone.getFirst() - 1;
|
||||||
int high = below + runLength;
|
int high = below + runLength;
|
||||||
int runPoints = (high * (high + 1)) / 2 - (below * (below + 1)) / 2;
|
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
|
newStones.add(new Stone(i + testedStone.getFirst(), testedStone
|
||||||
.getSecond()));
|
.getSecond()));
|
||||||
}
|
}
|
||||||
List<StoneSet> newResultList = new ArrayList<StoneSet>(
|
List<StoneSet> newResultList = new ArrayList<StoneSet>(result.getFirst());
|
||||||
result.getFirst());
|
|
||||||
newResultList.add(new StoneSet(newStones));
|
newResultList.add(new StoneSet(newStones));
|
||||||
result = new Pair<List<StoneSet>, Integer>(newResultList,
|
result = new Pair<List<StoneSet>, Integer>(newResultList,
|
||||||
result.getSecond() + runPoints);
|
result.getSecond() + runPoints);
|
||||||
|
@ -326,7 +312,7 @@ public class AIUtil {
|
||||||
* Counts the numbers of stones
|
* Counts the numbers of stones
|
||||||
*
|
*
|
||||||
* @param stones
|
* @param stones
|
||||||
* the stones to count
|
* the stones to count
|
||||||
* @return the numbers for all stones
|
* @return the numbers for all stones
|
||||||
*/
|
*/
|
||||||
public static Pair<TreeMap<Pair<Integer, StoneColor>, Integer>, Integer> countStones(
|
public static Pair<TreeMap<Pair<Integer, StoneColor>, Integer>, Integer> countStones(
|
||||||
|
|
Reference in a new issue