summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/model
diff options
context:
space:
mode:
authorJannis Harder <harder@informatik.uni-luebeck.de>2011-05-02 02:00:50 +0200
committerJannis Harder <harder@informatik.uni-luebeck.de>2011-05-02 02:00:50 +0200
commitb89d29aac2eed38a0696eb2d81876e5c817f9327 (patch)
treed89b478d62b3d659259c433bd29845a6520ffa9c /src/jrummikub/model
parentd1eb2d241e9fa0f9a57fabf4b57e9960bd9588cb (diff)
downloadJRummikub-b89d29aac2eed38a0696eb2d81876e5c817f9327.tar
JRummikub-b89d29aac2eed38a0696eb2d81876e5c817f9327.zip
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
Diffstat (limited to 'src/jrummikub/model')
-rw-r--r--src/jrummikub/model/GameState.java5
-rw-r--r--src/jrummikub/model/Stone.java13
-rw-r--r--src/jrummikub/model/StoneSet.java79
-rw-r--r--src/jrummikub/model/StoneTray.java36
-rw-r--r--src/jrummikub/model/Table.java9
5 files changed, 93 insertions, 49 deletions
diff --git a/src/jrummikub/model/GameState.java b/src/jrummikub/model/GameState.java
index d5c12ba..83aeff6 100644
--- a/src/jrummikub/model/GameState.java
+++ b/src/jrummikub/model/GameState.java
@@ -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;
}
}
diff --git a/src/jrummikub/model/Stone.java b/src/jrummikub/model/Stone.java
index 569d099..9721602 100644
--- a/src/jrummikub/model/Stone.java
+++ b/src/jrummikub/model/Stone.java
@@ -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;
+ }
+
}
diff --git a/src/jrummikub/model/StoneSet.java b/src/jrummikub/model/StoneSet.java
index 98d952f..465692a 100644
--- a/src/jrummikub/model/StoneSet.java
+++ b/src/jrummikub/model/StoneSet.java
@@ -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;
+ }
+ /**
+ * 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;
}
+ return true;
}
/**
diff --git a/src/jrummikub/model/StoneTray.java b/src/jrummikub/model/StoneTray.java
index ff32153..0d48bf5 100644
--- a/src/jrummikub/model/StoneTray.java
+++ b/src/jrummikub/model/StoneTray.java
@@ -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);
diff --git a/src/jrummikub/model/Table.java b/src/jrummikub/model/Table.java
index 8a1b40f..591b04e 100644
--- a/src/jrummikub/model/Table.java
+++ b/src/jrummikub/model/Table.java
@@ -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;
}
}