Advertisement
matbiz01

grassField

Nov 17th, 2021
826
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.20 KB | None | 0 0
  1. package agh.ics.oop;
  2.  
  3. import java.util.LinkedList;
  4. import java.util.List;
  5.  
  6. import static java.util.concurrent.ThreadLocalRandom.current;
  7.  
  8. public class GrassField implements IWorldMap{
  9.     private List<Animal> animals = new LinkedList<Animal>();
  10.     private List<Grass> grasses = new LinkedList<>();
  11.     private Vector2d lowerLeft;
  12.     private Vector2d upperRight;
  13.     public GrassField(int grassCount){
  14.         boolean taken = false;
  15.         boolean placed = false;
  16.         int grassBound = (int)Math.round(Math.sqrt(grassCount * 10));
  17.         Vector2d position;
  18.         for(int i = 0; i < grassCount; i++){
  19.             while(!placed){
  20.                 position = new Vector2d(current().nextInt(0, grassBound + 1), current().nextInt(0, grassBound + 1));
  21.                 for(Grass grass:grasses){
  22.                     if(grass.getPosition().equals(position)){
  23.                         taken = true;
  24.                         break;
  25.                     }
  26.                     taken = false;
  27.                 }
  28.                 if(!taken){
  29.                     grasses.add(new Grass(position));
  30.                     taken = false;
  31.                     placed = true;
  32.                 }
  33.             }
  34.             placed = false;
  35.         }
  36.     }
  37.  
  38.     public boolean canMoveTo(Vector2d position){
  39.         for(Animal animal : animals){
  40.             if(animal.getPosition().equals(position)){
  41.                 return false;
  42.             }
  43.         }
  44.         return true;
  45.     }
  46.  
  47.     public boolean place(Animal animal){
  48.         Vector2d animalPos = animal.getPosition();
  49.         if(canMoveTo(animalPos)){
  50.             animals.add(animal);
  51.             return true;
  52.         }
  53.         return false;
  54.     }
  55.  
  56.     public boolean isOccupied(Vector2d position){
  57.         for(Animal animal : animals){
  58.             if(animal.getPosition().equals(position)){
  59.                 return true;
  60.             }
  61.         }
  62.  
  63.         for(Grass grass : grasses){
  64.             if(grass.getPosition().equals(position)){
  65.                 return true;
  66.             }
  67.         }
  68.         return false;
  69.     }
  70.  
  71.     public Object objectAt(Vector2d position) {
  72.         for(Animal animal : animals){
  73.             if(animal.getPosition().equals(position)){
  74.                 return animal;
  75.             }
  76.         }
  77.  
  78.         for(Grass grass : grasses){
  79.             if(grass.getPosition().equals(position)){
  80.                 return grass;
  81.             }
  82.         }
  83.         return null;
  84.     }
  85.  
  86.     public String toString(){
  87.         IWorldMap map = this;
  88.         MapVisualizer visualizer = new MapVisualizer(map);
  89.         setBounds();
  90.         return visualizer.draw(lowerLeft, upperRight);
  91.     }
  92.  
  93.     private void setBounds(){
  94.         lowerLeft = new Vector2d(Integer.MAX_VALUE, Integer.MAX_VALUE);
  95.         upperRight = new Vector2d(Integer.MIN_VALUE, Integer.MIN_VALUE);
  96.  
  97.         for(Animal animal: animals){
  98.             lowerLeft = lowerLeft.lowerLeft(animal.getPosition());
  99.             upperRight = upperRight.upperRight(animal.getPosition());
  100.         }
  101.  
  102.         for(Grass grass: grasses){
  103.             lowerLeft = lowerLeft.lowerLeft(grass.getPosition());
  104.             upperRight = upperRight.upperRight(grass.getPosition());
  105.         }
  106.     }
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement