diff options
author | Jannis Harder <harder@informatik.uni-luebeck.de> | 2011-06-17 22:12:43 +0200 |
---|---|---|
committer | Jannis Harder <harder@informatik.uni-luebeck.de> | 2011-06-17 22:12:43 +0200 |
commit | 5a4d1ccb12d697d6cd0a0ea187045c9f6370814c (patch) | |
tree | fbac044520bf9230bf58f006b9ed7742011e09fd /src/jrummikub/model | |
parent | 47bf19036e049787fa7742f5ff72a08a0c9e887c (diff) | |
download | JRummikub-5a4d1ccb12d697d6cd0a0ea187045c9f6370814c.tar JRummikub-5a4d1ccb12d697d6cd0a0ea187045c9f6370814c.zip |
Removed AIUtil
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@449 72836036-5685-4462-b002-a69064685172
Diffstat (limited to 'src/jrummikub/model')
-rw-r--r-- | src/jrummikub/model/Hand.java | 56 |
1 files changed, 53 insertions, 3 deletions
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<Stone> implements IHand { turnLogic.needIntialMeldThreshold(); return turnLogic.solve(); } + + + private static void incrementStoneCount( + TreeMap<Pair<Integer, StoneColor>, Integer> stones, + Pair<Integer, StoneColor> stone) { + if (stones.containsKey(stone)) { + stones.put(stone, stones.get(stone) + 1); + } else { + stones.put(stone, 1); + } + } + + private final static Comparator<Pair<Integer, StoneColor>> comparator = new Comparator<Pair<Integer, StoneColor>>() { + @Override + public int compare(Pair<Integer, StoneColor> o1, + Pair<Integer, StoneColor> 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<TreeMap<Pair<Integer, StoneColor>, Integer>, Integer> countStones( + List<Stone> stones) { + int jokerCount = 0; + TreeMap<Pair<Integer, StoneColor>, Integer> stoneCounts = new TreeMap<Pair<Integer, StoneColor>, Integer>( + comparator); + + for (Stone stone : stones) { + if (stone.isJoker()) { + jokerCount++; + } else { + Pair<Integer, StoneColor> key = new Pair<Integer, StoneColor>( + stone.getValue(), stone.getColor()); + + incrementStoneCount(stoneCounts, key); + } + } + return new Pair<TreeMap<Pair<Integer, StoneColor>, Integer>, Integer>( + stoneCounts, jokerCount); + } @Override public int getIdenticalStoneCount() { @@ -106,8 +157,7 @@ public class Hand extends StoneTray<Stone> implements IHand { stones.add(iter.next().getFirst()); } - Pair<TreeMap<Pair<Integer, StoneColor>, Integer>, Integer> stoneCounts = AIUtil - .countStones(stones); + Pair<TreeMap<Pair<Integer, StoneColor>, Integer>, Integer> stoneCounts = countStones(stones); int pairCount = 0; |