function:RenderSidePart pageleftbodycaption pageleftbody sidenote Main.GridWorld4-SideNote Main.SideNote Site.SideNote

# Grid World 4

<< GridWorld 3 | HomeworkTrailIndex | >>

# Review Exercises

1. Set 7, Nos. 1-6, page 28
1. What 6 methods are implemented in Critter?
2. What are the five basic actions common to all critters when they act?
3. Should subclasses of Critter override the getActors method? Explain.
4. Describe three ways that a critter could process actors.
5. What three methods must be invoked to make a critter move? Explain each of these methods.
6. Why is there no Critter constructor?
2. Set 8, Nos. 1-6, page 30
1. Why does act cause a ChameleonCritter to act differently from a Critter even though ChameleonCritter does not override act?
2. Why does the makeMove method of ChameleonCritter call super.makeMove?
3. How would you make the ChameleonCritter drop flowers in its old location when it moves?
4. Why doesn’t ChameleonCritter override the getActors method?
5. Which class contains the getLocation method?
6. How can a Critter access its own grid?
3. Set 9, Nos. 1-7, pages 31-32
1. Why doesn’t CrabCritter override the processActors method?
2. Describe the process a CrabCritter uses to find and eat other actors. Does it always eat all neighboring actors? Explain.
3. Why is the getLocationsInDirections method used in CrabCritter?
4. If a CrabCritter has location (3,4) and faces south, what are the possible locations for actors that are returned by a call to the getActors method?
5. What are the similarities and differences between the movements of a CrabCritter and a Critter?
6. How does a CrabCritter determine when it turns instead of moving?
7. Why don’t the CrabCritter objects eat each other?
4. Exercises 1-6, page 32 In the following exercises, your first step should be to decide which of the five methods—getActors, processActors, getMoveLocations, selectMoveLocation, and makeMove—should be changed to get the desired result.
1. Modify the processActors method in ChameleonCritter so that if the list of actors to process is empty, the color of the ChameleonCritter will darken (like a flower).
2. Create a class called ChameleonKid that extends ChameleonCritter as modified in exercise 1. A ChameleonKid changes its color to the color of one of the actors immediately in front or behind. If there is no actor in either of these locations, then the ChameleonKid darkens like the modified ChameleonCritter.
3. Create a class called RockHound that extends Critter. A RockHound gets the actors to be processed in the samewayasaCritter. Itremovesanyrocksinthatlistfromthegrid.ARockHoundmoveslikea Critter.
4. Create a class BlusterCritter that extends Critter. A BlusterCritter looks at all of the neighbors within two steps of its current location. (For a BlusterCritter not near an edge, this includes 24 locations). It countsthenumberofcrittersinthoselocations.Iftherearefewerthanccritters,theBlusterCritter’s color getsbrighter(colorvaluesincrease).Iftherearecormorecritters,theBlusterCritter’s colordarkens(color values decrease). Here, c is a value that indicates the courage of the critter. It should be set in the constructor.
5. Create a class QuickCrab that extends CrabCritter. A QuickCrab processes actors the same way a CrabCritter does. A QuickCrab moves to one of the two locations, randomly selected, that are two spaces to its right or left, if that location and the intervening location are both empty. Otherwise, a QuickCrab moves like a CrabCritter.
6. Create a class KingCrab that extends CrabCritter. A KingCrab gets the actors to be processed in the same way a CrabCritter does. A KingCrab causes each actor that it processes to move one location further away from the KingCrab. If the actor cannot move away, the KingCrab removes it from the grid. When the KingCrab has completed processing the actors, it moves like a CrabCritter.
5. Group Activity: CrabCritter, pages 33-34
1. Specify: Each group specifies a new creature that extends Critter. The specifications must describe the properties and behavior of the new creature in detail.
2. Design: The groups exchange specifications. Each group reads the specification that it received and determines the needed variables and basic algorithms for the creature.
3. Code: Each group implements the code for the creature in the specification that it received.
4. Test: Each group writes test cases for the creature that it specified in step 1. Test cases are exchanged as in step 2. The recipients verify that the implementations meet the specifications.

# Programming Exercises

Do one for 7 points, two for 8, etc...

## PolluterCritter

Make a critter that drops a rock wherever it eats a bug or a flower.

## PolluterCritter.java

import info.gridworld.actor.*;
import info.gridworld.grid.*;
import java.util.ArrayList;
/**
*PolluterCritter-- a critter that drops a rock of the same color as itself wherever it eats a bug or a flower..(or rather, a non-rock, non-critter)
*
* @version (a version number or a date)
*/
public class PolluterCritter extends Critter
{

}


## PolluterCritterRunner.java

import info.gridworld.actor.*;
import info.gridworld.grid.*;
public class PolluterCritterRunner
{
public static void main(String[] args)
{
ActorWorld world = new ActorWorld(new BoundedGrid(5,5));
world.show();
}
}



## PointerCritter

Make a critter so it only eats things that it faces, rather than all its neighbors. Have it turn into the same color as what it eats. If there is nothing to eat, have it move there if it can, otherwise have it turn left 45 degrees.

## RockHoundCritter

Make a critter that only eats rocks.

## YellowHunterCritter

Make a critter that only eats yellow flowers, yellow rocks or yellow bugs. Make it move in the direction of closest edible object (a yellow rock, a yellow flower or a yellow bug..NOT a yellow critter---make sure examine everything on the grid, not just the neighborhood, so that it will move toward the yellow food if it is more than one step away).

## BreederCritter

Make a critter that will "drop a critter" if there is a critter in the neighborhood. If there is no empty Location in its neighborhood to drop a new critter, have it die (presumably by embarrassment or overcrowding).

## HungryCritter

Make a critter that will die if it runs out of food. Keep track of how many things it has eaten. Each step, it uses up one of the things it has eaten. If it runs out, it dies.

## SpawnCritter

Make a Critter that will spawn critters in all locations where it eats something.