Allow laying out stone sets
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@180 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
b9fbe279c3
commit
b86571cf83
8 changed files with 209 additions and 125 deletions
|
@ -31,9 +31,9 @@ public class JRummikub {
|
|||
GameState gameState = new GameState();
|
||||
View view = new View();
|
||||
|
||||
for (Stone stone : gameState.getGameHeap().drawStones(5)) {
|
||||
/*for (Stone stone : gameState.getGameHeap().drawStones(5)) {
|
||||
gameState.getTable().drop(new StoneSet(stone), new Position(0, 0));
|
||||
}
|
||||
}*/
|
||||
|
||||
RoundControl roundControl = new RoundControl(gameState, view);
|
||||
roundControl.startRound();
|
||||
|
|
|
@ -12,6 +12,7 @@ import jrummikub.util.Connection;
|
|||
import jrummikub.util.Event;
|
||||
import jrummikub.util.IEvent;
|
||||
import jrummikub.util.IListener;
|
||||
import jrummikub.util.IListener1;
|
||||
import jrummikub.util.IListener2;
|
||||
import jrummikub.view.IView;
|
||||
|
||||
|
@ -59,8 +60,7 @@ public class TurnControl {
|
|||
|
||||
@Override
|
||||
public void handle(Stone stone, Boolean collect) {
|
||||
handStoneClick(stone, collect);
|
||||
|
||||
stoneClick(stone, collect);
|
||||
}
|
||||
}));
|
||||
|
||||
|
@ -72,6 +72,36 @@ public class TurnControl {
|
|||
collectionStoneClick(stone, collect);
|
||||
}
|
||||
}));
|
||||
connections.add(view.getTablePanel().getStoneClickEvent()
|
||||
.add(new IListener2<Stone, Boolean>() {
|
||||
|
||||
@Override
|
||||
public void handle(Stone stone, Boolean collect) {
|
||||
stoneClick(stone, collect);
|
||||
}
|
||||
}));
|
||||
|
||||
connections.add(view.getTablePanel().getClickEvent()
|
||||
.add(new IListener1<Position>() {
|
||||
@Override
|
||||
public void handle(Position pos) {
|
||||
tableClick(pos);
|
||||
}
|
||||
}));
|
||||
connections.add(view.getTablePanel().getLeftConnectorClickEvent()
|
||||
.add(new IListener1<StoneSet>() {
|
||||
@Override
|
||||
public void handle(StoneSet set) {
|
||||
leftConnectorClick(set);
|
||||
}
|
||||
}));
|
||||
connections.add(view.getTablePanel().getRightConnectorClickEvent()
|
||||
.add(new IListener1<StoneSet>() {
|
||||
@Override
|
||||
public void handle(StoneSet set) {
|
||||
rightConnectorClick(set);
|
||||
}
|
||||
}));
|
||||
|
||||
view.getPlayerPanel().getHandPanel().setStones(hand.clone());
|
||||
view.enableStartTurnPanel(false);
|
||||
|
@ -87,7 +117,7 @@ public class TurnControl {
|
|||
|
||||
}
|
||||
|
||||
private void handStoneClick(Stone stone, boolean collect) {
|
||||
private void stoneClick(Stone stone, boolean collect) {
|
||||
if (collect) {
|
||||
if (!selectedStones.remove(stone)) {
|
||||
selectedStones.add(stone);
|
||||
|
@ -109,17 +139,34 @@ public class TurnControl {
|
|||
|
||||
view.setSelectedStones(selectedStones);
|
||||
}
|
||||
|
||||
private void leftConnectorClick(StoneSet set){
|
||||
|
||||
|
||||
private void pickUpSelectedStones() {
|
||||
for (Stone stone : selectedStones) {
|
||||
hand.pickUp(stone);
|
||||
table.pickUpStone(stone);
|
||||
}
|
||||
}
|
||||
|
||||
private void rightConnectorClick(StoneSet set){
|
||||
|
||||
|
||||
private void tableClick(Position position) {
|
||||
if (selectedStones.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
pickUpSelectedStones();
|
||||
table.drop(new StoneSet(selectedStones), position);
|
||||
selectedStones.clear();
|
||||
|
||||
view.getTablePanel().setStoneSets(table);
|
||||
view.getPlayerPanel().getHandPanel().setStones(hand);
|
||||
view.setSelectedStones(selectedStones);
|
||||
}
|
||||
|
||||
private void tableClick(Position position){
|
||||
|
||||
|
||||
private void leftConnectorClick(StoneSet set) {
|
||||
|
||||
}
|
||||
|
||||
private void rightConnectorClick(StoneSet set) {
|
||||
|
||||
}
|
||||
|
||||
private void endOfTurn() {
|
||||
|
|
|
@ -14,7 +14,7 @@ import jrummikub.util.Pair;
|
|||
* Type of positioned objects (must implement Sizeable)
|
||||
*/
|
||||
public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||
protected HashMap<E, Position> objects = new HashMap<E, Position>();
|
||||
protected HashMap<E, Pair<E, Position>> objects = new HashMap<E, Pair<E, Position>>();
|
||||
|
||||
/** Possible move directions in case of overlapping Stones/Sets */
|
||||
|
||||
|
@ -29,8 +29,8 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
|||
*/
|
||||
@Override
|
||||
public E pickUp(Position position) {
|
||||
for (Map.Entry<E, Position> i : objects.entrySet()) {
|
||||
Position currentPosition = i.getValue();
|
||||
for (Map.Entry<E, Pair<E, Position>> i : objects.entrySet()) {
|
||||
Position currentPosition = i.getValue().getSecond();
|
||||
E currentObject = i.getKey();
|
||||
// Tests if position is left of, above ... the current object
|
||||
if (position.getX() < currentPosition.getX()) {
|
||||
|
@ -68,10 +68,10 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
|||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void drop(E object, Position position, Direction direction) {
|
||||
for (Map.Entry<E, Position> i : ((Map<E, Position>) objects.clone())
|
||||
.entrySet()) {
|
||||
Position currentPosition = i.getValue();
|
||||
E currentObject = i.getKey();
|
||||
for (Pair<E, Position> i : ((Map<E, Pair<E, Position>>) objects.clone())
|
||||
.values()) {
|
||||
Position currentPosition = i.getSecond();
|
||||
E currentObject = i.getFirst();
|
||||
if (!objectsOverlap(object, position, currentObject,
|
||||
currentPosition)) {
|
||||
continue;
|
||||
|
@ -101,10 +101,10 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
|||
break;
|
||||
}
|
||||
|
||||
objects.remove(i.getKey());
|
||||
objects.remove(i.getFirst());
|
||||
drop(currentObject, newPosition, direction);
|
||||
}
|
||||
objects.put(object, position);
|
||||
objects.put(object, new Pair<E, Position>(object, position));
|
||||
}
|
||||
|
||||
/** Tests whether two objects overlap **/
|
||||
|
@ -127,14 +127,14 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
|||
}
|
||||
|
||||
private Direction getMoveDirection(E object, Position position,
|
||||
Map.Entry<E, Position> blocking) {
|
||||
boolean isVertical = getMoveOrientationn(object, position, blocking);
|
||||
Pair<E, Position> blocking) {
|
||||
boolean isVertical = getMoveOrientation(object, position, blocking);
|
||||
float objectMidpointX = position.getX() + object.getWidth() / 2;
|
||||
float objectMidpointY = position.getY() + object.getHeight() / 2;
|
||||
float blockingMidpointX = blocking.getValue().getX()
|
||||
+ blocking.getKey().getWidth() / 2;
|
||||
float blockingMidpointY = blocking.getValue().getY()
|
||||
+ blocking.getKey().getHeight() / 2;
|
||||
float blockingMidpointX = blocking.getSecond().getX()
|
||||
+ blocking.getFirst().getWidth() / 2;
|
||||
float blockingMidpointY = blocking.getSecond().getY()
|
||||
+ blocking.getFirst().getHeight() / 2;
|
||||
if (isVertical) {
|
||||
if (objectMidpointY < blockingMidpointY) {
|
||||
return Direction.BOTTOM;
|
||||
|
@ -150,21 +150,21 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
|||
}
|
||||
}
|
||||
|
||||
private boolean getMoveOrientationn(E object, Position position,
|
||||
Map.Entry<E, Position> blocking) {
|
||||
private boolean getMoveOrientation(E object, Position position,
|
||||
Pair<E, Position> blocking) {
|
||||
float objectRight = position.getX() + object.getWidth();
|
||||
float blockingRight = blocking.getValue().getX()
|
||||
+ blocking.getKey().getWidth();
|
||||
float blockingRight = blocking.getSecond().getX()
|
||||
+ blocking.getFirst().getWidth();
|
||||
float overlapRight = Math.min(objectRight, blockingRight);
|
||||
float overlapLeft = Math.max(position.getX(), blocking.getValue()
|
||||
float overlapLeft = Math.max(position.getX(), blocking.getSecond()
|
||||
.getX());
|
||||
float overlapX = overlapRight - overlapLeft;
|
||||
float objectBottom = position.getY() + object.getHeight();
|
||||
float blockingBottom = blocking.getValue().getY()
|
||||
+ blocking.getKey().getHeight();
|
||||
float blockingBottom = blocking.getSecond().getY()
|
||||
+ blocking.getFirst().getHeight();
|
||||
float overlapBottom = Math.min(objectBottom, blockingBottom);
|
||||
float overlapTop = Math
|
||||
.max(position.getY(), blocking.getValue().getY());
|
||||
.max(position.getY(), blocking.getSecond().getY());
|
||||
float overlapY = overlapBottom - overlapTop;
|
||||
// vertical or horizontal Shift
|
||||
// TODO magic factor
|
||||
|
@ -178,33 +178,12 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
|||
*/
|
||||
@Override
|
||||
public Position getPosition(E object) {
|
||||
return objects.get(object);
|
||||
return objects.get(object).getSecond();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Pair<E, Position>> iterator() {
|
||||
final Iterator<Map.Entry<E, Position>> entryIterator = objects
|
||||
.entrySet().iterator();
|
||||
return new Iterator<Pair<E, Position>>() {
|
||||
Iterator<Map.Entry<E, Position>> iterator = entryIterator;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return iterator.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pair<E, Position> next() {
|
||||
Map.Entry<E, Position> entry = iterator.next();
|
||||
return new Pair<E, Position>(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
iterator.remove();
|
||||
}
|
||||
|
||||
};
|
||||
return objects.values().iterator();
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -227,7 +206,7 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
|||
public IStoneTray<E> clone() {
|
||||
try {
|
||||
StoneTray<E> copy = (StoneTray<E>) super.clone();
|
||||
copy.objects = (HashMap<E, Position>) objects.clone();
|
||||
copy.objects = (HashMap<E, Pair<E, Position>>) objects.clone();
|
||||
|
||||
return copy;
|
||||
} catch (CloneNotSupportedException e) {
|
||||
|
|
|
@ -27,9 +27,7 @@ public class Table extends StoneTray<StoneSet> implements ITable {
|
|||
@Override
|
||||
public Pair<StoneSet, StoneSet> pickUpStone(Stone stone) {
|
||||
StoneInfo info = findStoneInfo(stone);
|
||||
|
||||
System.err.println("Stone: " + stone);
|
||||
|
||||
|
||||
if (info == null) {
|
||||
return null;
|
||||
}
|
||||
|
@ -76,14 +74,10 @@ public class Table extends StoneTray<StoneSet> implements ITable {
|
|||
private Pair<StoneSet, StoneSet> splitSet(StoneSet set, Position setPosition,
|
||||
int stonePosition) {
|
||||
pickUp(set);
|
||||
|
||||
|
||||
Pair<StoneSet, StoneSet> firstSplit = set.splitAt(stonePosition);
|
||||
System.err.println("Size: " + set.size());
|
||||
System.err.println("stonePosition: " + stonePosition);
|
||||
System.err.println("Frist split: " + firstSplit.getFirst() + " " + firstSplit.getSecond());
|
||||
Pair<StoneSet, StoneSet> secondSplit = firstSplit.getSecond().splitAt(1);
|
||||
System.err.println("Second split: " + secondSplit.getFirst() + " " + secondSplit.getSecond());
|
||||
|
||||
|
||||
StoneSet leftSet = firstSplit.getFirst();
|
||||
StoneSet rightSet = secondSplit.getSecond();
|
||||
|
||||
|
|
Reference in a new issue