diff options
Diffstat (limited to 'src/jrummikub/control')
-rw-r--r-- | src/jrummikub/control/TurnControl.java | 146 |
1 files changed, 123 insertions, 23 deletions
diff --git a/src/jrummikub/control/TurnControl.java b/src/jrummikub/control/TurnControl.java index 0f8f776..686d923 100644 --- a/src/jrummikub/control/TurnControl.java +++ b/src/jrummikub/control/TurnControl.java @@ -10,6 +10,7 @@ import jrummikub.model.IHand; import jrummikub.model.ITable; import jrummikub.model.Position; import jrummikub.model.Stone; +import jrummikub.model.StoneColor; import jrummikub.model.StoneSet; import jrummikub.util.Connection; import jrummikub.util.Event; @@ -21,27 +22,6 @@ import jrummikub.util.Pair; import jrummikub.view.IView; public class TurnControl { - static class HandStonePositionComparator implements - Comparator<Pair<Stone, Position>> { - @Override - public int compare(Pair<Stone, Position> pair1, Pair<Stone, Position> pair2) { - Position pos1 = pair1.getSecond(), pos2 = pair2.getSecond(); - if (pos1.getY() < pos2.getY()) { - return -1; - } else if (pos1.getY() > pos2.getY()) { - return 1; - } else { - if (pos1.getX() < pos2.getX()) { - return -1; - } else if (pos1.getX() > pos2.getX()) { - return 1; - } else { - return 0; - } - } - } - } - private IHand hand; private ITable table; private ITurnTimer timer; @@ -181,12 +161,41 @@ public class TurnControl { timer.startTimer(); } - private void sortByRuns() { + private void sortStones(Comparator<Stone> comparator) { + List<Stone> stones = new ArrayList<Stone>(); + for (Pair<Stone, Position> entry : hand) { + stones.add(entry.getFirst()); + } + for (Stone stone : stones) { + hand.pickUp(stone); + } + + Collections.sort(stones, comparator); + + int x = 0, y = 0; + for (Stone stone : stones) { + hand.drop(stone, new Position(x, y)); + x++; + if (x >= RoundControl.HAND_WIDTH) { + x = 0; + y++; + + if (y >= RoundControl.HAND_HEIGHT) { + throw new ArrayIndexOutOfBoundsException(); // TODO We can't handle + // this yet... + } + } + } + view.getPlayerPanel().getHandPanel().setStones(hand); } - private void sortByGroups() { + private void sortByRuns() { + sortStones(new RunComparator()); + } + private void sortByGroups() { + sortStones(new GroupComparator()); } private void stoneClick(Stone stone, boolean collect) { @@ -377,4 +386,95 @@ public class TurnControl { public IEvent getEndOfTurnEvent() { return endOfTurnEvent; } + + static private int compareJokers(Stone s1, Stone s2) { + if (!s1.isJoker() && s2.isJoker()) { + return -1; + } else if (s1.isJoker() && !s2.isJoker()) { + return 1; + } else { + return 0; + } + } + + static private int compareColors(Stone s1, Stone s2) { + if (s1.getColor() == s2.getColor()) + return 0; + + for (StoneColor color : StoneColor.values()) { + if (s1.getColor() == color) { + return -1; + } else if (s2.getColor() == color) { + return 1; + } + } + + return 0; + } + + static private int compareValues(Stone s1, Stone s2) { + if (s1.getValue() < s2.getValue()) { + return -1; + } else if (s1.getValue() > s2.getValue()) { + return 1; + } else { + return 0; + } + } + + private static class RunComparator implements Comparator<Stone> { + @Override + public int compare(Stone s1, Stone s2) { + int jokerCompare = compareJokers(s1, s2); + if (jokerCompare != 0) { + return jokerCompare; + } + + int colorCompare = compareColors(s1, s2); + if (colorCompare != 0) { + return colorCompare; + } + + return compareValues(s1, s2); + } + } + + private static class GroupComparator implements Comparator<Stone> { + @Override + public int compare(Stone s1, Stone s2) { + int jokerCompare = compareJokers(s1, s2); + if (jokerCompare != 0) { + return jokerCompare; + } + + int valueCompare = compareValues(s1, s2); + if (valueCompare != 0) { + return valueCompare; + } + + return compareColors(s1, s2); + } + } + + static class HandStonePositionComparator implements + Comparator<Pair<Stone, Position>> { + @Override + public int compare(Pair<Stone, Position> pair1, Pair<Stone, Position> pair2) { + Position pos1 = pair1.getSecond(), pos2 = pair2.getSecond(); + if (pos1.getY() < pos2.getY()) { + return -1; + } else if (pos1.getY() > pos2.getY()) { + return 1; + } else { + if (pos1.getX() < pos2.getX()) { + return -1; + } else if (pos1.getX() > pos2.getX()) { + return 1; + } else { + return 0; + } + } + } + } + } |