2013 Grid World Utilities

<< 2013 TokenPass | APQuestionsTrailIndex | 2013 SkyView >>

Click here to see the questions from 2013

GridWorldUtilities.java

import info.gridworld.actor.Actor;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;

import java.util.ArrayList;


public class GridWorldUtilities 
{

	/** Gets all the locations in grid that do not contain objects.
	 * @param grid a reference to a BoundedGrid object
	 * @return an array list (possibly empty) of empty locations in grid.
	 * The size of the returned list is 0 if there are no empty locations in grid.
	 * Each empty location in grid should appear exactly once in the returned list. 
	 */ 
	public static ArrayList<Location> getEmptyLocations(Grid<Actor> grid)
	{
                 /* to be implemented in part (a) */


	}
	//There may be instance variables that are not shown.
}

JumpingCritter.java

import java.util.ArrayList;
import info.gridworld.actor.Critter;
import info.gridworld.grid.Location;


public class JumpingCritter extends Critter {

	/**
	 * A JumpingCritter acts like a Critter, except that it moves by jumping to a 
	 * randomly selected empty location in its grid. 
	 * If there are no empty locations, the JumpingCritter removes itself from the grid.
	 */

	//to be implemented inpart (b)
}

JumpingCritterRunner1.java

import info.gridworld.actor.*;
import info.gridworld.grid.*;
import java.awt.Color;
public class JumpingCritterRunner1 
{
	public static void main(String[] args)
	{
		ActorWorld world = new ActorWorld();
		world.setGrid(new BoundedGrid<Actor>(4,4));
		JumpingCritter j=new JumpingCritter();
		j.setDirection(Location.SOUTHWEST);
		Bug b = new Bug();
		b.setDirection(Location.EAST);
		world.add(new Location(2, 1), new Rock());
		world.add(new Location(3, 1), b);
		world.add(new Location(2, 0), j);
		world.show();
	}

}

JumpingCritterRunner2.java

import info.gridworld.actor.*;
import info.gridworld.grid.*;
import java.awt.Color;
public class JumpingCritterRunner2 
{
	public static void main(String[] args)
	{
		ActorWorld world = new ActorWorld();
		world.setGrid(new BoundedGrid<Actor>(4,4));
		JumpingCritter j=new JumpingCritter();
		j.setDirection(Location.NORTH);


		for(int r=0 ; r<4;r++)
			for(int c=0; c<4; c++)
				world.add(new Location(r, c), new Rock());
		world.add(new Location(1,2), j);
		world.show();
	}
}