Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package agh.ics.oop;
- import java.util.LinkedList;
- import java.util.List;
- import static java.util.concurrent.ThreadLocalRandom.current;
- public class GrassField implements IWorldMap{
- private List<Animal> animals = new LinkedList<Animal>();
- private List<Grass> grasses = new LinkedList<>();
- private Vector2d lowerLeft;
- private Vector2d upperRight;
- public GrassField(int grassCount){
- boolean taken = false;
- boolean placed = false;
- int grassBound = (int)Math.round(Math.sqrt(grassCount * 10));
- Vector2d position;
- for(int i = 0; i < grassCount; i++){
- while(!placed){
- position = new Vector2d(current().nextInt(0, grassBound + 1), current().nextInt(0, grassBound + 1));
- for(Grass grass:grasses){
- if(grass.getPosition().equals(position)){
- taken = true;
- break;
- }
- taken = false;
- }
- if(!taken){
- grasses.add(new Grass(position));
- taken = false;
- placed = true;
- }
- }
- placed = false;
- }
- }
- public boolean canMoveTo(Vector2d position){
- for(Animal animal : animals){
- if(animal.getPosition().equals(position)){
- return false;
- }
- }
- return true;
- }
- public boolean place(Animal animal){
- Vector2d animalPos = animal.getPosition();
- if(canMoveTo(animalPos)){
- animals.add(animal);
- return true;
- }
- return false;
- }
- public boolean isOccupied(Vector2d position){
- for(Animal animal : animals){
- if(animal.getPosition().equals(position)){
- return true;
- }
- }
- for(Grass grass : grasses){
- if(grass.getPosition().equals(position)){
- return true;
- }
- }
- return false;
- }
- public Object objectAt(Vector2d position) {
- for(Animal animal : animals){
- if(animal.getPosition().equals(position)){
- return animal;
- }
- }
- for(Grass grass : grasses){
- if(grass.getPosition().equals(position)){
- return grass;
- }
- }
- return null;
- }
- public String toString(){
- IWorldMap map = this;
- MapVisualizer visualizer = new MapVisualizer(map);
- setBounds();
- return visualizer.draw(lowerLeft, upperRight);
- }
- private void setBounds(){
- lowerLeft = new Vector2d(Integer.MAX_VALUE, Integer.MAX_VALUE);
- upperRight = new Vector2d(Integer.MIN_VALUE, Integer.MIN_VALUE);
- for(Animal animal: animals){
- lowerLeft = lowerLeft.lowerLeft(animal.getPosition());
- upperRight = upperRight.upperRight(animal.getPosition());
- }
- for(Grass grass: grasses){
- lowerLeft = lowerLeft.lowerLeft(grass.getPosition());
- upperRight = upperRight.upperRight(grass.getPosition());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement