More javadocs for the model

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@64 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Jannis Harder 2011-05-02 04:46:00 +02:00
parent 91b921248f
commit 1e7cdb33f5
8 changed files with 153 additions and 51 deletions

View file

@ -9,15 +9,33 @@ public class Position {
private float x; private float x;
private float y; private float y;
/**
* Create a new position by specifying the coordinates
*
* @param x
* x coordinate
* @param y
* y coordinate
*/
public Position(float x, float y) { public Position(float x, float y) {
this.x = x; this.x = x;
this.y = y; this.y = y;
} }
/**
* Get the x coordinate of the position
*
* @return x coordinate
*/
public float getX() { public float getX() {
return x; return x;
} }
/**
* Get the y coordinate of the position
*
* @return y coordinate
*/
public float getY() { public float getY() {
return y; return y;
} }

View file

@ -1,9 +1,22 @@
package jrummikub.model; package jrummikub.model;
/** /**
* Objects that have a size * Objects that have a size. This interface has to be implemented for objects
* placed on a @{link StoneTray} to make detection of overlapping objects
* possible.
*/ */
public interface Sizeable { public interface Sizeable {
/**
* Get the width of the object
*
* @return object width
*/
public float getWidth(); public float getWidth();
/**
* Get the height of the object
*
* @return object height
*/
public float getHeight(); public float getHeight();
} }

View file

@ -7,37 +7,67 @@ public class Stone implements Sizeable {
private StoneColor color; private StoneColor color;
private final boolean joker; private final boolean joker;
/**
* Creates a joker of the given color. The color is only used for
* displaying.
*
* @param color
* joker color
*/
public Stone(StoneColor color) { public Stone(StoneColor color) {
this.value = 0; this.value = 0;
this.color = color; this.color = color;
this.joker = true; this.joker = true;
} }
/**
* Creates a normal stone of a given color and value
*
* @param value
* stone value
* @param color
* stone color
*/
public Stone(int value, StoneColor color) { public Stone(int value, StoneColor color) {
this.value = value; this.value = value;
this.color = color; this.color = color;
this.joker = false; this.joker = false;
} }
@Deprecated
public Stone(int value, StoneColor color, boolean joker) { public Stone(int value, StoneColor color, boolean joker) {
this.value = value; this.value = value;
this.color = color; this.color = color;
this.joker = joker; this.joker = joker;
} }
/**
* Returns the color of the stone.
*
* @return stone color
*/
public StoneColor getColor() { public StoneColor getColor() {
return color; return color;
} }
/**
* Returns whether the stone is a joker or not.
*
* @return true when the stone is a joker
*/
public boolean isJoker() { public boolean isJoker() {
return joker; return joker;
} }
/**
* Returns the value of the stone. Don't use this value for jokers.
*
* @return stone value
*/
public int getValue() { public int getValue() {
return value; return value;
} }
@Override @Override
public float getWidth() { public float getWidth() {
return 1; return 1;

View file

@ -20,16 +20,20 @@ public class StoneHeap {
for (int i = 1; i <= 13; i++) { for (int i = 1; i <= 13; i++) {
for (int j = 0; j < 2; j++) { for (int j = 0; j < 2; j++) {
for (StoneColor c : EnumSet.allOf(StoneColor.class)) { for (StoneColor c : EnumSet.allOf(StoneColor.class)) {
heap.add(new Stone(i, c, false)); heap.add(new Stone(i, c));
} }
} }
} }
// Joker // Joker
heap.add(new Stone(0, StoneColor.BLACK, true)); heap.add(new Stone(StoneColor.BLACK));
heap.add(new Stone(0, StoneColor.RED, true)); heap.add(new Stone(StoneColor.RED));
} }
/** Removes random {@link Stone} from the heap and returns it */ /**
* Removes random {@link Stone} from the heap and returns it
*
* @return the drawn stone
*/
public Stone drawStone() { public Stone drawStone() {
return heap.remove(generator.nextInt(heap.size())); return heap.remove(generator.nextInt(heap.size()));
} }
@ -39,6 +43,7 @@ public class StoneHeap {
* *
* @param number * @param number
* number of requested Stones * number of requested Stones
* @return list of drawn stones
*/ */
public List<Stone> drawStones(int number) { public List<Stone> drawStones(int number) {
List<Stone> drawnStones = new ArrayList<Stone>(); List<Stone> drawnStones = new ArrayList<Stone>();

View file

@ -24,7 +24,11 @@ public class StoneSet implements Iterable<Stone>, Sizeable {
this.stones = new ArrayList<Stone>(stones); this.stones = new ArrayList<Stone>(stones);
} }
/** Test for rule conflict within the StoneSet */ /**
* Test for rule conflict within the StoneSet
*
* @return true when the set is valid according to the rules
*/
public boolean isValid() { public boolean isValid() {
if (stones.size() < 3) { if (stones.size() < 3) {
return false; return false;
@ -80,6 +84,7 @@ public class StoneSet implements Iterable<Stone>, Sizeable {
} }
return true; return true;
} }
/** /**
* Test for rule conflict within the StoneSet, assuming we have a group * Test for rule conflict within the StoneSet, assuming we have a group
*/ */
@ -107,6 +112,7 @@ public class StoneSet implements Iterable<Stone>, Sizeable {
* *
* @param position * @param position
* Splitting {@link Position} * Splitting {@link Position}
* @return A pair of StoneSets, one for each split part
*/ */
public Pair<StoneSet, StoneSet> splitAt(int position) { public Pair<StoneSet, StoneSet> splitAt(int position) {
// Exception in case of wrong index // Exception in case of wrong index
@ -124,6 +130,7 @@ public class StoneSet implements Iterable<Stone>, Sizeable {
* *
* @param other * @param other
* StoneSet to be joined to active StoneSet * StoneSet to be joined to active StoneSet
* @return the combined StoneSet
*/ */
public StoneSet join(StoneSet other) { public StoneSet join(StoneSet other) {
List<Stone> joinedList = new ArrayList<Stone>(); List<Stone> joinedList = new ArrayList<Stone>();
@ -132,10 +139,22 @@ public class StoneSet implements Iterable<Stone>, Sizeable {
return new StoneSet(joinedList); return new StoneSet(joinedList);
} }
/**
* Returns the number of stones in the set.
*
* @return number of stones
*/
public int size() { public int size() {
return stones.size(); return stones.size();
} }
/**
* Returns the i-th stone of the set (starting with 0)
*
* @param i
* number of the stone to return
* @return the i-th stone
*/
public Stone get(int i) { public Stone get(int i) {
return stones.get(i); return stones.get(i);
} }

View file

@ -9,6 +9,9 @@ import jrummikub.util.Pair;
/** /**
* A StoneTray is a collection of positioned objects (for example {@link Stone}s * A StoneTray is a collection of positioned objects (for example {@link Stone}s
* or {@link StoneSet}s. * or {@link StoneSet}s.
*
* @param <E>
* Type of positioned objects (must implement Sizeable)
*/ */
public class StoneTray<E extends Sizeable> implements public class StoneTray<E extends Sizeable> implements
Iterable<Pair<E, Position>> { Iterable<Pair<E, Position>> {
@ -20,12 +23,12 @@ public class StoneTray<E extends Sizeable> implements
LEFT, RIGHT, TOP, BOTTOM; LEFT, RIGHT, TOP, BOTTOM;
} }
/** /**
* Removes object from tray and returns it * Removes object from tray and returns it
* *
* @param position * @param position
* position of the object that will be removed * position of the object that will be removed
* @return the picked up stone
*/ */
public E pickUp(Position position) { public E pickUp(Position position) {
for (Pair<E, Position> i : objects) { for (Pair<E, Position> i : objects) {
@ -69,8 +72,8 @@ public class StoneTray<E extends Sizeable> implements
for (Pair<E, Position> i : objects) { for (Pair<E, Position> i : objects) {
Position currentPosition = i.getSecond(); Position currentPosition = i.getSecond();
E currentObject = i.getFirst(); E currentObject = i.getFirst();
if (!objectsOverlap(object, position, if (!objectsOverlap(object, position, currentObject,
currentObject, currentPosition)) { currentPosition)) {
continue; continue;
} }
// Object would be placed inside the current object // Object would be placed inside the current object
@ -113,12 +116,10 @@ public class StoneTray<E extends Sizeable> implements
if (position1.getY() + object1.getHeight() <= position2.getY()) { if (position1.getY() + object1.getHeight() <= position2.getY()) {
return false; return false;
} }
if (position1.getX() >= position2.getX() if (position1.getX() >= position2.getX() + object2.getWidth()) {
+ object2.getWidth()) {
return false; return false;
} }
if (position1.getY() >= position2.getY() if (position1.getY() >= position2.getY() + object2.getHeight()) {
+ object2.getHeight()) {
return false; return false;
} }
return true; return true;
@ -169,6 +170,13 @@ public class StoneTray<E extends Sizeable> implements
return overlapX > overlapY; return overlapX > overlapY;
} }
/**
* 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 Position getPosition(E object) {
for (Pair<E, Position> i : objects) { for (Pair<E, Position> i : objects) {
if (object.equals(i.getFirst())) { if (object.equals(i.getFirst())) {

View file

@ -31,7 +31,7 @@ public class Table extends StoneTray<StoneSet> {
return null; return null;
} }
/** Tests the Table for rule conflicts by checking all the {@link StoneSets} */ /** Tests the Table for rule conflicts by checking all the {@link StoneSet} */
public boolean isValid() { public boolean isValid() {
// TODO implement this method // TODO implement this method
return false; return false;

View file

@ -2,18 +2,23 @@ package jrummikub.util;
/** /**
* A pair of objects * A pair of objects
* @param <T1> the first type of the pair *
* @param <T2> the second type of the pair * @param <T1>
* Type of first component
* @param <T2>
* Type of second component
*/ */
public class Pair<T1, T2> { public class Pair<T1, T2> {
private final T1 first; private final T1 first;
private final T2 second; private final T2 second;
/** /**
* Create a new pair * Create a new pair from two values
* *
* @param first the first value * @param first
* @param second the second value * the first pair component
* @param second
* the second pair component
*/ */
public Pair(T1 first, T2 second) { public Pair(T1 first, T2 second) {
this.first = first; this.first = first;
@ -21,14 +26,18 @@ public class Pair<T1, T2> {
} }
/** /**
* @return the first value * Extract the first component of a pair
*
* @return the first pair component
*/ */
public T1 getFirst() { public T1 getFirst() {
return first; return first;
} }
/** /**
* @return the first value * Extract the second component of a pair
*
* @return the second pair component
*/ */
public T2 getSecond() { public T2 getSecond() {
return second; return second;