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;
- class RectangularMap implements IWorldMap {
- private final Vector2d lowerLeft;
- private final Vector2d upperRight;
- private List<Animal> animals = new LinkedList<Animal>();
- public RectangularMap(int width, int height){
- lowerLeft = new Vector2d(0, 0);
- upperRight = new Vector2d(width - 1, height - 1);
- }
- public boolean canMoveTo(Vector2d position){
- if(!position.precedes(upperRight) || !position.follows(lowerLeft)){
- return false;
- }
- if(isOccupied(position)){
- return false;
- }
- return true;
- }
- public boolean place(Animal animal){
- Vector2d animalPos = animal.getPosition();
- if(!isOccupied(animalPos) && animalPos.precedes(upperRight) && animalPos.follows(lowerLeft)){
- animals.add(animal);
- return true;
- }
- return false;
- }
- public boolean isOccupied(Vector2d position){
- for(Animal animal : animals){
- if(animal.getPosition().equals(position)){
- return true;
- }
- }
- return false;
- }
- public Object objectAt(Vector2d position) {
- for(Animal animal : animals){
- if(animal.getPosition().equals(position)){
- return animal;
- }
- }
- return null;
- }
- public String toString(){
- IWorldMap map = this;
- MapVisualizer visualizer = new MapVisualizer(map);
- return visualizer.draw(lowerLeft, upperRight);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement