Double ist das neue float
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@377 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
41786cb842
commit
682357b584
7 changed files with 170 additions and 148 deletions
|
@ -43,10 +43,10 @@ public class Hand extends StoneTray<Stone> implements IHand {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected Pair<Position, Direction> fixInvalidDrop(Stone stone, Position pos,
|
||||
Direction dir) {
|
||||
float x = pos.getX();
|
||||
float y = pos.getY();
|
||||
protected Pair<Position, Direction> fixInvalidDrop(Stone stone,
|
||||
Position pos, Direction dir) {
|
||||
double x = pos.getX();
|
||||
double y = pos.getY();
|
||||
|
||||
if (x >= 0 && x <= WIDTH - 1) {
|
||||
return null;
|
||||
|
@ -55,9 +55,11 @@ public class Hand extends StoneTray<Stone> implements IHand {
|
|||
return new Pair<Position, Direction>(new Position(0, y), RIGHT);
|
||||
} else {
|
||||
if (getFreeRowSpace((int) y) == 0) {
|
||||
return new Pair<Position, Direction>(new Position(0, y + 1), RIGHT);
|
||||
return new Pair<Position, Direction>(new Position(0, y + 1),
|
||||
RIGHT);
|
||||
} else {
|
||||
return new Pair<Position, Direction>(new Position(WIDTH - 1, y), LEFT);
|
||||
return new Pair<Position, Direction>(
|
||||
new Position(WIDTH - 1, y), LEFT);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -83,7 +85,8 @@ public class Hand extends StoneTray<Stone> implements IHand {
|
|||
|
||||
List<Stone> stones = new ArrayList<Stone>();
|
||||
|
||||
for (Iterator<Pair<Stone, Position>> iter = this.iterator(); iter.hasNext();) {
|
||||
for (Iterator<Pair<Stone, Position>> iter = this.iterator(); iter
|
||||
.hasNext();) {
|
||||
stones.add(iter.next().getFirst());
|
||||
}
|
||||
|
||||
|
@ -101,7 +104,8 @@ public class Hand extends StoneTray<Stone> implements IHand {
|
|||
public int getIdenticalStoneCount() {
|
||||
List<Stone> stones = new ArrayList<Stone>();
|
||||
|
||||
for (Iterator<Pair<Stone, Position>> iter = this.iterator(); iter.hasNext();) {
|
||||
for (Iterator<Pair<Stone, Position>> iter = this.iterator(); iter
|
||||
.hasNext();) {
|
||||
stones.add(iter.next().getFirst());
|
||||
}
|
||||
|
||||
|
|
|
@ -7,8 +7,8 @@ package jrummikub.model;
|
|||
|
||||
public class Position {
|
||||
|
||||
private float x;
|
||||
private float y;
|
||||
private double x;
|
||||
private double y;
|
||||
|
||||
/**
|
||||
* Create a new position by specifying the coordinates
|
||||
|
@ -18,7 +18,7 @@ public class Position {
|
|||
* @param y
|
||||
* y coordinate
|
||||
*/
|
||||
public Position(float x, float y) {
|
||||
public Position(double x, double y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ public class Position {
|
|||
*
|
||||
* @return x coordinate
|
||||
*/
|
||||
public float getX() {
|
||||
public double getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
|
@ -37,7 +37,7 @@ public class Position {
|
|||
*
|
||||
* @return y coordinate
|
||||
*/
|
||||
public float getY() {
|
||||
public double getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
|
@ -55,9 +55,9 @@ public class Position {
|
|||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Position other = (Position) obj;
|
||||
if (Float.floatToIntBits(x) != Float.floatToIntBits(other.x))
|
||||
if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x))
|
||||
return false;
|
||||
if (Float.floatToIntBits(y) != Float.floatToIntBits(other.y))
|
||||
if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
@ -66,8 +66,8 @@ public class Position {
|
|||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + Float.floatToIntBits(x);
|
||||
result = prime * result + Float.floatToIntBits(y);
|
||||
result = (int) (prime * result + Double.doubleToLongBits(x));
|
||||
result = (int) (prime * result + Double.doubleToLongBits(y));
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
@ -100,17 +100,17 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
|||
return null;
|
||||
}
|
||||
|
||||
private static boolean lessOrEqual(float x, float y) {
|
||||
if (-0.000001f < y && y < 0.000001f) {
|
||||
return (x < y + 0.000001f);
|
||||
private static boolean lessOrEqual(double d, double e) {
|
||||
if (-0.000001 < e && e < 0.000001) {
|
||||
return (d < e + 0.000001);
|
||||
}
|
||||
|
||||
float q = x / y;
|
||||
if (0.999999f < q && q < 1.000001f) {
|
||||
double q = d / e;
|
||||
if (0.999999 < q && q < 1.000001) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return x < y;
|
||||
return d < e;
|
||||
}
|
||||
|
||||
/** Tests whether two objects overlap **/
|
||||
|
@ -137,11 +137,11 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
|||
private Direction getMoveDirection(E object, Position position,
|
||||
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.getSecond().getX()
|
||||
double objectMidpointX = position.getX() + object.getWidth() / 2;
|
||||
double objectMidpointY = position.getY() + object.getHeight() / 2;
|
||||
double blockingMidpointX = blocking.getSecond().getX()
|
||||
+ blocking.getFirst().getWidth() / 2;
|
||||
float blockingMidpointY = blocking.getSecond().getY()
|
||||
double blockingMidpointY = blocking.getSecond().getY()
|
||||
+ blocking.getFirst().getHeight() / 2;
|
||||
if (isVertical) {
|
||||
if (objectMidpointY < blockingMidpointY) {
|
||||
|
@ -165,20 +165,20 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
|||
*/
|
||||
private boolean getMoveOrientation(E object, Position position,
|
||||
Pair<E, Position> blocking) {
|
||||
float objectRight = position.getX() + object.getWidth();
|
||||
float blockingRight = blocking.getSecond().getX()
|
||||
double objectRight = position.getX() + object.getWidth();
|
||||
double blockingRight = blocking.getSecond().getX()
|
||||
+ blocking.getFirst().getWidth();
|
||||
float overlapRight = Math.min(objectRight, blockingRight);
|
||||
float overlapLeft = Math.max(position.getX(), blocking.getSecond()
|
||||
double overlapRight = Math.min(objectRight, blockingRight);
|
||||
double overlapLeft = Math.max(position.getX(), blocking.getSecond()
|
||||
.getX());
|
||||
float overlapX = overlapRight - overlapLeft;
|
||||
float objectBottom = position.getY() + object.getHeight();
|
||||
float blockingBottom = blocking.getSecond().getY()
|
||||
double overlapX = overlapRight - overlapLeft;
|
||||
double objectBottom = position.getY() + object.getHeight();
|
||||
double blockingBottom = blocking.getSecond().getY()
|
||||
+ blocking.getFirst().getHeight();
|
||||
float overlapBottom = Math.min(objectBottom, blockingBottom);
|
||||
float overlapTop = Math.max(position.getY(), blocking.getSecond()
|
||||
double overlapBottom = Math.min(objectBottom, blockingBottom);
|
||||
double overlapTop = Math.max(position.getY(), blocking.getSecond()
|
||||
.getY());
|
||||
float overlapY = overlapBottom - overlapTop;
|
||||
double overlapY = overlapBottom - overlapTop;
|
||||
// vertical or horizontal Shift
|
||||
// TODO magic factor
|
||||
return overlapX > overlapY;
|
||||
|
|
Reference in a new issue