import info.gridworld.actor.ActorWorld;
import info.gridworld.actor.Bug;
import info.gridworld.actor.Flower;
import info.gridworld.actor.Rock;
import info.gridworld.actor.Actor;
import java.awt.Color;
import java.util.*;
import info.gridworld.grid.Location;
import info.gridworld.grid.Grid;

 


/**
 * A Bug is an actor that can move and turn. It drops flowers as
 * it moves. 
* The implementation of this class is testable on the AP CS A and AB exams. */ public class Bug6 extends Actor { /** * Constructs a red bug. */ public Bug6() { setColor(Color.RED); } /** * Constructs a bug of a given color. * @param bugColor the color for this bug */ public Bug6(Color bugColor) { setColor(bugColor); } /** * Moves if it can move, turns otherwise. */ public void act() { if (canMove()) move(); else turn(); } /** * Turns the bug 45 degrees to the right without changing its location. */ public void turn() { setDirection(getDirection() + Location.HALF_RIGHT); } /** * Moves the bug forward, putting a flower into the location it previously * occupied. */ public void move() { Grid<Actor> gr = getGrid(); if (gr == null) return; Location loc = getLocation(); Location next = loc.getAdjacentLocation(getDirection()); if (gr.isValid(next)) moveTo(next); else removeSelfFromGrid(); Flower flower = new Flower(getColor()); flower.putSelfInGrid(gr, loc); } /** * Tests whether this bug can move forward into a location that is empty or * contains a flower. * @return true if this bug can move. */ public boolean canMove() { Grid<Actor> gr = getGrid(); if (gr == null) return false; Location loc = getLocation(); Location next = loc.getAdjacentLocation(getDirection()); if (!gr.isValid(next)) return false; Actor neighbor = gr.get(next); return (neighbor == null) || (neighbor instanceof Flower); // ok to move into empty location or onto flower // not ok to move onto any other actor } }