diff --git a/pom.xml b/pom.xml index 17b0c5621f60413befa5279a881a82f61a8cc907..26c4cb5f79823e67e45fd58161c1bfa291480336 100644 --- a/pom.xml +++ b/pom.xml @@ -1,18 +1,20 @@ - - 4.0.0 - fr.ufrst.m1info.gl.tparineymates - robot-modules - pom - 1.0-SNAPSHOT - robot-modules - http://maven.apache.org - - - junit - junit - 3.8.1 - test - - + + 4.0.0 + fr.ufrst.m1info.gl.tparineymates + robot-modules + pom + 1.0-SNAPSHOT + robot-modules + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + + robot + diff --git a/robot/pom.xml b/robot/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..7fcd127d7defa64d073315a82cd40f72138a5f96 --- /dev/null +++ b/robot/pom.xml @@ -0,0 +1,78 @@ + + + 4.0.0 + + robot-modules + fr.ufrst.m1info.gl.tparineymates + 1.0-SNAPSHOT + + + fr.ufrst.m1info.gl.tparineymates + robot + 1.0-SNAPSHOT + + robot + + http://www.example.com + + + UTF-8 + 1.7 + 1.7 + + + + + junit + junit + 4.11 + test + + + + + + + + + maven-clean-plugin + 3.1.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.8.0 + + + maven-surefire-plugin + 2.22.1 + + + maven-jar-plugin + 3.0.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + maven-site-plugin + 3.7.1 + + + maven-project-info-reports-plugin + 3.0.0 + + + + + diff --git a/robot/src/main/java/fr/ufrst/m1info/gl/tparineymates/LandSensor.java b/robot/src/main/java/fr/ufrst/m1info/gl/tparineymates/LandSensor.java new file mode 100644 index 0000000000000000000000000000000000000000..1d9626f70e07f7659506e6a8e401836f14ff9e2d --- /dev/null +++ b/robot/src/main/java/fr/ufrst/m1info/gl/tparineymates/LandSensor.java @@ -0,0 +1,21 @@ +package fr.ufrst.m1info.gl.tparineymates; + +import java.util.Random; + +public class LandSensor { + + private Random random; + + public LandSensor(Random random) { + this.random = random; + } + + public double getPointToPointEnergyCoefficient(Coordinates coordinate1, Coordinates coordinate2) { + double distance = distance(coordinate1, coordinate2); + return 1 + distance / (distance * random.nextDouble()); + } + + public static double distance(Coordinates coordinate1, Coordinates coordinate2) { + return Math.sqrt(Math.pow(coordinate1.getX()-coordinate2.getX(), 2) + Math.pow(coordinate1.getY()-coordinate2.getY(),2)); + } +} diff --git a/robot/src/main/java/fr/ufrst/m1info/gl/tparineymates/Robot.java b/robot/src/main/java/fr/ufrst/m1info/gl/tparineymates/Robot.java new file mode 100644 index 0000000000000000000000000000000000000000..af48e60470d7fbf6de86b81a095a1772c012e8ca --- /dev/null +++ b/robot/src/main/java/fr/ufrst/m1info/gl/tparineymates/Robot.java @@ -0,0 +1,115 @@ +package fr.ufrst.m1info.gl.tparineymates; + +import java.util.ArrayList; + +import static fr.ufrst.m1info.gl.mates.RoadBookCalculator.calculateRoadBook; + +public class Robot { + + private Coordinates position; + private Direction direction; + private boolean isLanded; + private RoadBook roadBook; + private final double energyConsumption; // energie consommée pour la réalisation d'une action dans les conditions + // idéales + private LandSensor landSensor; + private Battery cells; // cells + + public Robot() { + this(1.0, new Battery()); + } + + public Robot(double energyConsumption, Battery cells) { + isLanded = false; + this.energyConsumption = energyConsumption; + this.cells = cells; + } + + public void land(Coordinates landPosition, LandSensor sensor) { + position = landPosition; + direction = Direction.NORTH; + isLanded = true; + landSensor = sensor; + cells.setUp(); + } + + public int getXposition() throws UnlandedRobotException { + if (!isLanded) + throw new UnlandedRobotException(); + return position.getX(); + } + + public int getYposition() throws UnlandedRobotException { + if (!isLanded) + throw new UnlandedRobotException(); + return position.getY(); + } + + public Direction getDirection() throws UnlandedRobotException { + if (!isLanded) + throw new UnlandedRobotException(); + return direction; + } + + public void moveForward() throws UnlandedRobotException, InterruptedException { + if (!isLanded) + throw new UnlandedRobotException(); + Coordinates nextPosition = MapTools.nextForwardPosition(position, direction); + double neededEnergy = landSensor.getPointToPointEnergyCoefficient(position, nextPosition) * energyConsumption; + boolean move = false; + do { + try { + cells.use(neededEnergy); + move = true; + } catch (InsufficientChargeException e) { + long l = cells.timeToSufficientCharge(neededEnergy); + Thread.sleep(l); + } + } while (!move); + position = nextPosition; + } + + public void moveBackward() throws UnlandedRobotException { + if (!isLanded) + throw new UnlandedRobotException(); + position = MapTools.nextBackwardPosition(position, direction); + } + + public void turnLeft() throws UnlandedRobotException { + if (!isLanded) + throw new UnlandedRobotException(); + direction = MapTools.counterclockwise(direction); + } + + public void turnRight() throws UnlandedRobotException { + if (!isLanded) + throw new UnlandedRobotException(); + direction = MapTools.clockwise(direction); + } + + public void setRoadBook(RoadBook roadBook) { + this.roadBook = roadBook; + } + + public void letsGo() throws UnlandedRobotException, UndefinedRoadbookException, InterruptedException { + if (roadBook == null) + throw new UndefinedRoadbookException(); + while (roadBook.hasInstruction()) { + Instruction nextInstruction = roadBook.next(); + if (nextInstruction == Instruction.FORWARD) + moveForward(); + else if (nextInstruction == Instruction.BACKWARD) + moveBackward(); + else if (nextInstruction == Instruction.TURNLEFT) + turnLeft(); + else if (nextInstruction == Instruction.TURNRIGHT) + turnRight(); + } + } + + public void computeRoadTo(Coordinates destination) throws UnlandedRobotException { + if (!isLanded) + throw new UnlandedRobotException(); + setRoadBook(calculateRoadBook(direction, position, destination, new ArrayList())); + } +} diff --git a/robot/src/main/java/fr/ufrst/m1info/gl/tparineymates/UnlandedRobotException.java b/robot/src/main/java/fr/ufrst/m1info/gl/tparineymates/UnlandedRobotException.java new file mode 100644 index 0000000000000000000000000000000000000000..0cf5635171d7293832201ff5a6dfc9787a8e7ffc --- /dev/null +++ b/robot/src/main/java/fr/ufrst/m1info/gl/tparineymates/UnlandedRobotException.java @@ -0,0 +1,16 @@ +package fr.ufrst.m1info.gl.tparineymates; + + + + +public class UnlandedRobotException extends Exception { + + /** + * + */ + private static final long serialVersionUID = 8808058592281751853L; + + public UnlandedRobotException() { + super("Le robot doit être posé avant tout déplacement"); + } +}