summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--mock/jrummikub/model/MockHand.java11
-rw-r--r--mock/jrummikub/model/MockPlayer.java4
-rw-r--r--src/jrummikub/control/RoundControl.java25
-rw-r--r--src/jrummikub/control/TurnControl.java5
-rw-r--r--src/jrummikub/model/Hand.java27
-rw-r--r--src/jrummikub/model/IHand.java15
-rw-r--r--test/jrummikub/control/RoundControlTest.java50
7 files changed, 89 insertions, 48 deletions
diff --git a/mock/jrummikub/model/MockHand.java b/mock/jrummikub/model/MockHand.java
index d262c87..30bda51 100644
--- a/mock/jrummikub/model/MockHand.java
+++ b/mock/jrummikub/model/MockHand.java
@@ -65,4 +65,15 @@ public class MockHand implements IHand {
return null;
}
}
+
+ @Override
+ public int getRowCount() {
+ return 0;
+ }
+
+ @Override
+ public int getFreeRowSpace(int row) {
+ // TODO Auto-generated method stub
+ return 0;
+ }
}
diff --git a/mock/jrummikub/model/MockPlayer.java b/mock/jrummikub/model/MockPlayer.java
index d9c4a43..214a4e0 100644
--- a/mock/jrummikub/model/MockPlayer.java
+++ b/mock/jrummikub/model/MockPlayer.java
@@ -7,7 +7,7 @@ import java.awt.Color;
*/
public class MockPlayer implements IPlayer {
/** */
- public MockHand hand;
+ public Hand hand;
/** */
public String name;
/** */
@@ -20,7 +20,7 @@ public class MockPlayer implements IPlayer {
* the player color
*/
public MockPlayer(String name, Color color) {
- hand = new MockHand();
+ hand = new Hand();
this.name = name;
this.color = color;
}
diff --git a/src/jrummikub/control/RoundControl.java b/src/jrummikub/control/RoundControl.java
index 7cea255..dad2e30 100644
--- a/src/jrummikub/control/RoundControl.java
+++ b/src/jrummikub/control/RoundControl.java
@@ -149,17 +149,26 @@ public class RoundControl {
return ret;
}
- void dealStone() {
- gameState
- .getActivePlayer()
- .getHand()
- .drop(gameState.getGameHeap().drawStone(),
- new Position(Hand.WIDTH - 1, Hand.HEIGHT - 1));
+ void dealStones(int count) {
+ IHand hand = gameState.getActivePlayer().getHand();
+ int rowCount = hand.getRowCount();
+
+ for (int i = 0; i < count; ++i) {
+ if (hand.getFreeRowSpace(rowCount - 1) == 0) {
+ rowCount++;
+ }
+
+ hand.drop(gameState.getGameHeap().drawStone(), new Position(
+ Hand.WIDTH - 1, rowCount - 1));
+ }
+ }
+
+ private void dealStone() {
+ dealStones(1);
}
private void dealPenalty(int count) {
- for (int i = 0; i < count + 3; ++i)
- dealStone();
+ dealStones(count + 3);
}
private void win() {
diff --git a/src/jrummikub/control/TurnControl.java b/src/jrummikub/control/TurnControl.java
index 06e1d76..4652ea7 100644
--- a/src/jrummikub/control/TurnControl.java
+++ b/src/jrummikub/control/TurnControl.java
@@ -247,11 +247,6 @@ public class TurnControl {
if (x >= Hand.WIDTH) {
x = 0;
y++;
-
- if (y >= Hand.HEIGHT) {
- // TODO We can't handle this yet...
- throw new ArrayIndexOutOfBoundsException();
- }
}
}
diff --git a/src/jrummikub/model/Hand.java b/src/jrummikub/model/Hand.java
index 1337829..ab5eb19 100644
--- a/src/jrummikub/model/Hand.java
+++ b/src/jrummikub/model/Hand.java
@@ -10,22 +10,31 @@ public class Hand extends StoneTray<Stone> implements IHand {
* The width of the hand
*/
public final static int WIDTH = 14;
- /**
- * The height of the hand
- */
- @Deprecated
- public final static int HEIGHT = 2;
- private boolean rowIsFull(float row) {
+ @Override
+ public int getFreeRowSpace(int row) {
int count = 0;
for (Pair<Stone, Position> entry : this) {
if (entry.getSecond().getY() == row) {
count++;
}
}
- return count == WIDTH;
+ return WIDTH - count;
+ }
+
+ @Override
+ public int getRowCount() {
+ int rows = 0;
+
+ for (Pair<Stone, Position> entry : this) {
+ if (entry.getSecond().getY() > rows) {
+ rows = (int) entry.getSecond().getY();
+ }
+ }
+
+ return rows + 1;
}
-
+
@Override
protected Pair<Position, Direction> fixInvalidDrop(Stone stone, Position pos,
Direction dir) {
@@ -38,7 +47,7 @@ public class Hand extends StoneTray<Stone> implements IHand {
if (x < 0) {
return new Pair<Position, Direction>(new Position(0, y), RIGHT);
} else {
- if (rowIsFull(y)) {
+ if (getFreeRowSpace((int) y) == 0) {
return new Pair<Position, Direction>(new Position(0, y + 1), RIGHT);
} else {
return new Pair<Position, Direction>(new Position(WIDTH - 1, y), LEFT);
diff --git a/src/jrummikub/model/IHand.java b/src/jrummikub/model/IHand.java
index f5234d1..dafa9c7 100644
--- a/src/jrummikub/model/IHand.java
+++ b/src/jrummikub/model/IHand.java
@@ -5,4 +5,19 @@ package jrummikub.model;
*/
public interface IHand extends IStoneTray<Stone> {
+ /**
+ * The number of used rows
+ *
+ * @return the number of rows
+ */
+ int getRowCount();
+
+ /**
+ * Gets the amount of free space in a hand row
+ *
+ * @param row
+ * the row number
+ * @return the number of stones that can fit into the row
+ */
+ int getFreeRowSpace(int row);
}
diff --git a/test/jrummikub/control/RoundControlTest.java b/test/jrummikub/control/RoundControlTest.java
index 3e610e3..dba3ae9 100644
--- a/test/jrummikub/control/RoundControlTest.java
+++ b/test/jrummikub/control/RoundControlTest.java
@@ -15,6 +15,8 @@ import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
+import jrummikub.model.Hand;
+import jrummikub.model.IHand;
import jrummikub.model.MockGameState;
import jrummikub.model.MockTable;
import jrummikub.model.Position;
@@ -52,11 +54,9 @@ public class RoundControlTest {
private void checkCorrectlyDealed() {
assertEquals(106 - testGameState.getPlayerCount() * 14
- - testGameState.table.getSize(), testGameState.getGameHeap()
- .getSize());
+ - testGameState.table.getSize(), testGameState.getGameHeap().getSize());
for (int i = 0; i < testGameState.getPlayerCount(); i++) {
- assertEquals(14, testGameState.getNthNextPlayer(i).getHand()
- .getSize());
+ assertEquals(14, testGameState.getNthNextPlayer(i).getHand().getSize());
}
}
@@ -91,27 +91,28 @@ public class RoundControlTest {
view.getTablePanel().rightPlayerName = null;
view.displayStartTurnPanel = false;
}
-
+
/** */
- @Test
- public void playerCameOutCorrectly(){
+ @Test
+ public void playerCameOutCorrectly() {
MockTable oldTable = new MockTable();
MockTable newTable = new MockTable();
Stone blueTen = new Stone(10, BLUE);
Stone redTen = new Stone(10, RED);
Stone blackTen = new Stone(10, BLACK);
-
+
}
/** */
@Test
- public void testDealStone() {
+ public void testDealStones() {
testRound.deal();
checkCorrectlyDealed();
- for (int i = 0; i < 28 - 14; i++) {
- testRound.dealStone();
+ for (int i = 0; i < 14; i++) {
+ testRound.dealStones(2);
}
- assertEquals(28, testGameState.getActivePlayer().getHand().getSize());
+ assertEquals(2 * 14 + 14, testGameState.getActivePlayer().getHand()
+ .getSize());
}
/** */
@@ -150,7 +151,9 @@ public class RoundControlTest {
view.startTurnEvent.emit();
assertFalse(view.displayStartTurnPanel);
- testGameState.players.get(0).hand.stones.remove(0);
+
+ IHand hand = testGameState.players.get(0).hand;
+ hand.pickUp(hand.iterator().next().getFirst());
resetTurnStart();
view.getPlayerPanel().endTurnEvent.emit();
@@ -170,8 +173,9 @@ public class RoundControlTest {
view.startTurnEvent.emit();
assertFalse(view.displayStartTurnPanel);
- Stone stone = testGameState.players.get(0).hand.stones.remove(0)
- .getFirst();
+ IHand hand = testGameState.players.get(0).hand;
+ Stone stone = hand.iterator().next().getFirst();
+ hand.pickUp(stone);
newTable.drop(new StoneSet(stone), new Position(0, 0));
resetTurnStart();
view.getPlayerPanel().endTurnEvent.emit();
@@ -232,10 +236,11 @@ public class RoundControlTest {
view.startTurnEvent.emit();
assertFalse(view.displayStartTurnPanel);
- Stone stone = testGameState.players.get(0).hand.stones.remove(0)
- .getFirst();
+ IHand hand = testGameState.players.get(0).hand;
+ Stone stone = hand.iterator().next().getFirst();
+ hand.pickUp(stone);
newTable.drop(new StoneSet(stone), new Position(0, 0));
- testGameState.players.get(0).hand.stones.clear();
+ testGameState.players.get(0).hand = new Hand();
resetTurnStart();
view.getPlayerPanel().endTurnEvent.emit();
@@ -253,15 +258,12 @@ public class RoundControlTest {
Stone blueTwo = new Stone(2, BLUE);
Stone blueThree = new Stone(3, BLUE);
Stone blueFour = new Stone(4, BLUE);
- StoneSet oldSet1 = new StoneSet(
- Arrays.asList(blueOne, redOne, blackOne));
+ StoneSet oldSet1 = new StoneSet(Arrays.asList(blueOne, redOne, blackOne));
StoneSet oldSet2 = new StoneSet(blueTwo);
oldTable.drop(oldSet1, new Position(0, 0));
oldTable.drop(oldSet2, new Position(0, 0));
- StoneSet newSet1 = new StoneSet(Arrays.asList(blueOne, blueTwo,
- blueFour));
- StoneSet newSet2 = new StoneSet(Arrays.asList(redOne, blackOne,
- blueThree));
+ StoneSet newSet1 = new StoneSet(Arrays.asList(blueOne, blueTwo, blueFour));
+ StoneSet newSet2 = new StoneSet(Arrays.asList(redOne, blackOne, blueThree));
newTable.drop(newSet1, new Position(0, 0));
newTable.drop(newSet2, new Position(0, 0));