diff --git a/OGP1718-Worms/src/Main.java b/OGP1718-Worms/src/Main.java deleted file mode 100644 index 5e75eb6..0000000 --- a/OGP1718-Worms/src/Main.java +++ /dev/null @@ -1,35 +0,0 @@ -import worms.facade.Facade; -import worms.facade.IFacade; -import worms.model.Team; -import worms.model.World; -import worms.model.Worm; -import worms.programs.BinaryExpression; -import worms.programs.Expression; -import worms.programs.UnaryExpression; -import worms.util.Coordinate; - -import java.io.Console; -import java.util.concurrent.ThreadLocalRandom; -import java.util.function.BinaryOperator; -import java.util.function.DoubleBinaryOperator; - -public class Main { - private static final double EPS = 1e-4; - private static boolean[][] passableMap = new boolean[][] { // - { false, false, false, false }, // - { true, true, true, true }, // - { true, true, true, true }, // - { false, false, false, false } }; - - public static void main(String[] args) { - - for (int i = 0; i < 10; i++) { - - int x =ThreadLocalRandom.current().nextInt(0, 15) / 2; - x += (x % 2 == 0 ? 1:0); - System.out.println(x); - - } - - } -} diff --git a/OGP1718-Worms/src/worms/model/World.java b/OGP1718-Worms/src/worms/model/World.java index 7b578f4..8a2b290 100644 --- a/OGP1718-Worms/src/worms/model/World.java +++ b/OGP1718-Worms/src/worms/model/World.java @@ -481,7 +481,7 @@ public class World { obj.setWorld(null); throw new IllegalArgumentException(); } - else if(Worm.class.isInstance(obj) && (!obj.isValidLocation(obj.getLocation()) || !isAdjacent(obj.getLocation(), obj.getRadius()) )) { + else if(obj instanceof Worm && (!obj.isValidLocation(obj.getLocation()) || !isAdjacent(obj.getLocation(), obj.getRadius()) )) { obj.setWorld(null); throw new IllegalArgumentException(); } diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index 2f3a98a..9649ef6 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -13,7 +13,7 @@ import java.util.concurrent.ThreadLocalRandom; /** * A class with the specifications of the worm * - * @authors Arthur Bols and Leen Dereu (1ste bachelor informatica) + * @Authors Arthur Bols and Leen Dereu (1ste bachelor informatica) * * URL: https://github.com/KUL-ogp/ogp1718-project-bols-dereu * @@ -663,7 +663,7 @@ public class Worm extends GameObject implements IJumpable{ double maxLocDirection = minDirection; Coordinate maxLoc = location; - for (; minDirection <= maxDirection; minDirection += 0.0175) { + for (; minDirection <= maxDirection && minDirection <= 2 * PI; minDirection += 0.0175) { if (minDirection < 0) minDirection = 0.0; Coordinate tempLoc = getFurthestLocationInDirection(minDirection, this.radius); if (!this.world.isAdjacent(tempLoc, this.radius)) tempLoc = location; @@ -672,10 +672,15 @@ public class Worm extends GameObject implements IJumpable{ maxLocDirection = minDirection; } } - Coordinate tempLoc = getFurthestLocationInDirection(maxDirection, this.radius); - if (this.world.isAdjacent(tempLoc, this.radius) && getDistance(location, tempLoc) / Math.abs(direction - minDirection) > getDistance(location, maxLoc) / Math.abs(direction - maxLocDirection)) { - maxLoc = tempLoc; - maxLocDirection = maxDirection; + + if (maxDirection <= 2 * PI) { + + Coordinate tempLoc = getFurthestLocationInDirection(maxDirection, this.radius); + if (this.world.isAdjacent(tempLoc, this.radius) && getDistance(location, tempLoc) / + Math.abs(direction - minDirection) > getDistance(location, maxLoc) / Math.abs(direction - maxLocDirection)) { + maxLoc = tempLoc; + maxLocDirection = maxDirection; + } } if (maxLoc.getX() == location.getX() && maxLoc.getY() == location.getY()) return direction; diff --git a/OGP1718-Worms/src/worms/util/Coordinate.java b/OGP1718-Worms/src/worms/util/Coordinate.java index fd3e49c..3984874 100644 --- a/OGP1718-Worms/src/worms/util/Coordinate.java +++ b/OGP1718-Worms/src/worms/util/Coordinate.java @@ -63,7 +63,7 @@ public class Coordinate extends Tuple { * |result == new double[]{getX(), getY()} */ public double[] toArray() { - return new double[]{getX(), getY()}; + return new double[]{this.item1, this.item2}; } /** diff --git a/OGP1718-Worms/tests/worms/model/WormTest.java b/OGP1718-Worms/tests/worms/model/WormTest.java index 3b8560b..ab49c6f 100644 --- a/OGP1718-Worms/tests/worms/model/WormTest.java +++ b/OGP1718-Worms/tests/worms/model/WormTest.java @@ -1,94 +1,94 @@ -//package worms.model; -// -//import org.junit.jupiter.api.Test; -//import org.junit.jupiter.api.BeforeAll; -//import org.junit.jupiter.api.RepeatedTest; -//import static org.junit.jupiter.api.Assertions.*; -//import worms.util.Coordinate; -// -//import java.util.Random; -// -//class WormTest { -// -// private static Worm worm; -// private static Random r = new Random(); -// -// @BeforeAll -// static void setUp() { -// worm = new Worm(Coordinate.create(0.0, 0.0), 0, "Test", 1); -// } -// -// @RepeatedTest(5000) -// void move() { -// worm = new Worm(Coordinate.create(0.0, 0.0), 0, "Test", 1); -// int steps = r.nextInt(5101) - 100; -// double maxAngle = Math.abs(2 * Math.PI - worm.getOrientation()); -// double minAngle = worm.getOrientation(); -// double combAngle = maxAngle + minAngle; -// double angle = r.nextDouble() * combAngle - minAngle; -// worm.turn(angle); -// long cost = (long) Math.ceil(Math.abs(Math.cos(worm.getOrientation())) + Math.abs(4 * Math.sin(worm.getOrientation()))) * steps; -// if (steps < 0 || cost > worm.getActionPoints()) { -// assertThrows(IllegalArgumentException.class, () -> worm.move(steps)); -// } -// else { -// -// double newLocX = worm.getLocation().item1 + steps * worm.getRadius() * Math.cos(worm.getOrientation()); -// double newLocY = worm.getLocation().item2 + steps * worm.getRadius() * Math.sin(worm.getOrientation()); -// worm.move(steps); -// assertEquals(Coordinate.create(newLocX, newLocY), worm.getLocation()); -// } -// } -// -// @RepeatedTest(5000) -// void turn() { -// double angle = r.nextDouble() * 4 * Math.PI - 2 * Math.PI; -// double oldAngle = worm.getOrientation(); -// -// if (!Worm.isValidOrientation(oldAngle + angle) || worm.getActionPoints() - (long) Math.ceil(Math.toDegrees(angle) / 6) < 0) { -// assertThrows(AssertionError.class, () -> worm.turn(angle)); -// worm = new Worm(Coordinate.create(0.0, 0.0), 0, "Test", 1); -// } -// else { -// worm.turn(angle); -// assertEquals(oldAngle + angle, worm.getOrientation()); -// } -// -// } -// -// @Test -// void turnInvalidValue() { -// assertThrows(AssertionError.class, () -> worm.turn(Double.NaN)); -// } -// -// @RepeatedTest(5000) -// void jump() { -// -// worm = new Worm(Coordinate.create(0.0, 0.0), 0, "Test", 1); -// double maxAngle = Math.abs(2 * Math.PI - worm.getOrientation()); -// double minAngle = worm.getOrientation(); -// double combAngle = maxAngle + minAngle; -// double angle = r.nextDouble() * combAngle - minAngle; -// worm.turn(angle); -// int steps = r.nextInt(100); -// worm.move(steps); -// -// if (worm.getOrientation() >= Math.PI || worm.getActionPoints() == 0){ -// assertThrows(IllegalStateException.class, () -> worm.jump()); -// } -// else { -// double orient = worm.getOrientation(); -// double force = 5 * worm.getActionPoints() + worm.getMass() * Worm.G; -// double v = force / worm.getMass() * Worm.FORCE_TIME; -// double newLocX = worm.getLocation().item1 + Math.pow(v, 2) * Math.sin(2 * orient) / Worm.G; -// worm.jump(); -// assertEquals(Coordinate.create(newLocX, worm.getLocation().getY()), worm.getLocation()); -// } -// } -// @Test -// void doubleJump() { -// worm = new Worm(Coordinate.create(0.0, 0.0), 0, "Test", 1); -// worm.jump(); -// assertThrows(IllegalStateException.class, () -> worm.jump()); -// } -//} \ No newline at end of file +package worms.model; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.RepeatedTest; + +import static java.lang.Math.*; +import static org.junit.jupiter.api.Assertions.*; +import worms.util.Coordinate; + +import java.util.Random; + +class WormTest { + + + private static boolean[][] map = new boolean[][] { { false, false, false, false, false, false, false, false, false, false }, + { true, true, true, true, true, true, true, true, true, false }, + { true, true, true, true, true, true, true, true, true, false }, + { true, true, true, true, true, true, true, true, true, false }, + { true, true, true, true, true, true, true, true, true, false }, + { true, true, true, true, true, true, true, true, true, false }, + { true, true, true, true, true, true, true, true, true, false }, + { true, true, true, true, true, true, true, true, true, false }, + { true, true, true, true, true, true, true, true, true, false }, + { true, true, true, true, true, true, true, true, true, false }, }; + private static World world; + + private static Worm worm; + private static Random r = new Random(); + + @BeforeAll + static void setUp() { + worm = new Worm(world, new double[]{1, 1}, 0, 0.5, "Test", null); + world = new World(10.0, 10.0, map); + } + + @RepeatedTest(500) + void move() { + worm = new Worm(world, new double[] { 8.375, 3.0 }, 0, 0.6, "Test", null); + + double maxAngle = Math.abs(2 * Math.PI - worm.getOrientation()); + double minAngle = worm.getOrientation(); + double combAngle = maxAngle + minAngle; + double angle = r.nextDouble() * combAngle - minAngle; + worm.turn(angle); + double direction = worm.getFurthestLocationDirection(); + + long cost = (long) Math.ceil(abs(direction * cos(worm.getOrientation())) + abs(4 * direction * sin(worm.getOrientation()))); + if (direction < 0 || direction > 2 * Math.PI || Double.isNaN(direction) || worm.getRadius() < 0 || + Double.isNaN(worm.getRadius()) || Double.isInfinite(worm.getRadius()) || cost > worm.getActionPoints()) { + try { + Coordinate newLoc = worm.getFurthestLocationInDirection(direction, worm.getRadius()); + worm.move(); + assertEquals(newLoc, worm.getLocation()); + } catch (IllegalArgumentException e) { + return; + } + + } else { + Coordinate newLoc = worm.getFurthestLocationInDirection(direction, worm.getRadius()); + worm.move(); + assertEquals(newLoc, worm.getLocation()); + } + } + + @RepeatedTest(5000) + void turn() { + double angle = r.nextDouble() * 4 * Math.PI - 2 * Math.PI; + double oldAngle = worm.getOrientation(); + + if (!Worm.isValidOrientation(oldAngle + angle) || worm.getActionPoints() - (long) Math.ceil(Math.toDegrees(angle) / 6) < 0) { + assertThrows(AssertionError.class, () -> worm.turn(angle)); + worm = new Worm(world, new double[] { 8.375, 3.0 }, 0, 0.6, "Test", null); + } + else { + worm.turn(angle); + assertEquals(oldAngle + angle, worm.getOrientation()); + } + + } + + @Test + void turnInvalidValue() { + assertThrows(AssertionError.class, () -> worm.turn(Double.NaN)); + } + + + @Test + void doubleJump() { + worm = new Worm(world, new double[] { 8.375, 3.0 }, Math.PI / 2.0, 0.6, "Test", null); + worm.jump(0.01); + assertThrows(IllegalStateException.class, () -> worm.jump(0.01)); + } +} \ No newline at end of file