Refactored model to conform to the requested metrics

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@59 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Jannis Harder 2011-05-02 02:00:50 +02:00
parent d1eb2d241e
commit b89d29aac2
5 changed files with 93 additions and 49 deletions

View file

@ -1,5 +1,7 @@
package jrummikub.model;
import java.util.List;
/** Class managing the overall and momentary GameState */
public class GameState {
private Table table;
@ -13,7 +15,8 @@ public class GameState {
}
public Player activePlayer() {
// TODO implement this
return null;
}
}

View file

@ -2,7 +2,7 @@ package jrummikub.model;
/** Basic Rummikub Stone */
public class Stone {
public class Stone implements Sizeable {
private int value;
private StoneColor color;
private final boolean joker;
@ -37,4 +37,15 @@ public class Stone {
return value;
}
@Override
public float getWidth() {
return 1;
}
@Override
public float getHeight() {
return 1;
}
}

View file

@ -43,45 +43,62 @@ public class StoneSet implements Iterable<Stone>, Sizeable {
// is run
if (stones.get(nonJoker1).getColor() == stones.get(nonJoker2)
.getColor()) {
StoneColor runColor = stones.get(nonJoker1).getColor();
int startValue = stones.get(nonJoker1).getValue() - nonJoker1;
int endValue = startValue + stones.size() - 1;
if (startValue < 1 || endValue > 13) {
return false;
}
for (int i = 0; i < stones.size(); i++) {
if (stones.get(i).isJoker()) {
continue;
}
if (stones.get(i).getColor() != runColor) {
// warum macht er das nicht?
return false;
}
if (stones.get(i).getValue() != i + startValue) {
return false;
}
}
return true;
return isValidRun(nonJoker1);
}
// is group
else {
if (stones.size() > 4) {
return isValidGroup();
}
}
/**
* Test for rule conflict within the StoneSet, assuming we have a run
*
* @param referencePosition
* position of stone used as reference (any non-joker stone)
*/
private boolean isValidRun(int referencePosition) {
StoneColor runColor = stones.get(referencePosition).getColor();
int startValue = stones.get(referencePosition).getValue()
- referencePosition;
int endValue = startValue + stones.size() - 1;
if (startValue < 1 || endValue > 13) {
return false;
}
for (int i = 0; i < stones.size(); i++) {
if (stones.get(i).isJoker()) {
continue;
}
if (stones.get(i).getColor() != runColor) {
// warum macht er das nicht?
return false;
}
Set<StoneColor> seenColors = new HashSet<StoneColor>();
for (Stone i : stones) {
if (i.isJoker()) {
continue;
}
if (seenColors.contains(i.getColor())) {
return false;
} else {
seenColors.add(i.getColor());
}
if (stones.get(i).getValue() != i + startValue) {
return false;
}
return true;
}
return true;
}
/**
* Test for rule conflict within the StoneSet, assuming we have a group
*/
private boolean isValidGroup() {
if (stones.size() > 4) {
return false;
}
Set<StoneColor> seenColors = new HashSet<StoneColor>();
for (Stone i : stones) {
if (i.isJoker()) {
continue;
}
if (seenColors.contains(i.getColor())) {
return false;
} else {
seenColors.add(i.getColor());
}
}
return true;
}
/**

View file

@ -69,19 +69,8 @@ public class StoneTray<E extends Sizeable> implements
for (Pair<E, Position> i : objects) {
Position currentPosition = i.getSecond();
E currentObject = i.getFirst();
// Tests if position is left of, above ... the current object
if (position.getX() + object.getWidth() <= currentPosition.getX()) {
continue;
}
if (position.getY() + object.getHeight() <= currentPosition.getY()) {
continue;
}
if (position.getX() >= currentPosition.getX()
+ currentObject.getWidth()) {
continue;
}
if (position.getY() >= currentPosition.getY()
+ currentObject.getHeight()) {
if (!objectsOverlap(object, position,
currentObject, currentPosition)) {
continue;
}
// Object would be placed inside the current object
@ -114,6 +103,27 @@ public class StoneTray<E extends Sizeable> implements
objects.add(new Pair<E, Position>(object, position));
}
/** Tests whether two objects overlap **/
private boolean objectsOverlap(E object1, Position position1, E object2,
Position position2) {
// Tests if position is left of, above ... the current object
if (position1.getX() + object1.getWidth() <= position2.getX()) {
return false;
}
if (position1.getY() + object1.getHeight() <= position2.getY()) {
return false;
}
if (position1.getX() >= position2.getX()
+ object2.getWidth()) {
return false;
}
if (position1.getY() >= position2.getY()
+ object2.getHeight()) {
return false;
}
return true;
}
private Direction getMoveDirection(E object, Position position,
Pair<E, Position> blocking) {
boolean isVertical = getMoveOrientationn(object, position, blocking);

View file

@ -14,7 +14,8 @@ public class Table extends StoneTray<StoneSet> {
* {@link Position} of the selected {@link Stone}
*/
public Stone pickUpStone(Position position) {
// TODO implement this method
return null;
}
/**
@ -26,11 +27,13 @@ public class Table extends StoneTray<StoneSet> {
* End {@link Position} of the range
*/
public StoneSet pickUpRange(Position from, Position to) {
// TODO implement this method
return null;
}
/** Tests the Table for rule conflicts by checking all the {@link StoneSets} */
public boolean isValid() {
// TODO implement this method
return false;
}
}