Advertisement
fiveriverflow

CarefulBug

Nov 29th, 2015
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.93 KB | None | 0 0
  1. import info.gridworld.actor.Bug;
  2. import info.gridworld.grid.Location;
  3.  
  4. public class CarefulBug extends Bug {
  5.     private int steps;
  6.     private int sideLength;
  7.  
  8.     /**
  9.      * Constructs a box bug that traces a square of a given side length
  10.      * @param length the side length
  11.      */
  12.     public CarefulBug(int length)
  13.     {
  14.         steps = 0;
  15.         sideLength = length;
  16.     }
  17.  
  18.     /**
  19.      * Moves to the next location of the square.
  20.      */
  21.     public void act()
  22.     {
  23.         if (steps < sideLength && canMove())
  24.         {
  25.             move();
  26.             steps++;
  27.         }
  28.         else
  29.         {
  30.             turn();
  31.             turn();
  32.             steps = 0;
  33.         }
  34.     }
  35.    
  36.     /**
  37.      * Reset Step count
  38.      */
  39.     public void resetCount() {
  40.         steps = 0;
  41.     }
  42.    
  43.     /**
  44.      * Turns the bug left
  45.      */
  46.     public void turnLeft() {
  47.         setDirection(getDirection() - 45);
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement