//  by Rick Reichenbach

// APSI Las Vegas Jun 2010


import info.gridworld.actor.*;

import info.gridworld.grid.*;


public class PacBug extends Bug

{


    /**

     * Constructs a box bug that traces a square of a given side length

     * @param length the side length

     */

    public PacBug()

    {


    }


    /**

     * Moves to the next location of the square.

     */

    public void act()

    {

        if (canMove())

        {

            move();

        }

        else

        {

            turn();

        }

    }

    /**

     * overrides the Bug class so that the Bug can screen wrap by removing

     * the check for gr.isValid(Next)

     */

    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 

        {

            int r = next.getRow(), c = next.getCol();

            if( c == -1 )

                c = gr.getNumCols() - 1;

            if( c == gr.getNumCols() )

                c = 0;

            if( r == -1 )

                r = gr.getNumRows() - 1;

            if( r == gr.getNumRows() )

                r = 0;

            next = new Location( r,c );

            moveTo(next);

        }

        Flower flower = new Flower(getColor());

        flower.putSelfInGrid(gr, loc);


    }


    public boolean canMove()

    {

        Grid<Actor> gr = getGrid();

        if (gr == null)

            return false;

        Location loc = getLocation();

        Location next = loc.getAdjacentLocation(getDirection());

        if (!gr.isValid(next))

        {

            int r = next.getRow(), c = next.getCol();

            if( c == -1 )

                c = gr.getNumCols() - 1;

            if( c == gr.getNumCols() )

                c = 0;

            if( r == -1 )

                r = gr.getNumRows() - 1;

            if( r == gr.getNumRows() )

                r = 0;

            next = new Location( r,c );

        }

        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


    }

}


*********************************************************************************


import info.gridworld.actor.ActorWorld;

import info.gridworld.grid.Location;

import java.awt.Color;


public class PacBugRunner

{

    public static void main(String[] args)

    {

        ActorWorld world = new ActorWorld();

        BoxBug alice = new BoxBug(6);

        alice.setColor(Color.ORANGE);

        BoxBug bob = new BoxBug(3);

        PacBug pinky = new PacBug();

        pinky.setColor( Color.BLUE );

        world.add(new Location(7, 8), alice);

        world.add(new Location(5, 5), bob);

        world.add(new Location( 3, 3 ), pinky );

        world.show();

    }

}