diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2011-05-10 01:22:56 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2011-05-10 01:22:56 +0200 |
commit | 56b75e037ab2061d1f7462d2f71a83ca5a9353b6 (patch) | |
tree | fb20a21e290c4c42d6c283ef9c19521d97d303cc | |
parent | 8a3439f7369e4059cb49f65413028b57f8fc7daa (diff) | |
download | JRummikub-56b75e037ab2061d1f7462d2f71a83ca5a9353b6.tar JRummikub-56b75e037ab2061d1f7462d2f71a83ca5a9353b6.zip |
Implement hand auto-sort
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@205 72836036-5685-4462-b002-a69064685172
-rw-r--r-- | src/jrummikub/control/TurnControl.java | 146 | ||||
-rw-r--r-- | src/jrummikub/view/impl/PlayerPanel.java | 4 | ||||
-rw-r--r-- | test/jrummikub/control/TurnControlTest.java | 106 |
3 files changed, 183 insertions, 73 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; + } + } + } + } + } diff --git a/src/jrummikub/view/impl/PlayerPanel.java b/src/jrummikub/view/impl/PlayerPanel.java index 9c12a80..ce8b8ec 100644 --- a/src/jrummikub/view/impl/PlayerPanel.java +++ b/src/jrummikub/view/impl/PlayerPanel.java @@ -101,7 +101,7 @@ class PlayerPanel extends JPanel implements IPlayerPanel { currentPlayerNameLabel.setVerticalTextPosition(JLabel.CENTER); leftPanel.add(currentPlayerNameLabel); - sortByGroupsButton = new JButton("<html><center>Nach Gruppen sortieren"); + sortByGroupsButton = new JButton("<html><center>Nach Sammlungen sortieren"); sortByGroupsButton.setFont(sortByGroupsButton.getFont().deriveFont(0)); sortByGroupsButton.setMargin(new Insets(0, 0, 0, 0)); sortByGroupsButton.addActionListener(new ActionListener() { @@ -208,7 +208,7 @@ class PlayerPanel extends JPanel implements IPlayerPanel { int firstLineHeight = (int) ((height - SIDE_PANEL_SEPARATOR) * SIDE_PANEL_FIRST_LINE_HEIGHT); int buttonWidth = (width - SIDE_PANEL_SEPARATOR) / 2; int buttonHeight = height - SIDE_PANEL_SEPARATOR - firstLineHeight; - float fontSize = (float) Math.sqrt(buttonWidth * buttonHeight) / 5; + float fontSize = (float) Math.sqrt(buttonWidth * buttonHeight) / 6; if (fontSize > MAX_BUTTON_FONT_SIZE) fontSize = MAX_BUTTON_FONT_SIZE; diff --git a/test/jrummikub/control/TurnControlTest.java b/test/jrummikub/control/TurnControlTest.java index b4766b6..44b7729 100644 --- a/test/jrummikub/control/TurnControlTest.java +++ b/test/jrummikub/control/TurnControlTest.java @@ -112,10 +112,9 @@ public class TurnControlTest { public void showInitialHand() { mockView.displayStartTurnPanel = true; - List<Pair<Stone, Position>> stones = Arrays - .asList(new Pair<Stone, Position>(new Stone(RED), new Position( - 0, 0)), new Pair<Stone, Position>(new Stone(BLACK), - new Position(1, 0))); + List<Pair<Stone, Position>> stones = Arrays.asList( + new Pair<Stone, Position>(new Stone(RED), new Position(0, 0)), + new Pair<Stone, Position>(new Stone(BLACK), new Position(1, 0))); mockHand.iterable = stones; @@ -241,8 +240,8 @@ public class TurnControlTest { mockView.playerPanel.handPanel.stoneClickEvent.emit(firstStone, true); mockView.playerPanel.handPanel.stoneClickEvent.emit(secondStone, true); - mockView.tablePanel.stoneCollectionPanel.stoneClickEvent.emit( - firstStone, false); + mockView.tablePanel.stoneCollectionPanel.stoneClickEvent.emit(firstStone, + false); assertCollection(Arrays.asList(secondStone)); } @@ -257,8 +256,8 @@ public class TurnControlTest { mockView.playerPanel.handPanel.stoneClickEvent.emit(firstStone, true); mockView.playerPanel.handPanel.stoneClickEvent.emit(secondStone, true); - mockView.tablePanel.stoneCollectionPanel.stoneClickEvent.emit( - firstStone, true); + mockView.tablePanel.stoneCollectionPanel.stoneClickEvent.emit(firstStone, + true); assertCollection(Arrays.asList(secondStone, firstStone)); } @@ -273,8 +272,8 @@ public class TurnControlTest { mockView.playerPanel.handPanel.stoneClickEvent.emit(firstStone, true); mockView.playerPanel.handPanel.stoneClickEvent.emit(secondStone, true); - mockView.tablePanel.stoneCollectionPanel.stoneClickEvent.emit( - firstStone, true); + mockView.tablePanel.stoneCollectionPanel.stoneClickEvent.emit(firstStone, + true); mockView.tablePanel.stoneCollectionPanel.setClickEvent.emit(firstStone, true); @@ -376,8 +375,7 @@ public class TurnControlTest { Stone stone2 = new Stone(2, StoneColor.RED); Stone stone3 = new Stone(3, StoneColor.RED); Stone stone4 = new Stone(4, StoneColor.RED); - StoneSet set1 = new StoneSet(Arrays.asList(stone1, stone2, stone3, - stone4)); + StoneSet set1 = new StoneSet(Arrays.asList(stone1, stone2, stone3, stone4)); mockTable.findStoneSet.put(stone1, set1); mockTable.findStoneSet.put(stone3, set1); @@ -397,8 +395,7 @@ public class TurnControlTest { Stone stone2 = new Stone(2, StoneColor.RED); Stone stone3 = new Stone(3, StoneColor.RED); Stone stone4 = new Stone(4, StoneColor.RED); - StoneSet set1 = new StoneSet(Arrays.asList(stone1, stone2, stone3, - stone4)); + StoneSet set1 = new StoneSet(Arrays.asList(stone1, stone2, stone3, stone4)); mockTable.findStoneSet.put(stone1, set1); mockTable.findStoneSet.put(stone3, set1); @@ -420,8 +417,7 @@ public class TurnControlTest { Stone stone2 = new Stone(2, StoneColor.RED); Stone stone3 = new Stone(3, StoneColor.RED); Stone stone4 = new Stone(4, StoneColor.RED); - StoneSet set1 = new StoneSet(Arrays.asList(stone1, stone2, stone3, - stone4)); + StoneSet set1 = new StoneSet(Arrays.asList(stone1, stone2, stone3, stone4)); mockTable.findStoneSet.put(stone1, set1); mockTable.findStoneSet.put(stone3, set1); @@ -608,10 +604,10 @@ public class TurnControlTest { Stone blackThree = new Stone(3, BLACK); Stone blackFour = new Stone(4, BLACK); Stone blackFive = new Stone(5, BLACK); - StoneSet oldSet1 = new StoneSet(Arrays.asList(blueOne, redOne, - blackOne, redTwo, redThree, redFour, blackTwo, blackThree)); - StoneSet oldSet2 = new StoneSet(Arrays.asList(blueTwo, blackFour, - blackFive)); + StoneSet oldSet1 = new StoneSet(Arrays.asList(blueOne, redOne, blackOne, + redTwo, redThree, redFour, blackTwo, blackThree)); + StoneSet oldSet2 = new StoneSet( + Arrays.asList(blueTwo, blackFour, blackFive)); table.drop(oldSet1, new Position(0, 0)); table.drop(oldSet2, new Position(0, 0)); mockHand.drop(blueThree, new Position(0, 0)); @@ -724,10 +720,10 @@ public class TurnControlTest { Stone blackThree = new Stone(3, BLACK); Stone blackFour = new Stone(4, BLACK); Stone blackFive = new Stone(5, BLACK); - StoneSet oldSet1 = new StoneSet(Arrays.asList(blueOne, redOne, - blackOne, redTwo, redThree, redFour, blackTwo, blackThree)); - StoneSet oldSet2 = new StoneSet(Arrays.asList(blueTwo, blackFour, - blackFive)); + StoneSet oldSet1 = new StoneSet(Arrays.asList(blueOne, redOne, blackOne, + redTwo, redThree, redFour, blackTwo, blackThree)); + StoneSet oldSet2 = new StoneSet( + Arrays.asList(blueTwo, blackFour, blackFive)); table.drop(oldSet1, new Position(0, 0)); table.drop(oldSet2, new Position(0, 0)); mockHand.drop(blueThree, new Position(0, 0)); @@ -840,10 +836,10 @@ public class TurnControlTest { Stone blackThree = new Stone(3, BLACK); Stone blackFour = new Stone(4, BLACK); Stone blackFive = new Stone(5, BLACK); - StoneSet oldSet1 = new StoneSet(Arrays.asList(blueOne, redOne, - blackOne, redTwo, redThree, redFour, blackTwo, blackThree)); - StoneSet oldSet2 = new StoneSet(Arrays.asList(blueTwo, blackFour, - blackFive)); + StoneSet oldSet1 = new StoneSet(Arrays.asList(blueOne, redOne, blackOne, + redTwo, redThree, redFour, blackTwo, blackThree)); + StoneSet oldSet2 = new StoneSet( + Arrays.asList(blueTwo, blackFour, blackFive)); table.drop(oldSet1, new Position(0, 0)); table.drop(oldSet2, new Position(0, 0)); mockHand.drop(blueThree, new Position(0, 0)); @@ -910,9 +906,11 @@ public class TurnControlTest { checkTableDisplay(table); checkHandDisplay(mockHand); } - + @Test public void testSortByGroups() { + testControl.startTurn(); + Stone red1 = new Stone(1, StoneColor.RED); Stone blue2 = new Stone(2, StoneColor.BLUE); Stone red4 = new Stone(4, StoneColor.RED); @@ -939,22 +937,25 @@ public class TurnControlTest { mockHand.drop(orange13, new Position(0, 0)); mockHand.drop(red11, new Position(0, 0)); mockHand.drop(black10, new Position(0, 0)); - + mockView.playerPanel.sortByGroupsEvent.emit(); - - List<Pair<Stone, Position>> stones = new ArrayList<Pair<Stone, Position>>(mockHand.stones); + + List<Pair<Stone, Position>> stones = new ArrayList<Pair<Stone, Position>>( + mockHand.stones); Collections.sort(stones, new TurnControl.HandStonePositionComparator()); - + assertEquals(stones.size(), 13); - + assertSame(stones.get(0).getFirst(), blue1); assertSame(stones.get(1).getFirst(), red1); assertSame(stones.get(2).getFirst(), blue2); assertSame(stones.get(3).getFirst(), red3); - - assertTrue(stones.get(4).getFirst() == blue4 || stones.get(4).getFirst() == blue4a); - assertTrue(stones.get(5).getFirst() == blue4 || stones.get(5).getFirst() == blue4a); - + + assertTrue(stones.get(4).getFirst() == blue4 + || stones.get(4).getFirst() == blue4a); + assertTrue(stones.get(5).getFirst() == blue4 + || stones.get(5).getFirst() == blue4a); + assertSame(stones.get(6).getFirst(), red4); assertSame(stones.get(7).getFirst(), black5); assertSame(stones.get(8).getFirst(), black10); @@ -962,10 +963,14 @@ public class TurnControlTest { assertSame(stones.get(10).getFirst(), red11); assertSame(stones.get(11).getFirst(), orange13); assertSame(stones.get(12).getFirst(), joker); + + checkHandDisplay(mockHand); } - + @Test public void testSortByRuns() { + testControl.startTurn(); + Stone red1 = new Stone(1, StoneColor.RED); Stone blue2 = new Stone(2, StoneColor.BLUE); Stone red4 = new Stone(4, StoneColor.RED); @@ -992,28 +997,33 @@ public class TurnControlTest { mockHand.drop(orange13, new Position(0, 0)); mockHand.drop(red11, new Position(0, 0)); mockHand.drop(black10, new Position(0, 0)); - + mockView.playerPanel.sortByRunsEvent.emit(); - - List<Pair<Stone, Position>> stones = new ArrayList<Pair<Stone, Position>>(mockHand.stones); + + List<Pair<Stone, Position>> stones = new ArrayList<Pair<Stone, Position>>( + mockHand.stones); Collections.sort(stones, new TurnControl.HandStonePositionComparator()); - + assertEquals(stones.size(), 13); - + assertSame(stones.get(0).getFirst(), black5); assertSame(stones.get(1).getFirst(), black10); assertSame(stones.get(2).getFirst(), orange10); assertSame(stones.get(3).getFirst(), orange13); assertSame(stones.get(4).getFirst(), blue1); assertSame(stones.get(5).getFirst(), blue2); - - assertTrue(stones.get(6).getFirst() == blue4 || stones.get(6).getFirst() == blue4a); - assertTrue(stones.get(7).getFirst() == blue4 || stones.get(7).getFirst() == blue4a); - + + assertTrue(stones.get(6).getFirst() == blue4 + || stones.get(6).getFirst() == blue4a); + assertTrue(stones.get(7).getFirst() == blue4 + || stones.get(7).getFirst() == blue4a); + assertSame(stones.get(8).getFirst(), red1); assertSame(stones.get(9).getFirst(), red3); assertSame(stones.get(10).getFirst(), red4); assertSame(stones.get(11).getFirst(), red11); assertSame(stones.get(12).getFirst(), joker); + + checkHandDisplay(mockHand); } } |