Flower Class Grid World Case Study

Chapter 10  GridWorld: Part 2

Part 2 of the GridWorld case study uses some features we haven’t seen yet, so you will get a preview now and more details later. As a reminder, you can find the documentation for the GridWorld classes at .

When you install GridWorld, you should have a folder named , which contains , and .

Copy these files into your working folder and import them into your development environment. There are instructions here that might help: .

Here is the code from :

info.gridworld.actor.ActorWorld; info.gridworld.grid.Location; java.awt.Color; BoxBugRunner { main(String[] args) { ActorWorld world = ActorWorld(); BoxBug alice = BoxBug(6); alice.setColor(Color.ORANGE); BoxBug bob = BoxBug(3); world.add( Location(7, 8), alice); world.add( Location(5, 5), bob); world.show(); } }

Everything here should be familiar, with the possible exception of , which is part of GridWorld, and similar to .

contains the class definition for BoxBug.

BoxBug Bug { steps; sideLength; BoxBug( length) { steps = 0; sideLength = length; } }

The first line says that this class extends , which means that a is a kind of .

The next two lines are instance variables. Every has variables named , which determines the size of the box it draws, and , which keeps track of how many steps the has taken.

The next line defines a constructor, which is a special method that initializes the instance variables. When you create a by invoking , Java invokes this constructor.

The parameter of the constructor is the side length.

behavior is controlled by the method. Here is the method for :

act() { (steps < sideLength && canMove()) { move(); steps++; } { turn(); turn(); steps = 0; } }

If the can move, and has not taken the required number of steps, it moves and increments .

If it hits a wall or completes one side of the box, it turns 90 degrees to the right and resets to 0.

Run the program and see what it does. Did you get the behavior you expected?

10.1  Termites

I wrote a class called that extends and adds the ability to interact with flowers. To run it, download these files and import them into your development environment:




Because extends , all methods also work on s. But s have additional methods that s don’t have.

hasFlower(); seeFlower(); createFlower(); dropFlower(); throwFlower(); pickUpFlower();

For some methods provides one definition and provides another. In that case, the method overrides the method.

For example, returns if there is a flower in the next location, so s can trample s. returns if there is any object in the next location, so behavior is different.

As another example, Termites have a version of that takes an integer number of degrees as a parameter. Finally, Termites have , which turns left or right 45 degrees at random.

Here is the code from :

TermiteRunner { main(String[] args) { ActorWorld world = ActorWorld(); makeFlowers(world, 20); Termite alice = Termite(); world.add(alice); Termite bob = Termite(); bob.setColor(Color.blue); world.add(bob); world.show(); } makeFlowers(ActorWorld world, n) { ( i = 0; i<n; i++) { world.add( EternalFlower()); } } }

Everything here should be familiar. creates an with 20 and two .

An is a that overrides so the flowers don’t get darker.

EternalFlower Flower { act() {} }

If you run you should see two termites moving at random among the flowers.

demonstrates the methods that interact with flowers. Here is the class definition:

MyTermite Termite { act() { (getGrid() == ) ; (seeFlower()) { pickUpFlower(); } (hasFlower()) { dropFlower(); } (canMove()) { move(); } randomTurn(); } }

extends and overrides . If sees a flower, it picks it up. If it has a flower, it drops it.

10.2  Langton’s Termite

Langton’s Ant is a simple model of ant behavior that displays surprisingly complex behavior. The Ant lives on a grid like GridWorld where each cell is either white or black. The Ant moves according to these rules:

  • If the Ant is on a white cell, it turns to the right, makes the cell black, and moves forward.
  • If the Ant is on a black cell, it turns to the left, makes the cell white, and moves forward.

Because the rules are simple, you might expect the Ant to do something simple like make a square or repeat a simple pattern. But starting on a grid with all white cells, the Ant makes more than 10,000 steps in a seemingly random pattern before it settles into a repeating loop of 104 steps.

You can read more about Langton’s Ant at .

It is not easy to implement Langton’s Ant in GridWorld because we can’t set the color of the cells. As an alternative, we can use Flowers to mark cells, but we can’t have an Ant and a Flower in the same cell, so we can’t implement the Ant rules exactly.

Instead we’ll create what I’ll call a , which uses to check whether there is a flower in the next cell, to pick it up, and to put a flower in the next cell. You might want to read the code for these methods to be sure you know what they do.

10.3  Exercises

Exercise 1   Now you know enough to do the exercises in the Student Manual, Part 2. Go do them, and then come back for more fun.

Exercise 2   The purpose of this exercise is to explore the behavior of Termites that interact with flowers.

Modify to create instead of . Then run it again. should move around at random, moving the flowers around. The total number of flowers should stay the same (including the ones are holding).

In Termites, Turtles and Traffic Jams, Mitchell Resnick describes a simple model of termite behavior:

  • If you see a flower, pick it up. Unless you already have a flower; in that case, drop the one you have.
  • Move forward, if you can.
  • Turn left or right at random.

Modify to implement this model. What effect do you think this change will have on the behavior of ?

Try it out. Again, the total number of flowers does not change, but over time the flowers accumulate in a small number of piles, often just one.

This behavior is an an emergent property, which you can read about at . follow simple rules using only small-scale information, but the result is large-scale organization.

Experiment with different rules and see what effect they have on the system. Small changes can have unpredicable results!

Exercise 3  
  1. Make a copy of called and a copy of called . Modify them so the class definitions have the same name as the files, and so creates a .
  2. If you create a file named , GridWorld uses it to represent your Termite. You can download excellent pictures of insects from . To convert them to GIF format, you can use an application like ImageMagick.
  3. Modify to implement rules similar to Langton’s Ant. Experiment with different rules, and with both 45 and 90 degree turns. Find rules that run the maximum number of steps before the Termite starts to loop.
  4. To give your Termite enough room, you can make the grid bigger or switch to an .
  5. Create more than one and see how they interact.

У Мидж отвисла челюсть. - Извините, сэр… Бринкерхофф уже шел к двери, но Мидж точно прилипла к месту. - Я с вами попрощался, мисс Милкен, - холодно сказал Фонтейн.  - Я вас ни в чем не виню.

0 Replies to “Flower Class Grid World Case Study”

Lascia un Commento

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *