small changes

This commit is contained in:
2018-05-25 05:34:51 +02:00
parent cc277a045f
commit 80d6ce748c
5 changed files with 107 additions and 137 deletions

View File

@@ -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);
}
}
}

View File

@@ -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();
}

View File

@@ -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,11 +672,16 @@ public class Worm extends GameObject implements IJumpable{
maxLocDirection = minDirection;
}
}
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)) {
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;
return maxLocDirection;

View File

@@ -63,7 +63,7 @@ public class Coordinate extends Tuple<Double, Double> {
* |result == new double[]{getX(), getY()}
*/
public double[] toArray() {
return new double[]{getX(), getY()};
return new double[]{this.item1, this.item2};
}
/**

View File

@@ -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());
// }
//}
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));
}
}