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:
parent
d1eb2d241e
commit
b89d29aac2
5 changed files with 93 additions and 49 deletions
|
@ -1,5 +1,7 @@
|
||||||
package jrummikub.model;
|
package jrummikub.model;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/** Class managing the overall and momentary GameState */
|
/** Class managing the overall and momentary GameState */
|
||||||
public class GameState {
|
public class GameState {
|
||||||
private Table table;
|
private Table table;
|
||||||
|
@ -13,7 +15,8 @@ public class GameState {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Player activePlayer() {
|
public Player activePlayer() {
|
||||||
|
// TODO implement this
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ package jrummikub.model;
|
||||||
|
|
||||||
/** Basic Rummikub Stone */
|
/** Basic Rummikub Stone */
|
||||||
|
|
||||||
public class Stone {
|
public class Stone implements Sizeable {
|
||||||
private int value;
|
private int value;
|
||||||
private StoneColor color;
|
private StoneColor color;
|
||||||
private final boolean joker;
|
private final boolean joker;
|
||||||
|
@ -37,4 +37,15 @@ public class Stone {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public float getWidth() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public float getHeight() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,8 +43,25 @@ public class StoneSet implements Iterable<Stone>, Sizeable {
|
||||||
// is run
|
// is run
|
||||||
if (stones.get(nonJoker1).getColor() == stones.get(nonJoker2)
|
if (stones.get(nonJoker1).getColor() == stones.get(nonJoker2)
|
||||||
.getColor()) {
|
.getColor()) {
|
||||||
StoneColor runColor = stones.get(nonJoker1).getColor();
|
return isValidRun(nonJoker1);
|
||||||
int startValue = stones.get(nonJoker1).getValue() - nonJoker1;
|
|
||||||
|
}
|
||||||
|
// is group
|
||||||
|
else {
|
||||||
|
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;
|
int endValue = startValue + stones.size() - 1;
|
||||||
if (startValue < 1 || endValue > 13) {
|
if (startValue < 1 || endValue > 13) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -62,10 +79,11 @@ public class StoneSet implements Iterable<Stone>, Sizeable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
// is group
|
/**
|
||||||
else {
|
* Test for rule conflict within the StoneSet, assuming we have a group
|
||||||
|
*/
|
||||||
|
private boolean isValidGroup() {
|
||||||
if (stones.size() > 4) {
|
if (stones.size() > 4) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -82,7 +100,6 @@ public class StoneSet implements Iterable<Stone>, Sizeable {
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Splits the StoneSet at a specified {@link Position} and returns two new
|
* Splits the StoneSet at a specified {@link Position} and returns two new
|
||||||
|
|
|
@ -69,19 +69,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();
|
||||||
// Tests if position is left of, above ... the current object
|
if (!objectsOverlap(object, position,
|
||||||
if (position.getX() + object.getWidth() <= currentPosition.getX()) {
|
currentObject, currentPosition)) {
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (position.getY() + object.getHeight() <= currentPosition.getY()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (position.getX() >= currentPosition.getX()
|
|
||||||
+ currentObject.getWidth()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (position.getY() >= currentPosition.getY()
|
|
||||||
+ currentObject.getHeight()) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// Object would be placed inside the current object
|
// 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));
|
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,
|
private Direction getMoveDirection(E object, Position position,
|
||||||
Pair<E, Position> blocking) {
|
Pair<E, Position> blocking) {
|
||||||
boolean isVertical = getMoveOrientationn(object, position, blocking);
|
boolean isVertical = getMoveOrientationn(object, position, blocking);
|
||||||
|
|
|
@ -14,7 +14,8 @@ public class Table extends StoneTray<StoneSet> {
|
||||||
* {@link Position} of the selected {@link Stone}
|
* {@link Position} of the selected {@link Stone}
|
||||||
*/
|
*/
|
||||||
public Stone pickUpStone(Position position) {
|
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
|
* End {@link Position} of the range
|
||||||
*/
|
*/
|
||||||
public StoneSet pickUpRange(Position from, Position to) {
|
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} */
|
/** Tests the Table for rule conflicts by checking all the {@link StoneSets} */
|
||||||
public boolean isValid() {
|
public boolean isValid() {
|
||||||
|
// TODO implement this method
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue