2011-04-30 13:44:17 +02:00
|
|
|
package jrummikub.model;
|
|
|
|
|
2011-05-03 19:06:16 +02:00
|
|
|
import jrummikub.util.Pair;
|
|
|
|
|
2011-04-30 14:47:42 +02:00
|
|
|
/** Class administering the {@link Stone}s on the game-Table */
|
|
|
|
|
2011-05-04 16:23:10 +02:00
|
|
|
public class Table extends StoneTray<StoneSet> implements ITable {
|
2011-05-31 00:58:46 +02:00
|
|
|
private GameSettings gameSettings;
|
2011-04-30 13:44:17 +02:00
|
|
|
|
2011-05-04 19:41:41 +02:00
|
|
|
private static class StoneInfo {
|
|
|
|
StoneSet set;
|
|
|
|
Position setPosition;
|
|
|
|
int stonePosition;
|
2011-05-08 18:45:32 +02:00
|
|
|
|
2011-05-04 19:41:41 +02:00
|
|
|
public StoneInfo(StoneSet set, Position setPosition, int stonePosition) {
|
|
|
|
this.set = set;
|
|
|
|
this.setPosition = setPosition;
|
|
|
|
this.stonePosition = stonePosition;
|
|
|
|
}
|
|
|
|
}
|
2011-05-08 18:45:32 +02:00
|
|
|
|
2011-05-31 15:29:37 +02:00
|
|
|
/**
|
|
|
|
* Constructor for a table
|
|
|
|
*
|
|
|
|
* @param settings
|
|
|
|
* GameSettings
|
|
|
|
*/
|
2011-05-31 00:58:46 +02:00
|
|
|
public Table(GameSettings settings) {
|
|
|
|
gameSettings = settings;
|
|
|
|
}
|
|
|
|
|
2011-04-30 14:47:42 +02:00
|
|
|
/**
|
2011-05-03 19:06:13 +02:00
|
|
|
* Removes {@link Stone} from the Table
|
2011-04-30 14:47:42 +02:00
|
|
|
*
|
2011-05-03 19:06:13 +02:00
|
|
|
* @param stone
|
2011-05-31 00:58:46 +02:00
|
|
|
* stone to pick up
|
2011-04-30 14:47:42 +02:00
|
|
|
*/
|
2011-05-04 16:23:10 +02:00
|
|
|
@Override
|
2011-05-10 16:53:44 +02:00
|
|
|
public void pickUpStone(Stone stone) {
|
2011-05-04 19:41:41 +02:00
|
|
|
StoneInfo info = findStoneInfo(stone);
|
2011-05-09 00:33:34 +02:00
|
|
|
|
2011-05-10 16:53:44 +02:00
|
|
|
if (info != null) {
|
|
|
|
splitSet(info.set, info.setPosition, info.stonePosition);
|
2011-05-09 00:33:30 +02:00
|
|
|
}
|
2011-05-04 19:41:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private StoneInfo findStoneInfo(Stone stone) {
|
2011-05-03 19:06:16 +02:00
|
|
|
// Find the set of the stone
|
2011-05-04 19:41:41 +02:00
|
|
|
StoneInfo info;
|
2011-05-03 19:06:16 +02:00
|
|
|
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++;
|
|
|
|
}
|
2011-05-09 00:33:31 +02:00
|
|
|
set = null;
|
2011-05-03 19:06:16 +02:00
|
|
|
}
|
|
|
|
// Stone not found
|
|
|
|
if (set == null) {
|
2011-05-04 19:41:41 +02:00
|
|
|
info = null;
|
|
|
|
} else {
|
|
|
|
info = new StoneInfo(set, setPosition, stonePosition);
|
2011-05-03 19:06:16 +02:00
|
|
|
}
|
2011-05-04 19:41:41 +02:00
|
|
|
return info;
|
|
|
|
}
|
2011-05-08 18:45:32 +02:00
|
|
|
|
2011-05-04 19:41:41 +02:00
|
|
|
@Override
|
|
|
|
public StoneSet findStoneSet(Stone stone) {
|
|
|
|
StoneInfo info = findStoneInfo(stone);
|
|
|
|
if (info == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return info.set;
|
2011-05-03 19:06:16 +02:00
|
|
|
}
|
|
|
|
|
2011-05-31 00:58:46 +02:00
|
|
|
private void splitSet(StoneSet set, Position setPosition, int stonePosition) {
|
2011-05-03 19:06:16 +02:00
|
|
|
pickUp(set);
|
2011-05-09 00:33:34 +02:00
|
|
|
|
2011-05-03 19:06:16 +02:00
|
|
|
Pair<StoneSet, StoneSet> firstSplit = set.splitAt(stonePosition);
|
2011-05-31 00:58:46 +02:00
|
|
|
Pair<StoneSet, StoneSet> secondSplit = firstSplit.getSecond()
|
|
|
|
.splitAt(1);
|
2011-05-09 00:33:34 +02:00
|
|
|
|
2011-05-03 19:06:16 +02:00
|
|
|
StoneSet leftSet = firstSplit.getFirst();
|
|
|
|
StoneSet rightSet = secondSplit.getSecond();
|
2011-05-08 18:45:32 +02:00
|
|
|
|
2011-05-31 00:58:46 +02:00
|
|
|
if (set.classify(gameSettings).getFirst() == StoneSet.Type.RUN) {
|
2011-05-03 19:06:16 +02:00
|
|
|
Position leftPosition, rightPosition;
|
|
|
|
leftPosition = setPosition;
|
2011-05-31 00:58:46 +02:00
|
|
|
rightPosition = new Position(
|
|
|
|
setPosition.getX() + stonePosition + 1, setPosition.getY());
|
2011-05-08 18:45:32 +02:00
|
|
|
|
2011-05-03 19:06:16 +02:00
|
|
|
drop(leftSet, leftPosition);
|
|
|
|
drop(rightSet, rightPosition);
|
|
|
|
} else {
|
2011-05-08 18:45:32 +02:00
|
|
|
Position newPosition = new Position(setPosition.getX() + 0.5f,
|
|
|
|
setPosition.getY());
|
|
|
|
|
2011-05-03 19:06:16 +02:00
|
|
|
if (leftSet == null) {
|
|
|
|
drop(rightSet, newPosition);
|
|
|
|
} else if (rightSet == null) {
|
|
|
|
drop(leftSet, newPosition);
|
|
|
|
} else {
|
|
|
|
drop(leftSet.join(rightSet), newPosition);
|
|
|
|
}
|
|
|
|
}
|
2011-04-30 13:44:17 +02:00
|
|
|
}
|
|
|
|
|
2011-05-02 04:46:00 +02:00
|
|
|
/** Tests the Table for rule conflicts by checking all the {@link StoneSet} */
|
2011-05-04 16:23:10 +02:00
|
|
|
@Override
|
2011-04-30 13:44:17 +02:00
|
|
|
public boolean isValid() {
|
2011-05-03 19:06:16 +02:00
|
|
|
for (Pair<StoneSet, Position> i : this) {
|
2011-05-31 00:58:46 +02:00
|
|
|
if (!i.getFirst().isValid(gameSettings)) {
|
2011-05-03 19:06:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2011-04-30 13:44:17 +02:00
|
|
|
}
|
2011-05-03 19:06:13 +02:00
|
|
|
|
2011-04-30 13:44:17 +02:00
|
|
|
}
|