示例#1
1
  /**
   * Changes the positions of the worm as a result of a jump from the current position.
   *
   * @post If the worm jumped out of the map it will have been removed.
   *     |this.getWorld().getWorms().contains(this) == false
   * @post The worm jumped to the correct position | while (world.isPassable(tempXpos, tempYpos,
   *     this.getRadius())) | tempXpos = this.jumpStep(t)[0] | tempYpos = this.jumpStep(t)[1] | t +=
   *     timeStep | if ((world.isAdjacent(tempXpos, tempYpos, this.getRadius())) && |
   *     (Math.sqrt(Math.pow((origXpos-tempXpos), 2)+Math.pow((origYpos-tempYpos),
   *     2))>=this.getRadius() )) | new.getXpos() == tempXpos | new.getYpos() == tempYpos
   * @post The worm's actionpoints are reduced to zero. |new.getActionPoints() == 0;
   * @throws IllegalStateException When the worm has no action point left to jump the exception is
   *     thrown. |! canJump()
   */
  @Raw
  public void jump(Double timeStep) throws IllegalStateException {
    if (this.canJump()) {
      World world = this.getWorld();
      double origXpos = this.getXpos();
      double origYpos = this.getYpos();
      double tempXpos = this.getXpos();
      double tempYpos = this.getYpos();
      double t = 0;
      while (world.isPassable(tempXpos, tempYpos, this.getRadius())) {
        tempXpos = this.jumpStep(t)[0];
        tempYpos = this.jumpStep(t)[1];
        t += timeStep;

        if (isOutOfTheMap(tempXpos, tempYpos)) {
          this.killWorm();
          break;
        }
        if ((world.isAdjacent(tempXpos, tempYpos, this.getRadius()))
            && (Math.sqrt(Math.pow((origXpos - tempXpos), 2) + Math.pow((origYpos - tempYpos), 2))
                >= this.getRadius())) {
          this.setXpos(tempXpos);
          this.setYpos(tempYpos);
          this.consumeFood();
          this.setActionPoints(0);
          break;
        }
      }
    } else throw new IllegalStateException();
  }
示例#2
0
  /**
   * Returns the time it takes to worm to jump (to his new position).
   *
   * @throws IllegalStateException If the worm can't jump the exception is thrown. | ! canJump()
   */
  @Raw
  public double jumpTime(double timeStep) throws IllegalStateException {
    World world = this.getWorld();
    double origXpos = this.getXpos();
    double origYpos = this.getYpos();
    double tempXpos = this.getXpos();
    double tempYpos = this.getYpos();
    double t = 0;
    if (this.canJump()) {
      while (world.isPassable(tempXpos, tempYpos, this.getRadius())) {
        tempXpos = this.jumpStep(t)[0];
        tempYpos = this.jumpStep(t)[1];
        t += timeStep;

        if ((world.isAdjacent(tempXpos, tempYpos, this.getRadius()))
            && (Math.sqrt(Math.pow((origXpos - tempXpos), 2) + Math.pow((origYpos - tempYpos), 2))
                >= this.getRadius())) {
          return t;
        }
        if (isOutOfTheMap(tempXpos, tempYpos)) {
          return t;
        }
      }
      return t;
    } else throw new IllegalStateException();
  }
  /**
   * Changes the positions of the projectile as a result of a jump from the current position.
   *
   * @post If the projectile hits a worm this worm loses some hit points and the projectile is
   *     removed from the world and set non active. | if ((Math.sqrt(Math.pow(w.getXpos()-tempXpos,
   *     2)+Math.pow(w.getYpos()-tempYpos, 2))< maxDistance)) | while (this.getActive == true) |
   *     then (new.setHitPoints(worm.getHitPoints-this.getDamage()) | then (world.getProjectile() ==
   *     null) | then (new.getActive == false))
   * @post If the projectile doesn't hit a worm, the projectile will be deleted when it leaves the
   *     map or when it hits impassable terrain. | if !((Math.sqrt(Math.pow(w.getXpos()-tempXpos,
   *     2)+Math.pow(w.getYpos()-tempYpos, 2))< maxDistance)) | then ( if
   *     ((isOutOfTheMap(tempXpos,tempYpos))) | then ((world.getProjectile() == null) | then
   *     (new.getActive == false)) | (else if (world.isImpassable(tempXpos, tempYpos,
   *     this.getRadius()))) | then ((world.getProjectile() == null) | then (new.getActive ==
   *     false)))
   * @throws IllegalStateException If the projectile can't jump the exception is thrown. |!
   *     canJump()
   */
  @Raw
  public void jump(Double timeStep) throws IllegalStateException {
    if (this.canJump()) {
      World world = this.getWorld();
      double tempXpos = this.getXpos();
      double tempYpos = this.getYpos();
      double t = 0;
      while ((world.isPassable(tempXpos, tempYpos, this.getRadius()))) {

        tempXpos = this.jumpStep(t)[0];
        tempYpos = this.jumpStep(t)[1];

        Collection<Worm> collection = (world.getWorms());

        for (Worm w : collection) {
          Worm overlappingWorm = null;
          double maxDistance = this.getRadius() + w.getRadius();

          if (!(w == world.getCurrentWorm())
              && (Math.sqrt(
                      Math.pow(w.getXpos() - tempXpos, 2) + Math.pow(w.getYpos() - tempYpos, 2))
                  < maxDistance)) {

            overlappingWorm = w;
            while ((this.getActive() == true)) {
              overlappingWorm.setHitPoints(overlappingWorm.getHitPoints() - this.getDamage());
              this.deleteProjectile(world);
              this.setActive(false);
            }

          } else {
            overlappingWorm = null;
            if ((isOutOfTheMap(tempXpos, tempYpos))) {
              this.deleteProjectile(world);
              this.setActive(false);
            } else if (((world.isImpassable(tempXpos, tempYpos, this.getRadius())))
                && (this.getActive() == true)) {
              this.deleteProjectile(world);
              this.setActive(false);
            }
          }
        }
        t += timeStep;
      }
    } else {
      throw new IllegalStateException();
    }
  }
示例#4
0
 /** This method return the food objects that overlap with the worms position. */
 @Raw
 public Food overlappingFood() {
   World world = this.getWorld();
   double maxDistance = this.getRadius() + 0.2;
   Food overlappingFood = null;
   Collection<Food> collection = (world.getFood());
   for (Food f : collection) {
     if (Math.sqrt(
             Math.pow(f.getXpos() - this.getXpos(), 2) + Math.pow(f.getYpos() - this.getYpos(), 2))
         < maxDistance) {
       overlappingFood = f;
       break;
     } else {
       overlappingFood = null;
     }
   }
   return overlappingFood;
 }
