From 996892670dc15cf61123d47f4a6b78e945f636d4 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 31 May 2011 23:57:17 +0200 Subject: AIUtil: Fix attribute visibility git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@369 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/control/AIUtil.java | 128 +++++++++++++++++--------------------- 1 file changed, 57 insertions(+), 71 deletions(-) (limited to 'src/jrummikub') 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 stoneColors; + private GameSettings settings; + private List 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(Arrays.asList(StoneColor - .values())); + stoneColors = new ArrayList(Arrays.asList(StoneColor.values())); stoneColors.retainAll(settings.getStoneColors()); } @@ -43,8 +41,7 @@ public class AIUtil { int jokerCount; SearchState(int pointsMissing, - TreeMap, Integer> stoneCounts, - int jokerCount) { + TreeMap, 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, Integer> findSetsWithTotalPoints( int pointsMissing, - TreeMap, Integer> stoneCounts, - int jokerCount) { + TreeMap, Integer> stoneCounts, int jokerCount) { Pair, Integer> emptyResult = new Pair, Integer>( Collections. emptyList(), 0); if (pointsMissing <= 0) @@ -89,9 +85,9 @@ public class AIUtil { stoneCounts = (TreeMap, Integer>) stoneCounts .clone(); - SearchHelper searchHelper = new SearchHelper(pointsMissing, - new Pair, Integer>( - Collections. emptyList(), 0)); + SearchHelper searchHelper = new SearchHelper( + pointsMissing, + new Pair, Integer>(Collections. 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 nextStone = new Pair( - value, nextColor); + Pair nextStone = new Pair(value, + nextColor); - SearchHelper searchHelper = new SearchHelper(searchState.pointsMissing, - new Pair, Integer>( - Collections. emptyList(), 0)); + SearchHelper searchHelper = new SearchHelper( + searchState.pointsMissing, + new Pair, Integer>(Collections. emptyList(), 0)); try { if (nextColor != null) { - List newColors = new ArrayList( - chosenColors); + List newColors = new ArrayList(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 chosenColors) { int groupPoints = chosenColors.size() * value; Pair, Integer> result = findSetsWithTotalPoints( - searchState.pointsMissing - groupPoints, - searchState.stoneCounts, searchState.jokerCount); + searchState.pointsMissing - groupPoints, searchState.stoneCounts, + searchState.jokerCount); List newStones = new ArrayList(); for (StoneColor color : chosenColors) { newStones.add(new Stone(value, color)); } - List newResultList = new ArrayList( - result.getFirst()); + List newResultList = new ArrayList(result.getFirst()); newResultList.add(new StoneSet(newStones)); result = new Pair, Integer>(newResultList, result.getSecond() + groupPoints); @@ -216,9 +206,9 @@ public class AIUtil { SearchState searchState, Pair testedStone, int runLength) { - SearchHelper searchHelper = new SearchHelper(searchState.pointsMissing, - new Pair, Integer>( - Collections. emptyList(), 0)); + SearchHelper searchHelper = new SearchHelper( + searchState.pointsMissing, + new Pair, Integer>(Collections. emptyList(), 0)); try { Pair 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, Integer> handleFoundRun( - SearchState searchState, Pair testedStone, - int runLength) { + private Pair, Integer> handleFoundRun(SearchState searchState, + Pair 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 newResultList = new ArrayList( - result.getFirst()); + List newResultList = new ArrayList(result.getFirst()); newResultList.add(new StoneSet(newStones)); result = new Pair, 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, Integer>, Integer> countStones( -- cgit v1.2.3