From 604ef91282ad234130b1d46569efd486ab6c5024 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 16 May 2011 22:01:02 +0200 Subject: Fix dealing stones for more than 2 rows git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@244 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/control/RoundControl.java | 25 +++++++++++++++++-------- src/jrummikub/control/TurnControl.java | 5 ----- src/jrummikub/model/Hand.java | 27 ++++++++++++++++++--------- src/jrummikub/model/IHand.java | 15 +++++++++++++++ 4 files changed, 50 insertions(+), 22 deletions(-) (limited to 'src') 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 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 entry : this) { if (entry.getSecond().getY() == row) { count++; } } - return count == WIDTH; + return WIDTH - count; + } + + @Override + public int getRowCount() { + int rows = 0; + + for (Pair entry : this) { + if (entry.getSecond().getY() > rows) { + rows = (int) entry.getSecond().getY(); + } + } + + return rows + 1; } - + @Override protected Pair fixInvalidDrop(Stone stone, Position pos, Direction dir) { @@ -38,7 +47,7 @@ public class Hand extends StoneTray implements IHand { if (x < 0) { return new Pair(new Position(0, y), RIGHT); } else { - if (rowIsFull(y)) { + if (getFreeRowSpace((int) y) == 0) { return new Pair(new Position(0, y + 1), RIGHT); } else { return new Pair(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 { + /** + * 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); } -- cgit v1.2.3