From c9843770ddee02dca46c6ba5d81cc84da32d06d4 Mon Sep 17 00:00:00 2001 From: Bennet Gerlach Date: Wed, 4 May 2011 16:23:10 +0200 Subject: Added interfaces for model classes git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@104 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/model/GameState.java | 8 +++- src/jrummikub/model/Hand.java | 2 +- src/jrummikub/model/IHand.java | 5 +++ src/jrummikub/model/IStoneTray.java | 40 +++++++++++++++++ src/jrummikub/model/ITable.java | 16 +++++++ src/jrummikub/model/Player.java | 16 +++---- src/jrummikub/model/StoneTray.java | 90 +++++++++++++++++++------------------ src/jrummikub/model/Table.java | 4 +- 8 files changed, 126 insertions(+), 55 deletions(-) create mode 100644 src/jrummikub/model/IHand.java create mode 100644 src/jrummikub/model/IStoneTray.java create mode 100644 src/jrummikub/model/ITable.java (limited to 'src/jrummikub/model') diff --git a/src/jrummikub/model/GameState.java b/src/jrummikub/model/GameState.java index 35e9457..23e51b4 100644 --- a/src/jrummikub/model/GameState.java +++ b/src/jrummikub/model/GameState.java @@ -6,12 +6,13 @@ import java.util.List; /** Class managing the overall and momentary GameState */ public class GameState { - Table table; + ITable table; List players; int activePlayer; private StoneHeap gameHeap; public GameState() { + table = new Table(); players = new ArrayList(); players.add(new Player(Color.red)); players.add(new Player(Color.yellow)); @@ -20,6 +21,10 @@ public class GameState { activePlayer = 0; } + public ITable getTable() { + return table; + } + /** Changes the activePlayer to the next {@link Player} in the list */ public void nextPlayer() { activePlayer = (activePlayer + 1) % 4; @@ -32,5 +37,4 @@ public class GameState { public StoneHeap getGameHeap() { return gameHeap; } - } diff --git a/src/jrummikub/model/Hand.java b/src/jrummikub/model/Hand.java index 033dfa1..73ffab5 100644 --- a/src/jrummikub/model/Hand.java +++ b/src/jrummikub/model/Hand.java @@ -1,6 +1,6 @@ package jrummikub.model; /** Class managing a {@link Player}'s {@link Stone}s */ -public class Hand extends StoneTray { +public class Hand extends StoneTray implements IHand { } diff --git a/src/jrummikub/model/IHand.java b/src/jrummikub/model/IHand.java new file mode 100644 index 0000000..8bc7e7f --- /dev/null +++ b/src/jrummikub/model/IHand.java @@ -0,0 +1,5 @@ +package jrummikub.model; + +public interface IHand extends IStoneTray { + +} diff --git a/src/jrummikub/model/IStoneTray.java b/src/jrummikub/model/IStoneTray.java new file mode 100644 index 0000000..a9afed3 --- /dev/null +++ b/src/jrummikub/model/IStoneTray.java @@ -0,0 +1,40 @@ +package jrummikub.model; + +import jrummikub.util.Pair; + +public interface IStoneTray extends + Iterable> { + + /** + * Removes object from tray and returns it + * + * @param position + * position of the object that will be removed + * @return the picked up stone + */ + public E pickUp(Position position); + + /** + * Adds object to the tray + * + * @param object + * object to add to Hand + * @param position + * {@link Position} to put the object + */ + public void drop(E object, Position position); + + /** + * Returns the position of an object that is already on the tray + * + * @param object + * object whose position is requested + * @return position of the object or null when the object is not on the tray + */ + public Position getPosition(E object); + + public void pickUp(E object); + + public IStoneTray clone(); + +} \ No newline at end of file diff --git a/src/jrummikub/model/ITable.java b/src/jrummikub/model/ITable.java new file mode 100644 index 0000000..18e7d40 --- /dev/null +++ b/src/jrummikub/model/ITable.java @@ -0,0 +1,16 @@ +package jrummikub.model; + +public interface ITable { + + /** + * Removes {@link Stone} from the Table + * + * @param stone + * stone to pick up + */ + public void pickUpStone(Stone stone); + + /** Tests the Table for rule conflicts by checking all the {@link StoneSet} */ + public boolean isValid(); + +} \ No newline at end of file diff --git a/src/jrummikub/model/Player.java b/src/jrummikub/model/Player.java index 5e582ec..50a525a 100644 --- a/src/jrummikub/model/Player.java +++ b/src/jrummikub/model/Player.java @@ -4,22 +4,22 @@ import java.awt.Color; /** Class managing player data. No methods in release 1 */ public class Player { + + private IHand hand; + private Color color; + + // private String name; + public Player(Color color) { + hand = new Hand(); this.color = color; } - private Hand hand; - - private Color color; - - public Hand getHand() { + public IHand getHand() { return hand; } public Color getColor() { return color; } - - // private String name; - } diff --git a/src/jrummikub/model/StoneTray.java b/src/jrummikub/model/StoneTray.java index 21412a9..080786b 100644 --- a/src/jrummikub/model/StoneTray.java +++ b/src/jrummikub/model/StoneTray.java @@ -11,10 +11,9 @@ import jrummikub.util.Pair; * or {@link StoneSet}s. * * @param - * Type of positioned objects (must implement Sizeable) + * Type of positioned objects (must implement Sizeable) */ -public class StoneTray implements - Iterable> { +public class StoneTray implements IStoneTray { protected HashMap objects = new HashMap(); /** Possible move directions in case of overlapping Stones/Sets */ @@ -23,13 +22,12 @@ public class StoneTray implements LEFT, RIGHT, TOP, BOTTOM; } - /** - * Removes object from tray and returns it + /* + * (non-Javadoc) * - * @param position - * position of the object that will be removed - * @return the picked up stone + * @see jrummikub.model.IStoneTray#pickUp(jrummikub.model.Position) */ + @Override public E pickUp(Position position) { for (Map.Entry i : objects.entrySet()) { Position currentPosition = i.getValue(); @@ -41,12 +39,10 @@ public class StoneTray implements if (position.getY() < currentPosition.getY()) { continue; } - if (position.getX() > currentPosition.getX() - + currentObject.getWidth()) { + if (position.getX() > currentPosition.getX() + currentObject.getWidth()) { continue; } - if (position.getY() > currentPosition.getY() - + currentObject.getHeight()) { + if (position.getY() > currentPosition.getY() + currentObject.getHeight()) { continue; } // Position is inside the current object @@ -56,14 +52,12 @@ public class StoneTray implements return null; } - /** - * Adds object to the tray + /* + * (non-Javadoc) * - * @param object - * object to add to Hand - * @param position - * {@link Position} to put the object + * @see jrummikub.model.IStoneTray#drop(E, jrummikub.model.Position) */ + @Override public void drop(E object, Position position) { if (object != null) { drop(object, position, null); @@ -72,11 +66,11 @@ public class StoneTray implements @SuppressWarnings("unchecked") private void drop(E object, Position position, Direction direction) { - for (Map.Entry i : ((Map)objects.clone()).entrySet()) { + for (Map.Entry i : ((Map) objects.clone()) + .entrySet()) { Position currentPosition = i.getValue(); E currentObject = i.getKey(); - if (!objectsOverlap(object, position, currentObject, - currentPosition)) { + if (!objectsOverlap(object, position, currentObject, currentPosition)) { continue; } // Object would be placed inside the current object @@ -87,23 +81,23 @@ public class StoneTray implements // Move object to avoid overlap switch (direction) { case TOP: - newPosition = new Position(currentPosition.getX(), - position.getY() - currentObject.getHeight()); + newPosition = new Position(currentPosition.getX(), position.getY() + - currentObject.getHeight()); break; case BOTTOM: - newPosition = new Position(currentPosition.getX(), - position.getY() + object.getHeight()); + newPosition = new Position(currentPosition.getX(), position.getY() + + object.getHeight()); break; case LEFT: - newPosition = new Position(position.getX() - - currentObject.getWidth(), currentPosition.getY()); + newPosition = new Position(position.getX() - currentObject.getWidth(), + currentPosition.getY()); break; case RIGHT: newPosition = new Position(position.getX() + object.getWidth(), currentPosition.getY()); break; } - + objects.remove(i.getKey()); drop(currentObject, newPosition, direction); } @@ -159,37 +153,36 @@ public class StoneTray implements float blockingRight = blocking.getValue().getX() + blocking.getKey().getWidth(); float overlapRight = Math.min(objectRight, blockingRight); - float overlapLeft = Math.max(position.getX(), blocking.getValue() - .getX()); + float overlapLeft = Math.max(position.getX(), blocking.getValue().getX()); float overlapX = overlapRight - overlapLeft; float objectBottom = position.getY() + object.getHeight(); float blockingBottom = blocking.getValue().getY() + blocking.getKey().getHeight(); float overlapBottom = Math.min(objectBottom, blockingBottom); - float overlapTop = Math.max(position.getY(), blocking.getValue() - .getY()); + float overlapTop = Math.max(position.getY(), blocking.getValue().getY()); float overlapY = overlapBottom - overlapTop; // vertical or horizontal Shift // TODO magic factor return overlapX > overlapY; } - /** - * Returns the position of an object that is already on the tray + /* + * (non-Javadoc) * - * @param object - * object whose position is requested - * @return position of the object or null when the object is not on the tray + * @see jrummikub.model.IStoneTray#getPosition(E) */ + @Override public Position getPosition(E object) { return objects.get(object); } @Override public Iterator> iterator() { - final Iterator> entryIterator = objects.entrySet().iterator(); + final Iterator> entryIterator = objects.entrySet() + .iterator(); return new Iterator>() { Iterator> iterator = entryIterator; + @Override public boolean hasNext() { return iterator.hasNext(); @@ -205,18 +198,29 @@ public class StoneTray implements public void remove() { iterator.remove(); } - + }; } + /* + * (non-Javadoc) + * + * @see jrummikub.model.IStoneTray#pickUp(E) + */ + @Override public void pickUp(E object) { objects.remove(object); } - - + + /* + * (non-Javadoc) + * + * @see jrummikub.model.IStoneTray#clone() + */ + @SuppressWarnings("unchecked") @Override - public StoneTray clone() { - StoneTray copy = new StoneTray(); + public IStoneTray clone() { + StoneTray copy = new StoneTray(); copy.objects = (HashMap) objects.clone(); return copy; } diff --git a/src/jrummikub/model/Table.java b/src/jrummikub/model/Table.java index e82ca03..232986f 100644 --- a/src/jrummikub/model/Table.java +++ b/src/jrummikub/model/Table.java @@ -4,7 +4,7 @@ import jrummikub.util.Pair; /** Class administering the {@link Stone}s on the game-Table */ -public class Table extends StoneTray { +public class Table extends StoneTray implements ITable { /** * Removes {@link Stone} from the Table @@ -12,6 +12,7 @@ public class Table extends StoneTray { * @param stone * stone to pick up */ + @Override public void pickUpStone(Stone stone) { // Find the set of the stone StoneSet set = null; @@ -66,6 +67,7 @@ public class Table extends StoneTray { } /** Tests the Table for rule conflicts by checking all the {@link StoneSet} */ + @Override public boolean isValid() { for (Pair i : this) { if (!i.getFirst().isValid()) { -- cgit v1.2.3