Added interfaces for model classes

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@104 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Bennet Gerlach 2011-05-04 16:23:10 +02:00
parent b5bbfc25de
commit c9843770dd
9 changed files with 129 additions and 58 deletions

View file

@ -2,19 +2,19 @@ package jrummikub.control;
import jrummikub.model.Hand; import jrummikub.model.Hand;
import jrummikub.model.Table; import jrummikub.model.ITable;
import jrummikub.util.Event; import jrummikub.util.Event;
import jrummikub.util.IEvent; import jrummikub.util.IEvent;
import jrummikub.view.IView; import jrummikub.view.IView;
public class TurnControl { public class TurnControl {
private Hand hand; private Hand hand;
private Table table; private ITable table;
private TurnTimer timer; private TurnTimer timer;
private IView view; private IView view;
private Event endOfTurnEvent = new Event(); private Event endOfTurnEvent = new Event();
public TurnControl(Hand hand, Table table, IView view) { public TurnControl(Hand hand, ITable table, IView view) {
this.hand = hand; this.hand = hand;
this.table = table; this.table = table;
this.view = view; this.view = view;

View file

@ -6,12 +6,13 @@ import java.util.List;
/** Class managing the overall and momentary GameState */ /** Class managing the overall and momentary GameState */
public class GameState { public class GameState {
Table table; ITable table;
List<Player> players; List<Player> players;
int activePlayer; int activePlayer;
private StoneHeap gameHeap; private StoneHeap gameHeap;
public GameState() { public GameState() {
table = new Table();
players = new ArrayList<Player>(); players = new ArrayList<Player>();
players.add(new Player(Color.red)); players.add(new Player(Color.red));
players.add(new Player(Color.yellow)); players.add(new Player(Color.yellow));
@ -20,6 +21,10 @@ public class GameState {
activePlayer = 0; activePlayer = 0;
} }
public ITable getTable() {
return table;
}
/** Changes the activePlayer to the next {@link Player} in the list */ /** Changes the activePlayer to the next {@link Player} in the list */
public void nextPlayer() { public void nextPlayer() {
activePlayer = (activePlayer + 1) % 4; activePlayer = (activePlayer + 1) % 4;
@ -32,5 +37,4 @@ public class GameState {
public StoneHeap getGameHeap() { public StoneHeap getGameHeap() {
return gameHeap; return gameHeap;
} }
} }

View file

@ -1,6 +1,6 @@
package jrummikub.model; package jrummikub.model;
/** Class managing a {@link Player}'s {@link Stone}s */ /** Class managing a {@link Player}'s {@link Stone}s */
public class Hand extends StoneTray<Stone> { public class Hand extends StoneTray<Stone> implements IHand {
} }

View file

@ -0,0 +1,5 @@
package jrummikub.model;
public interface IHand extends IStoneTray<Stone> {
}

View file

@ -0,0 +1,40 @@
package jrummikub.model;
import jrummikub.util.Pair;
public interface IStoneTray<E extends Sizeable> extends
Iterable<Pair<E, Position>> {
/**
* 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<E> clone();
}

View file

@ -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();
}

View file

@ -4,22 +4,22 @@ import java.awt.Color;
/** Class managing player data. No methods in release 1 */ /** Class managing player data. No methods in release 1 */
public class Player { public class Player {
private IHand hand;
private Color color;
// private String name;
public Player(Color color) { public Player(Color color) {
hand = new Hand();
this.color = color; this.color = color;
} }
private Hand hand; public IHand getHand() {
private Color color;
public Hand getHand() {
return hand; return hand;
} }
public Color getColor() { public Color getColor() {
return color; return color;
} }
// private String name;
} }

View file

@ -11,10 +11,9 @@ import jrummikub.util.Pair;
* or {@link StoneSet}s. * or {@link StoneSet}s.
* *
* @param <E> * @param <E>
* Type of positioned objects (must implement Sizeable) * Type of positioned objects (must implement Sizeable)
*/ */
public class StoneTray<E extends Sizeable> implements public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
Iterable<Pair<E, Position>> {
protected HashMap<E, Position> objects = new HashMap<E, Position>(); protected HashMap<E, Position> objects = new HashMap<E, Position>();
/** Possible move directions in case of overlapping Stones/Sets */ /** Possible move directions in case of overlapping Stones/Sets */
@ -23,13 +22,12 @@ public class StoneTray<E extends Sizeable> implements
LEFT, RIGHT, TOP, BOTTOM; LEFT, RIGHT, TOP, BOTTOM;
} }
/** /*
* Removes object from tray and returns it * (non-Javadoc)
* *
* @param position * @see jrummikub.model.IStoneTray#pickUp(jrummikub.model.Position)
* position of the object that will be removed
* @return the picked up stone
*/ */
@Override
public E pickUp(Position position) { public E pickUp(Position position) {
for (Map.Entry<E, Position> i : objects.entrySet()) { for (Map.Entry<E, Position> i : objects.entrySet()) {
Position currentPosition = i.getValue(); Position currentPosition = i.getValue();
@ -41,12 +39,10 @@ public class StoneTray<E extends Sizeable> implements
if (position.getY() < currentPosition.getY()) { if (position.getY() < currentPosition.getY()) {
continue; continue;
} }
if (position.getX() > currentPosition.getX() if (position.getX() > currentPosition.getX() + currentObject.getWidth()) {
+ currentObject.getWidth()) {
continue; continue;
} }
if (position.getY() > currentPosition.getY() if (position.getY() > currentPosition.getY() + currentObject.getHeight()) {
+ currentObject.getHeight()) {
continue; continue;
} }
// Position is inside the current object // Position is inside the current object
@ -56,14 +52,12 @@ public class StoneTray<E extends Sizeable> implements
return null; return null;
} }
/** /*
* Adds object to the tray * (non-Javadoc)
* *
* @param object * @see jrummikub.model.IStoneTray#drop(E, jrummikub.model.Position)
* object to add to Hand
* @param position
* {@link Position} to put the object
*/ */
@Override
public void drop(E object, Position position) { public void drop(E object, Position position) {
if (object != null) { if (object != null) {
drop(object, position, null); drop(object, position, null);
@ -72,11 +66,11 @@ public class StoneTray<E extends Sizeable> implements
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private void drop(E object, Position position, Direction direction) { private void drop(E object, Position position, Direction direction) {
for (Map.Entry<E, Position> i : ((Map<E,Position>)objects.clone()).entrySet()) { for (Map.Entry<E, Position> i : ((Map<E, Position>) objects.clone())
.entrySet()) {
Position currentPosition = i.getValue(); Position currentPosition = i.getValue();
E currentObject = i.getKey(); E currentObject = i.getKey();
if (!objectsOverlap(object, position, currentObject, if (!objectsOverlap(object, position, currentObject, currentPosition)) {
currentPosition)) {
continue; continue;
} }
// Object would be placed inside the current object // Object would be placed inside the current object
@ -87,23 +81,23 @@ public class StoneTray<E extends Sizeable> implements
// Move object to avoid overlap // Move object to avoid overlap
switch (direction) { switch (direction) {
case TOP: case TOP:
newPosition = new Position(currentPosition.getX(), newPosition = new Position(currentPosition.getX(), position.getY()
position.getY() - currentObject.getHeight()); - currentObject.getHeight());
break; break;
case BOTTOM: case BOTTOM:
newPosition = new Position(currentPosition.getX(), newPosition = new Position(currentPosition.getX(), position.getY()
position.getY() + object.getHeight()); + object.getHeight());
break; break;
case LEFT: case LEFT:
newPosition = new Position(position.getX() newPosition = new Position(position.getX() - currentObject.getWidth(),
- currentObject.getWidth(), currentPosition.getY()); currentPosition.getY());
break; break;
case RIGHT: case RIGHT:
newPosition = new Position(position.getX() + object.getWidth(), newPosition = new Position(position.getX() + object.getWidth(),
currentPosition.getY()); currentPosition.getY());
break; break;
} }
objects.remove(i.getKey()); objects.remove(i.getKey());
drop(currentObject, newPosition, direction); drop(currentObject, newPosition, direction);
} }
@ -159,37 +153,36 @@ public class StoneTray<E extends Sizeable> implements
float blockingRight = blocking.getValue().getX() float blockingRight = blocking.getValue().getX()
+ blocking.getKey().getWidth(); + blocking.getKey().getWidth();
float overlapRight = Math.min(objectRight, blockingRight); float overlapRight = Math.min(objectRight, blockingRight);
float overlapLeft = Math.max(position.getX(), blocking.getValue() float overlapLeft = Math.max(position.getX(), blocking.getValue().getX());
.getX());
float overlapX = overlapRight - overlapLeft; float overlapX = overlapRight - overlapLeft;
float objectBottom = position.getY() + object.getHeight(); float objectBottom = position.getY() + object.getHeight();
float blockingBottom = blocking.getValue().getY() float blockingBottom = blocking.getValue().getY()
+ blocking.getKey().getHeight(); + blocking.getKey().getHeight();
float overlapBottom = Math.min(objectBottom, blockingBottom); float overlapBottom = Math.min(objectBottom, blockingBottom);
float overlapTop = Math.max(position.getY(), blocking.getValue() float overlapTop = Math.max(position.getY(), blocking.getValue().getY());
.getY());
float overlapY = overlapBottom - overlapTop; float overlapY = overlapBottom - overlapTop;
// vertical or horizontal Shift // vertical or horizontal Shift
// TODO magic factor // TODO magic factor
return overlapX > overlapY; return overlapX > overlapY;
} }
/** /*
* Returns the position of an object that is already on the tray * (non-Javadoc)
* *
* @param object * @see jrummikub.model.IStoneTray#getPosition(E)
* object whose position is requested
* @return position of the object or null when the object is not on the tray
*/ */
@Override
public Position getPosition(E object) { public Position getPosition(E object) {
return objects.get(object); return objects.get(object);
} }
@Override @Override
public Iterator<Pair<E, Position>> iterator() { public Iterator<Pair<E, Position>> iterator() {
final Iterator<Map.Entry<E, Position>> entryIterator = objects.entrySet().iterator(); final Iterator<Map.Entry<E, Position>> entryIterator = objects.entrySet()
.iterator();
return new Iterator<Pair<E, Position>>() { return new Iterator<Pair<E, Position>>() {
Iterator<Map.Entry<E, Position>> iterator = entryIterator; Iterator<Map.Entry<E, Position>> iterator = entryIterator;
@Override @Override
public boolean hasNext() { public boolean hasNext() {
return iterator.hasNext(); return iterator.hasNext();
@ -205,18 +198,29 @@ public class StoneTray<E extends Sizeable> implements
public void remove() { public void remove() {
iterator.remove(); iterator.remove();
} }
}; };
} }
/*
* (non-Javadoc)
*
* @see jrummikub.model.IStoneTray#pickUp(E)
*/
@Override
public void pickUp(E object) { public void pickUp(E object) {
objects.remove(object); objects.remove(object);
} }
/*
* (non-Javadoc)
*
* @see jrummikub.model.IStoneTray#clone()
*/
@SuppressWarnings("unchecked")
@Override @Override
public StoneTray<E> clone() { public IStoneTray<E> clone() {
StoneTray<E> copy = new StoneTray(); StoneTray<E> copy = new StoneTray<E>();
copy.objects = (HashMap<E, Position>) objects.clone(); copy.objects = (HashMap<E, Position>) objects.clone();
return copy; return copy;
} }

View file

@ -4,7 +4,7 @@ import jrummikub.util.Pair;
/** Class administering the {@link Stone}s on the game-Table */ /** Class administering the {@link Stone}s on the game-Table */
public class Table extends StoneTray<StoneSet> { public class Table extends StoneTray<StoneSet> implements ITable {
/** /**
* Removes {@link Stone} from the Table * Removes {@link Stone} from the Table
@ -12,6 +12,7 @@ public class Table extends StoneTray<StoneSet> {
* @param stone * @param stone
* stone to pick up * stone to pick up
*/ */
@Override
public void pickUpStone(Stone stone) { public void pickUpStone(Stone stone) {
// Find the set of the stone // Find the set of the stone
StoneSet set = null; StoneSet set = null;
@ -66,6 +67,7 @@ public class Table extends StoneTray<StoneSet> {
} }
/** Tests the Table for rule conflicts by checking all the {@link StoneSet} */ /** Tests the Table for rule conflicts by checking all the {@link StoneSet} */
@Override
public boolean isValid() { public boolean isValid() {
for (Pair<StoneSet, Position> i : this) { for (Pair<StoneSet, Position> i : this) {
if (!i.getFirst().isValid()) { if (!i.getFirst().isValid()) {