From 682357b5841f7853212e354199d1a46589663364 Mon Sep 17 00:00:00 2001 From: Ida Massow Date: Mon, 6 Jun 2011 19:53:45 +0200 Subject: Double ist das neue float git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@377 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/model/Hand.java | 20 +++++++++++-------- src/jrummikub/model/Position.java | 18 ++++++++--------- src/jrummikub/model/StoneTray.java | 40 +++++++++++++++++++------------------- 3 files changed, 41 insertions(+), 37 deletions(-) (limited to 'src/jrummikub/model') diff --git a/src/jrummikub/model/Hand.java b/src/jrummikub/model/Hand.java index b6d9eb4..812a4e4 100644 --- a/src/jrummikub/model/Hand.java +++ b/src/jrummikub/model/Hand.java @@ -43,10 +43,10 @@ public class Hand extends StoneTray implements IHand { } @Override - protected Pair fixInvalidDrop(Stone stone, Position pos, - Direction dir) { - float x = pos.getX(); - float y = pos.getY(); + protected Pair 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 implements IHand { return new Pair(new Position(0, y), RIGHT); } else { if (getFreeRowSpace((int) y) == 0) { - return new Pair(new Position(0, y + 1), RIGHT); + return new Pair(new Position(0, y + 1), + RIGHT); } else { - return new Pair(new Position(WIDTH - 1, y), LEFT); + return new Pair( + new Position(WIDTH - 1, y), LEFT); } } } @@ -83,7 +85,8 @@ public class Hand extends StoneTray implements IHand { List stones = new ArrayList(); - for (Iterator> iter = this.iterator(); iter.hasNext();) { + for (Iterator> iter = this.iterator(); iter + .hasNext();) { stones.add(iter.next().getFirst()); } @@ -101,7 +104,8 @@ public class Hand extends StoneTray implements IHand { public int getIdenticalStoneCount() { List stones = new ArrayList(); - for (Iterator> iter = this.iterator(); iter.hasNext();) { + for (Iterator> iter = this.iterator(); iter + .hasNext();) { stones.add(iter.next().getFirst()); } diff --git a/src/jrummikub/model/Position.java b/src/jrummikub/model/Position.java index 7396120..47c6c89 100644 --- a/src/jrummikub/model/Position.java +++ b/src/jrummikub/model/Position.java @@ -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; } diff --git a/src/jrummikub/model/StoneTray.java b/src/jrummikub/model/StoneTray.java index c4230f2..1f59afa 100644 --- a/src/jrummikub/model/StoneTray.java +++ b/src/jrummikub/model/StoneTray.java @@ -100,17 +100,17 @@ public class StoneTray implements IStoneTray { 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 implements IStoneTray { private Direction getMoveDirection(E object, Position position, Pair 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 implements IStoneTray { */ private boolean getMoveOrientation(E object, Position position, Pair 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; -- cgit v1.2.3