Exemplo n.º 1
0
 /**
  * Check whether this world has proper food rations attached to it.
  *
  * @return | result == | for each food in Food | ( if (this.hasAsFood(food)) | then
  *     canHaveAsFood(food) | && (food.getWorld() == this) )
  */
 @Raw
 public boolean hasProperFoodRations() {
   for (Food food : this.foodRations) {
     if (!canHaveAsFood(food) || food.getWorld() != this) return false;
   }
   return true;
 }
Exemplo n.º 2
0
 /**
  * returns the food object if it in within the given radius on the given position.
  *
  * @param x the given x-position
  * @param y the given y-position
  * @param radius the given radius
  * @return |if(isImpassable(x,y,radius)) | return null; |for(Food food: foodRations){ |
  *     if(Math.pow(Math.pow((food.getX() - x), 2.0) + | Math.pow((food.getY() - y),
  *     2.0),(1.0/2.0)) <= | (Food.getRadius() + radius)){ | return food; | } | } |return null;
  */
 public Food hitAnyFood(double x, double y, double radius) {
   if (isImpassable(x, y, radius)) return null;
   for (Food food : foodRations) {
     if (Math.pow(Math.pow((food.getX() - x), 2.0) + Math.pow((food.getY() - y), 2.0), (1.0 / 2.0))
         <= (Food.getRadius() + radius)) {
       return food;
     }
   }
   return null;
 }
Exemplo n.º 3
0
 /**
  * This method makes the worm eat food.
  *
  * @post When the food gets eaten the radius of the worm will increas with 10%. | new.getRadius()
  *     == old.getRadius() * 1.1
  * @post When the food gets eaten the worm will be placed to the nearest adjecent location. |
  *     world.isAdjacent(xpos, ypos , radius) == true
  * @post When the food gets eaten this food object will de removed from the world. | (!
  *     (this.getWorld()).hasAsObject(food))
  */
 @Raw
 public void consumeFood() {
   double xpos = this.getXpos();
   double ypos = this.getYpos();
   Food food = this.overlappingFood();
   if (!(food == null)) {
     this.setRadius(this.getRadius() * 1.1);
     this.setNearestAdjacent(xpos, ypos);
     food.unsetWorld();
   }
 }
Exemplo n.º 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;
 }
Exemplo n.º 5
0
 /**
  * Deactivate this world.
  *
  * @post | ( !new.isActive() )
  * @post | for each food in Food | ( if (this.hasAsFood(food)) | then ( !(new food).isActive() ) )
  * @post | for each food in Food | (!this.hasAsFood(food))
  */
 public void deactivate() {
   for (Food food : this.foodRations) {
     food.deactivate();
   }
   this.isActive = false;
 }
Exemplo n.º 6
0
 /**
  * Remove the given food from the set of food rations attached to this world.
  *
  * @param food The food to be removed
  * @post | !new.hasAsFood(food)
  * @post | if (hasAsFood(food)) | ((new food).getWorld() == null)
  * @throws IllegalArgumentException() | !hasAsFood(food)
  */
 public void removeAsFood(Food food) throws IllegalArgumentException {
   if (!hasAsFood(food)) throw new IllegalArgumentException();
   this.foodRations.remove(food);
   food.setWorld(null);
 }
Exemplo n.º 7
0
 /**
  * Add the given food to the set of food rations attached to this world.
  *
  * @param food The food to be added.
  * @post | new.hasAsFood(food)
  * @post | (new food).getWorld() == this
  * @throws IllegalArgumentException("You can't add this food.") | !canHaveAsFood(food)
  * @throws IllegalArgumentException("You can't add this food.") | ( (food != null) | &&
  *     (food.getWorld() != null) )
  */
 public void addAsFood(Food food) throws IllegalArgumentException {
   if (!canHaveAsFood(food)) throw new IllegalArgumentException("You can't add this food.");
   if (food.getWorld() != null) throw new IllegalArgumentException("You can't add this food.");
   this.foodRations.add(food);
   food.setWorld(this);
 }
Exemplo n.º 8
0
 /**
  * Check whether this world can have the given food as one of its food rations.
  *
  * @param food The food to check.
  * @return | if (food == null) | then result == false | else result == | ( food.isActive() &&
  *     this.isActive() )
  */
 @Raw
 public boolean canHaveAsFood(Food food) {
   return food != null && food.isActive() && this.isActive();
 }