Make Table.pickUpStone return the the stone sets that are created
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@171 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
82c58a1eae
commit
3a2c0638e3
2 changed files with 24 additions and 17 deletions
|
@ -1,5 +1,7 @@
|
||||||
package jrummikub.model;
|
package jrummikub.model;
|
||||||
|
|
||||||
|
import jrummikub.util.Pair;
|
||||||
|
|
||||||
public interface ITable extends IStoneTray<StoneSet> {
|
public interface ITable extends IStoneTray<StoneSet> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -7,8 +9,9 @@ public interface ITable extends IStoneTray<StoneSet> {
|
||||||
*
|
*
|
||||||
* @param stone
|
* @param stone
|
||||||
* stone to pick up
|
* stone to pick up
|
||||||
|
* @return the stone sets that are created by taking pickung the the stone
|
||||||
*/
|
*/
|
||||||
public void pickUpStone(Stone stone);
|
public Pair<StoneSet, StoneSet> pickUpStone(Stone stone);
|
||||||
|
|
||||||
/** 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();
|
||||||
|
|
|
@ -10,24 +10,25 @@ public class Table extends StoneTray<StoneSet> implements ITable {
|
||||||
StoneSet set;
|
StoneSet set;
|
||||||
Position setPosition;
|
Position setPosition;
|
||||||
int stonePosition;
|
int stonePosition;
|
||||||
|
|
||||||
public StoneInfo(StoneSet set, Position setPosition, int stonePosition) {
|
public StoneInfo(StoneSet set, Position setPosition, int stonePosition) {
|
||||||
this.set = set;
|
this.set = set;
|
||||||
this.setPosition = setPosition;
|
this.setPosition = setPosition;
|
||||||
this.stonePosition = stonePosition;
|
this.stonePosition = stonePosition;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes {@link Stone} from the Table
|
* Removes {@link Stone} from the Table
|
||||||
*
|
*
|
||||||
* @param stone
|
* @param stone
|
||||||
* stone to pick up
|
* stone to pick up
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void pickUpStone(Stone stone) {
|
public Pair<StoneSet, StoneSet> pickUpStone(Stone stone) {
|
||||||
StoneInfo info = findStoneInfo(stone);
|
StoneInfo info = findStoneInfo(stone);
|
||||||
|
|
||||||
splitSet(info.set, info.setPosition, info.stonePosition);
|
return splitSet(info.set, info.setPosition, info.stonePosition);
|
||||||
}
|
}
|
||||||
|
|
||||||
private StoneInfo findStoneInfo(Stone stone) {
|
private StoneInfo findStoneInfo(Stone stone) {
|
||||||
|
@ -55,7 +56,7 @@ public class Table extends StoneTray<StoneSet> implements ITable {
|
||||||
}
|
}
|
||||||
return info;
|
return info;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StoneSet findStoneSet(Stone stone) {
|
public StoneSet findStoneSet(Stone stone) {
|
||||||
StoneInfo info = findStoneInfo(stone);
|
StoneInfo info = findStoneInfo(stone);
|
||||||
|
@ -64,28 +65,29 @@ public class Table extends StoneTray<StoneSet> implements ITable {
|
||||||
}
|
}
|
||||||
return info.set;
|
return info.set;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void splitSet(StoneSet set, Position setPosition, int stonePosition) {
|
private Pair<StoneSet, StoneSet> splitSet(StoneSet set, Position setPosition,
|
||||||
|
int stonePosition) {
|
||||||
pickUp(set);
|
pickUp(set);
|
||||||
|
|
||||||
Pair<StoneSet, StoneSet> firstSplit = set.splitAt(stonePosition);
|
Pair<StoneSet, StoneSet> firstSplit = set.splitAt(stonePosition);
|
||||||
Pair<StoneSet, StoneSet> secondSplit = firstSplit.getSecond().splitAt(1);
|
Pair<StoneSet, StoneSet> secondSplit = firstSplit.getSecond().splitAt(1);
|
||||||
|
|
||||||
StoneSet leftSet = firstSplit.getFirst();
|
StoneSet leftSet = firstSplit.getFirst();
|
||||||
StoneSet rightSet = secondSplit.getSecond();
|
StoneSet rightSet = secondSplit.getSecond();
|
||||||
|
|
||||||
if (set.classify() == StoneSet.Type.RUN) {
|
if (set.classify() == StoneSet.Type.RUN) {
|
||||||
Position leftPosition, rightPosition;
|
Position leftPosition, rightPosition;
|
||||||
leftPosition = setPosition;
|
leftPosition = setPosition;
|
||||||
rightPosition = new Position(setPosition.getX() + stonePosition, setPosition.getY());
|
rightPosition = new Position(setPosition.getX() + stonePosition,
|
||||||
|
setPosition.getY());
|
||||||
|
|
||||||
drop(leftSet, leftPosition);
|
drop(leftSet, leftPosition);
|
||||||
drop(rightSet, rightPosition);
|
drop(rightSet, rightPosition);
|
||||||
} else {
|
} else {
|
||||||
Position newPosition = new Position(setPosition.getX() + 0.5f, setPosition.getY());
|
Position newPosition = new Position(setPosition.getX() + 0.5f,
|
||||||
|
setPosition.getY());
|
||||||
|
|
||||||
if (leftSet == null) {
|
if (leftSet == null) {
|
||||||
drop(rightSet, newPosition);
|
drop(rightSet, newPosition);
|
||||||
} else if (rightSet == null) {
|
} else if (rightSet == null) {
|
||||||
|
@ -94,6 +96,8 @@ public class Table extends StoneTray<StoneSet> implements ITable {
|
||||||
drop(leftSet.join(rightSet), newPosition);
|
drop(leftSet.join(rightSet), newPosition);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return new Pair<StoneSet, StoneSet>(leftSet, rightSet);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Tests the Table for rule conflicts by checking all the {@link StoneSet} */
|
/** Tests the Table for rule conflicts by checking all the {@link StoneSet} */
|
||||||
|
|
Reference in a new issue