Advertisement
matbiz01

RectangularMap

Nov 17th, 2021
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. package agh.ics.oop;
  2.  
  3. import java.util.LinkedList;
  4. import java.util.List;
  5.  
  6.  
  7.  
  8. class RectangularMap implements IWorldMap {
  9. private final Vector2d lowerLeft;
  10. private final Vector2d upperRight;
  11. private List<Animal> animals = new LinkedList<Animal>();
  12. public RectangularMap(int width, int height){
  13. lowerLeft = new Vector2d(0, 0);
  14. upperRight = new Vector2d(width - 1, height - 1);
  15. }
  16.  
  17. public boolean canMoveTo(Vector2d position){
  18. if(!position.precedes(upperRight) || !position.follows(lowerLeft)){
  19. return false;
  20. }
  21.  
  22. if(isOccupied(position)){
  23. return false;
  24. }
  25.  
  26. return true;
  27. }
  28.  
  29. public boolean place(Animal animal){
  30. Vector2d animalPos = animal.getPosition();
  31. if(!isOccupied(animalPos) && animalPos.precedes(upperRight) && animalPos.follows(lowerLeft)){
  32. animals.add(animal);
  33. return true;
  34. }
  35. return false;
  36. }
  37.  
  38. public boolean isOccupied(Vector2d position){
  39. for(Animal animal : animals){
  40. if(animal.getPosition().equals(position)){
  41. return true;
  42. }
  43. }
  44. return false;
  45. }
  46.  
  47. public Object objectAt(Vector2d position) {
  48. for(Animal animal : animals){
  49. if(animal.getPosition().equals(position)){
  50. return animal;
  51. }
  52. }
  53. return null;
  54. }
  55.  
  56. public String toString(){
  57. IWorldMap map = this;
  58. MapVisualizer visualizer = new MapVisualizer(map);
  59. return visualizer.draw(lowerLeft, upperRight);
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement