Bee

<< Dahlia | APQuestionsTrailIndex | WordScrambler >>

Bee

If you don't have the question handy, you can download the pdf document BeeQuestion.pdf

I made some 48 x 48 greyscale gifs so you can see the Bee and Clover when you run your code. "Bee" sure to place it in the same folder as your bytecode (the folder that has Bee.class after you compile)

Bee.gif

Clover.gif

The Bee starter code:

import info.gridworld.grid.*; 
import info.gridworld.actor.*; 
import java.awt.Color; 
import java.util.ArrayList;
public class Bee extends Critter
{
  // creates a yellow bee
  public Bee()
  {
    setColor(Color.YELLOW);
  }

  // processes the actors; pollinates all Clover objects
  // among them
  public void processActors(ArrayList<Actor> actors)
  {  /* to be implemented in part (a) */  



  }

  // returns the location chosen among locs and the current
  // location that is nearest to an unpollinated clover;
  // if several locations are at the same distance
  // from an unpollinated clover, returns any one of them;
  // if the grid does not have any unpollinated clovers,
  // returns the current location

  public Location selectMoveLocation(ArrayList<Location> locs)
  {  /* to be implemented in part (b) */  


    }
    return next;
  }


  // turns towards loc, then moves into loc like a
  // regular Critter
  public void makeMove(Location loc)
  {  /* to be implemented in part (c) */ 


    }

  // returns the distance between loc1 and loc2
  // precondition: loc1 != null and loc2 != null
  private int distance(Location loc1, Location loc2)
  { 
    int x=Math.abs(loc2.getCol()-loc1.getCol());
    int y=Math.abs(loc2.getRow()-loc1.getRow());
    double d=Math.sqrt(x*x+y*y);
    return (int)Math.round(d);
  }

  // returns the location of a Clover that has not
  // yet been pollinated and that is nearest to loc;
  // if the grid does not contain any unpollinated clovers,
  // returns null
  // precondition: loc != null
  public Location findNearestClover(Location loc)
  { /* updated 1:46pm Sat 4/25/2009 */ 
     Grid<Actor> gr= getGrid();
     if (gr == null) return null;
     ArrayList<Location> locs=gr.getOccupiedLocations();
     //remove all but the unpollinated clover
     int i=0;
     while (i<locs.size() ){
         Actor a=(Actor)gr.get(locs.get(i));
         if (a instanceof Clover && !((Clover)a).hasBeenPollinated())
         {
             i++;//skip to next
         }else{
             locs.remove(i);
         }
     }
     if (locs.size()==0) return null;
     Location closest=locs.get(0);
     i=1;
     while(i<locs.size() ){
        if (distance(loc, locs.get(i))<distance(loc,closest) )
             closest=locs.get(i);
        i++;
     }
     return closest;
    }
}

Clover

The Clover starter code:

import info.gridworld.grid.*; 
import info.gridworld.actor.*; 
import java.awt.Color; 
public class Clover extends Flower
{
  boolean pollinated;
  public Clover()
  {
      pollinated=false;
      setColor(Color.GREEN);
  }
  // pollinates this clover (typically called by a Bee)
  public void pollinate()
  { 
      pollinated=true; 
      setColor(Color.RED);
  }

  // returns true if this clover has been pollinated;
  // otherwise returns false
  public boolean hasBeenPollinated()
  {  
    return pollinated;
  }

  // fields, constructors, and other methods not shown
}

BeeRunner

Here is the code to help you see if your solution actually works. Don't forget that you can run it and right click on the grid to add bees and rocks and clovers.

import info.gridworld.grid.*; 
import info.gridworld.actor.*; 
import java.awt.Color; 
public class BeeRunner
{
	// instance variables - replace the example below with your own

	public static void main (String[] args)
	{
		ActorWorld world = new ActorWorld(new BoundedGrid(10,10) );
		Bee b=new Bee();
		b.setColor(Color.YELLOW);
		b.setDirection(Location.NORTH);
		world.add(new Location(2,3), b);

		Rock r =new Rock();
		Location rockLocation= new Location (5,5);
		world.add( rockLocation , r);

		world.add( new Location( 3,3), new Clover() );
		world.add( new Location( 8,8), new Clover() );
		world.add( new Location( 0,8), new Clover() );

        world.show();
	}


}