示例#5
0
  /**
   * The method makes the worm move to a next position that is adjacent to impassable terrain
   * following the slope of that terrain in the direction. The worm shall aim to maximize the
   * distance while minimizing the divergence. If no such location exists because all locations in
   * the direction +- 0,7875 are impassable the worm shall remain at its current position. If
   * locations in the direction are passable but not adjacent the worm shall move there and then
   * drop passively.
   *
   * @post If the worm can maximize the distance while minimizing the divergence, the worm has moved
   *     to the optimal location. |for (double a = 0.1;a<=this.getRadius();a=a+(0.01*a)) { | x2 =
   *     x+Math.cos(direction)*a; | y2 = y+Math.sin(direction)*a; | if (world.isAdjacent(x2, y2,
   *     this.getRadius()) && | world.isPassable(x2, y2, this.getRadius())) { | double d =
   *     Math.sqrt(Math.pow((x-x2),2)+Math.pow((y-y2),2)); | double s = Math.atan((x-x2)/(y-y2)); |
   *     if ((d>=maxD) && (s<minS)) { | minS=s; | maxD=d; | x2Max = x2; | y2Max= y2; | direction =
   *     direction +0.0175; |new.getXpos()==x2Max |new.getYpos() == y2Max
   * @post If the worm can't maximize the distance while minimizing the divergence and there is only
   *     impassable terrain in the checked directions, the worm will not have moved. |new.getXpos()
   *     == old.getXpos() |new.getYpos() == old.getYpos()
   * @post If the worm can't maximize the distance while minimizing the divergence and there is only
   *     passable terrain in the checked directions that is not adjacent, the worm will move there.
   *     |new.getXpos() == old.getXpos() + cos(direction)*radius |new.getYpos() == old.getYpos() +
   *     sin(direction)*radius
   * @post The worms actionpoints are correctly reduced. |new.getActionPoints ==
   *     old.getActionPoints() - old.computeCost2(old.getXpos(),old.getYpos())
   * @throws IllegalArgumentException If the worm can't move because he has insufficient
   *     actionpoints the exception is thrown. | ! isValidStep()
   * @throws IllegalStateException If the worm can't move because the worm isn't positioned in
   *     passable terrain and adjacent to impassable terrain the exception is thrown. | ! canMove()
   */
  @Raw
  public void move() throws IllegalArgumentException, IllegalStateException {
    if (!isValidStep()) throw new IllegalArgumentException();
    if (canMove()) {
      World world = this.getWorld();
      double x = this.getXpos();
      double y = this.getYpos();
      double prevx = x;
      double prevy = y;
      double x2 = x;
      double y2 = y;
      double x2Max = x2;
      double y2Max = y2;
      double c = -0.7875;
      double direction = this.getDirection() + c;

      double maxD = 0;
      double minS = this.getDirection();

      // geval 1: na gaan of in direction+-45° er een gischike volgende positie is
      //			en zo ja, ernaar verplaatsen.
      for (double a = 0.1; a <= this.getRadius(); a = a + (0.01 * a)) {
        x2 = x + Math.cos(direction) * a;
        y2 = y + Math.sin(direction) * a;
        if (world.isAdjacent(x2, y2, this.getRadius())
            && world.isPassable(x2, y2, this.getRadius())) {
          double d = Math.sqrt(Math.pow((x - x2), 2) + Math.pow((y - y2), 2));
          double s = Math.atan((x - x2) / (y - y2));
          if ((d >= maxD) && (s < minS)) {
            minS = s;
            maxD = d;
            x2Max = x2;
            y2Max = y2;
          }
        }
        direction = direction + 0.0175;
      }
      if (this.isOutOfTheMap(x2Max, y2Max)) {
        this.killWorm();
      } else {
        this.setXpos(x2Max);
        this.setYpos(y2Max);
        this.setActionPoints(this.getActionPoints() - this.computeCost2(prevx, prevy));
      }

      // geval2: Er werd in direction+-45° geen geschikte plaats gevonden
      //			nagaan of er in direction naar een passable locatie kan verplaatst worden,
      //			daarnaar verplaatsen en dan vallen (fall).
      if ((x2Max == x) && (y2Max == y)) {
        double pasXpos = x;
        double pasYpos = y;

        pasXpos = (x + (Math.cos(this.getDirection()) * this.getRadius()));
        pasYpos = (y + (Math.sin(this.getDirection()) * this.getRadius()));
        if (!world.isAdjacent(pasXpos, pasYpos, this.getRadius())
            && world.isPassable(pasXpos, pasYpos, this.getRadius())) {
          if (this.isOutOfTheMap(pasXpos, pasYpos)) {
            this.killWorm();
          } else {
            this.setXpos(pasXpos);
            this.setYpos(pasYpos);
            this.setActionPoints(this.getActionPoints() - this.computeCost2(prevx, prevy));
          }
        }
      }
      this.consumeFood();
    } else throw new IllegalStateException();
  }
示例#6
0
 /** @return ... | result == Math.sqrt(Math.pow(getXComponent(),2)+Math.pow(getYComponent(),2)) */
 public double getTotalLength() {
   return Math.sqrt(Math.pow(xComp, 2) + Math.pow(yComp, 2));
 }