Implemented Table, passes tests
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@90 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
760f914494
commit
7b8732714d
2 changed files with 61 additions and 6 deletions
|
@ -1,5 +1,7 @@
|
||||||
package jrummikub.model;
|
package jrummikub.model;
|
||||||
|
|
||||||
|
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> {
|
||||||
|
@ -11,13 +13,66 @@ public class Table extends StoneTray<StoneSet> {
|
||||||
* stone to pick up
|
* stone to pick up
|
||||||
*/
|
*/
|
||||||
public void pickUpStone(Stone stone) {
|
public void pickUpStone(Stone stone) {
|
||||||
// TODO implement this method
|
// Find the set of the stone
|
||||||
|
StoneSet set = null;
|
||||||
|
Position setPosition = null;
|
||||||
|
int stonePosition = 0;
|
||||||
|
setLoop: for (Pair<StoneSet, Position> i : this) {
|
||||||
|
set = i.getFirst();
|
||||||
|
setPosition = i.getSecond();
|
||||||
|
stonePosition = 0;
|
||||||
|
for (Stone j : set) {
|
||||||
|
if (j == stone) {
|
||||||
|
break setLoop;
|
||||||
|
}
|
||||||
|
stonePosition++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Stone not found
|
||||||
|
if (set == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
splitSet(set, setPosition, stonePosition);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void splitSet(StoneSet set, Position setPosition, int stonePosition) {
|
||||||
|
pickUp(set);
|
||||||
|
|
||||||
|
Pair<StoneSet, StoneSet> firstSplit = set.splitAt(stonePosition);
|
||||||
|
Pair<StoneSet, StoneSet> secondSplit = firstSplit.getSecond().splitAt(1);
|
||||||
|
|
||||||
|
StoneSet leftSet = firstSplit.getFirst();
|
||||||
|
StoneSet rightSet = secondSplit.getSecond();
|
||||||
|
|
||||||
|
if (set.classify() == StoneSet.Type.RUN) {
|
||||||
|
Position leftPosition, rightPosition;
|
||||||
|
leftPosition = setPosition;
|
||||||
|
rightPosition = new Position(setPosition.getX() + stonePosition, setPosition.getY());
|
||||||
|
|
||||||
|
drop(leftSet, leftPosition);
|
||||||
|
drop(rightSet, rightPosition);
|
||||||
|
} else {
|
||||||
|
Position newPosition = new Position(setPosition.getX() + 0.5f, setPosition.getY());
|
||||||
|
|
||||||
|
if (leftSet == null) {
|
||||||
|
drop(rightSet, newPosition);
|
||||||
|
} else if (rightSet == null) {
|
||||||
|
drop(leftSet, newPosition);
|
||||||
|
} else {
|
||||||
|
drop(leftSet.join(rightSet), newPosition);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Tests the Table for rule conflicts by checking all the {@link StoneSet} */
|
/** Tests the Table for rule conflicts by checking all the {@link StoneSet} */
|
||||||
public boolean isValid() {
|
public boolean isValid() {
|
||||||
// TODO implement this method
|
for (Pair<StoneSet, Position> i : this) {
|
||||||
return false;
|
if (!i.getFirst().isValid()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,7 +50,7 @@ public class TableTest {
|
||||||
for (Object i : testTable) {
|
for (Object i : testTable) {
|
||||||
counter++;
|
counter++;
|
||||||
}
|
}
|
||||||
assertEquals(counter, 1);
|
assertEquals(1, counter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -58,7 +58,7 @@ public class TableTest {
|
||||||
public void testPickUpStoneRun() {
|
public void testPickUpStoneRun() {
|
||||||
Stone targetStone = new Stone(BLACK);
|
Stone targetStone = new Stone(BLACK);
|
||||||
testTable.drop(new StoneSet(Arrays.asList(new Stone(1, RED), targetStone,
|
testTable.drop(new StoneSet(Arrays.asList(new Stone(1, RED), targetStone,
|
||||||
new Stone(3, BLACK))), new Position(0,0));
|
new Stone(3, RED))), new Position(0,0));
|
||||||
assertTrue(testTable.isValid());
|
assertTrue(testTable.isValid());
|
||||||
testTable.pickUpStone(targetStone);
|
testTable.pickUpStone(targetStone);
|
||||||
assertFalse(testTable.isValid());
|
assertFalse(testTable.isValid());
|
||||||
|
@ -67,6 +67,6 @@ public class TableTest {
|
||||||
for (Object i : testTable) {
|
for (Object i : testTable) {
|
||||||
counter++;
|
counter++;
|
||||||
}
|
}
|
||||||
assertEquals(counter, 2);
|
assertEquals(2, counter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue