Grub

<< Mountain | APQuestionsTrailIndex | Stats >>

Grub

GrubQuestion.pdf

Grub.gif

Starter Code

Grub.java

import info.gridworld.grid.*; 
import info.gridworld.actor.*; 
import java.awt.Color; 
import java.util.ArrayList;
public class Grub extends Critter 
{ 
  private int maxDistance; 

  public Grub(int distance) 
  {  maxDistance = distance;  } 

  /** @return one of the eight direction constants from the Location class 
   */ 
  public int getRandomDirection() 
  {  /* to be implemented in part (a) */  
    return 45*(int)(8*Math.random());

    } 


  /** Gets a list of possible locations for the next move. These locations must be valid  
   *  in the grid of this Grub. Implemented to return all locations in a random direction  
   *  up to and including the maximum distance that this Grub can burrow.  
   *  Postcondition: The state of all actors is unchanged. 
   *  @return a list of all locations within the maximum distance in a randomly selected direction  
   */ 
  public ArrayList<Location> getMoveLocations() 
  {  /* to be implemented in part (b) */ 
    ArrayList<Location> result=new ArrayList<Location>();

    return result;
    } 


  /** Selects the location for the next move.  
   *  Postcondition: (1) The returned location is an element of locs, this critter's current location,  
   *  or null. (2) The state of all actors is unchanged. 
   *  @param locs the possible locations for the next move  
   *  @return the location that was selected for the next move, or null to indicate  
   *          that this Grub should be removed from the grid. 
   */ 
  public Location selectMoveLocation(ArrayList<Location> locs) 
  {  /* to be implemented in part (c) */  
    Location result=getLocation();
    return result;
    } 

  // There may be instance variables, constructors, and methods that are not shown. 

} 

GrubRunner.java

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

	public static void main (String[] args)
	{
		ActorWorld world = new ActorWorld(new BoundedGrid(8,8) );

		Grub g1=new Grub(3);

		Grub g2=new Grub(5);

		world.add(new Location (2,2), g1);
		world.add(new Location (5,3), g2);
		world.add(new Location (2,0), new Flower());
		world.add(new Location (7,7), new Flower());
		world.add(new Location (5,5), new Rock());

        world.show();
	}


}

Solutions

part (a)

//returne one of the random directions of the Location class
public in getRandomDirection()
{
    int r=(int)(Math.random()*8)
    return 45*r;
}

or

     Random dice=new Random();
     return 45*dice.nextInteger(8);

or


  int turns = (int) (Math.random() * 8); 
  return Location.NORTH + turns * Location.HALF_RIGHT; 

Part (b)

    ArrayList<Location> result=new ArrayList<Location>();
    int direction=getRandomDirection();
    Location loc=getLocation();
    Grid gr=getGrid();

    for (int i=0; i< maxDistance; i++);
    {
        loc=loc.getAdjacentLocation(direction);
        if (gr.isValid(loc))
            result.add(loc);
    }
    return result;

Part (c)

    Location result=getLocation();
    if (locs.size()==0) return result;
    Grid gr=getGrid();
    int r=(int)(locs.size()*Math.random());
    result=locs.get(r);
    Actor a=(Actor)gr.get(result);
    if(a == null || a instanceof Flower)
      {
        return result;
      } else {
        return null;
      }