Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. //MyBug.java
  2. /*
  3. * MyBug extends Bug, so a MyBug is a Bug.
  4. * Bug extends Actor, so a MyBug is an Actor also.
  5. * Note: Rock and Flower classes extend the Actor class too,
  6. * which is why they can exist all on the same grid of Actor type.
  7. *
  8. * Other than the constructor names the code below is the same as in the
  9. * Bug class itself. Modify the code to change the way your bug behaves.
  10. *
  11. */
  12. import java.awt.Color;
  13.  
  14. import info.gridworld.grid.Grid;
  15. import info.gridworld.grid.Location;
  16. import info.gridworld.actor.*;
  17.  
  18. public class MyBug extends Bug
  19. {
  20. public MyBug()
  21. {
  22.  
  23. setColor(Color.BLUE);
  24. this.setDirection(Location.NORTHWEST);
  25.  
  26.  
  27. }
  28.  
  29. /**
  30. * Constructs a DiagonalBug of a given color.
  31. * @param bugColor the color for this bug
  32. */
  33. public MyBug(Color bugColor)
  34. {
  35. setColor(bugColor);
  36. this.setDirection(Location.NORTHWEST);
  37.  
  38. }
  39.  
  40. /**
  41. * Each time through the grid, each Actor object (Bug) is told to act().
  42. * If the MyBug can move then it moves or else it turns.
  43. */
  44. public void act()
  45. {
  46. if (canMove())
  47. move();
  48. else
  49. turn();
  50. }
  51.  
  52. /**
  53. * Turns the bug 45 degrees to the right without changing its location.
  54. */
  55. public void turn()
  56. {
  57. setDirection(getDirection() + Location.HALF_RIGHT);
  58. }
  59.  
  60. /**
  61. * Moves the bug forward, putting a flower into the location it previously occupied.
  62. */
  63. public void move()
  64. {
  65. Grid<Actor> gr = getGrid();
  66. if (gr == null)
  67. return;
  68. Location loc = getLocation();
  69. Location next = loc.getAdjacentLocation(getDirection());
  70. if (gr.isValid(next))
  71. moveTo(next);
  72. else
  73. removeSelfFromGrid();
  74.  
  75. Flower flower = new Flower(getColor());
  76. flower.putSelfInGrid(gr, loc);
  77. }
  78. /**
  79. * Tests whether this bug can move forward into a location that is empty or contains a flower.
  80. * @return true if this bug can move.
  81. */
  82. public boolean canMove()
  83. {
  84. Grid<Actor> gr = getGrid();
  85. if (gr == null)
  86. return false;
  87. Location loc = getLocation();
  88. Location next = loc.getAdjacentLocation(getDirection());
  89. if (!gr.isValid(next))
  90. return false;
  91. Actor neighbor = gr.get(next);
  92. if ((neighbor instanceof Rock))
  93. {
  94. neighbor.removeSelfFromGrid();
  95. return false;
  96. }
  97. return (neighbor == null) || (neighbor instanceof Flower);
  98. // Ok to move into empty location or onto flower
  99. // but not Ok to move onto any other actor
  100.  
  101. public void breed()
  102. {
  103. Grid<Actor> gr = getGrid();
  104. if (gr == null)
  105. return;
  106.  
  107. Location loc = getLocation();
  108. Location next = loc.getAdjacentLocation(loc.EAST);
  109. if (!gr.usValid(next))
  110. {
  111. next = loc.getAdjacentLocation(loc.WEST);
  112. if (!gr.isValid(next))
  113. return;
  114. }
  115.  
  116. Actor neighbor = gr.get(next);
  117. if (neighbor == null || neighbor instanceof Flower || neighbor instanceof Rock)
  118. {
  119. MyBug babyBuggy = new MyBig();
  120. babyBuggy.putSelfInGrid(gr, next);
  121. }
  122. }
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement