From 5a4d1ccb12d697d6cd0a0ea187045c9f6370814c Mon Sep 17 00:00:00 2001 From: Jannis Harder Date: Fri, 17 Jun 2011 22:12:43 +0200 Subject: Removed AIUtil git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@449 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/model/Hand.java | 56 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) (limited to 'src/jrummikub/model') diff --git a/src/jrummikub/model/Hand.java b/src/jrummikub/model/Hand.java index 6c3beca..69d659b 100644 --- a/src/jrummikub/model/Hand.java +++ b/src/jrummikub/model/Hand.java @@ -5,12 +5,12 @@ import static jrummikub.model.StoneTray.Direction.RIGHT; import java.util.ArrayList; import java.util.Collections; +import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.TreeMap; import jrummikub.ai.TurnLogic; -import jrummikub.control.AIUtil; import jrummikub.util.Pair; /** Class managing a {@link Player}'s {@link Stone}s */ @@ -96,6 +96,57 @@ public class Hand extends StoneTray implements IHand { turnLogic.needIntialMeldThreshold(); return turnLogic.solve(); } + + + private static void incrementStoneCount( + TreeMap, Integer> stones, + Pair stone) { + if (stones.containsKey(stone)) { + stones.put(stone, stones.get(stone) + 1); + } else { + stones.put(stone, 1); + } + } + + private final static Comparator> comparator = new Comparator>() { + @Override + public int compare(Pair o1, + Pair o2) { + int firstComparison = o1.getFirst().compareTo(o2.getFirst()); + if (firstComparison != 0) { + return -firstComparison; + } else { + return o1.getSecond().compareTo(o2.getSecond()); + } + } + }; + + /** + * Counts the numbers of stones + * + * @param stones + * the stones to count + * @return the numbers for all stones + */ + private static Pair, Integer>, Integer> countStones( + List stones) { + int jokerCount = 0; + TreeMap, Integer> stoneCounts = new TreeMap, Integer>( + comparator); + + for (Stone stone : stones) { + if (stone.isJoker()) { + jokerCount++; + } else { + Pair key = new Pair( + stone.getValue(), stone.getColor()); + + incrementStoneCount(stoneCounts, key); + } + } + return new Pair, Integer>, Integer>( + stoneCounts, jokerCount); + } @Override public int getIdenticalStoneCount() { @@ -106,8 +157,7 @@ public class Hand extends StoneTray implements IHand { stones.add(iter.next().getFirst()); } - Pair, Integer>, Integer> stoneCounts = AIUtil - .countStones(stones); + Pair, Integer>, Integer> stoneCounts = countStones(stones); int pairCount = 0; -- cgit v1.2.3