Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.92 KB | None | 0 0
  1. import info.gridworld.actor.Bug;
  2. import info.gridworld.grid.Location;
  3. /**
  4. * A ZBug traces out a Z pattern of a given size.
  5. */
  6. public class ZBug extends Bug
  7. {
  8. private int segmentLength; // the number of flowers in each segment
  9. private int steps; // the number of steps in the current side
  10. private int segment; // which segment of the Z the ZBug is on
  11. /**
  12. * Constructs a Zbug that traces a Z pattern in which each segment
  13. * of the Z has the given length
  14. * When the Z is drawn, the ZBug stops.
  15. * @param length the segment length
  16. */
  17. public ZBug(int length)
  18. {
  19. setDirection(Location.EAST);
  20. steps = 0;
  21. segment = 1;
  22. segmentLength = length;
  23. }
  24. public void act()
  25. {
  26. if (segment <= 3 && steps < segmentLength)
  27. {
  28. if (canMove())
  29. {
  30. move();
  31. steps++;
  32. }
  33. }
  34. else if (segment == 1)
  35. {
  36. setDirection(Location.SOUTHWEST);
  37. steps = 0;
  38. segment++;
  39. }
  40. else if (segment == 2)
  41. {
  42. setDirection(Location.EAST);
  43. steps = 0;
  44. segment++;
  45. }
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement