thinkapjava 5.1.2 documentation

GridWorld: Part 1

«  Conditionals and recursion   ::   Contents   ::   Value methods  »

GridWorld: Part 1

Getting started

Now is a good time to start working with the AP Computer Science Case Study, which is a program called GridWorld. To get started, install GridWorld, which you can download from the College Board.

When you unpack this code, you should have a folder named GridWorldCode that contains projects/firstProject, which contains BugRunner.java.

Make a copy of BugRunner.java in another folder and then import it into your development environment. There are instructions here that might help.

Once you run BugRunner.java, download the GridWorld Student Manual.

The Student Manual uses vocabulary I have not presented yet, so to get you started, here is a quick preview:

  • The components of GridWorld, including Bugs, Rocks and the Grid itself, are objects.
  • A constructor is a special method that creates new objects.
  • A class is a set of objects; every object belongs to a class.
  • An object is also called an instance because it is a member, or instance, of a class.
  • An attribute is a piece of information about an object, like its color or location.
  • An accessor method is a method that returns an attribute of an object.
  • A modifier method changes an attribute of an object.

Now you should be able to read Part 1 of the Student Manual and do the exercises.

BugRunner

BugRunner.java contains this code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import info.gridworld.actor.ActorWorld;
import info.gridworld.actor.Bug;
import info.gridworld.actor.Rock;

public class BugRunner
{
    public static void main(String[] args)
    {
        ActorWorld world = new ActorWorld();
        world.add(new Bug());
        world.add(new Rock());
        world.show();
    }
}

The first three lines are import statements; they list the classes from GridWorld used in this program. You can find the documentation for these classes here.

Like the other programs we have seen, BugRunner defines a class that provides a main method. The first line of main creates an ActorWorld object. new is a Java keyword that creates new objects.

The next two lines create a Bug and a Rock, and add them to world. The last line shows the world on the screen.

Open BugRunner.java for editing and replace this line:

world.add(new Bug());

with these lines:

Bug redBug = new Bug();
world.add(redBug);

The first line assigns the Bug to a variable named redBug; we can use redBug to invoke the Bug’s methods. Try this:

System.out.println(redBug.getLocation());

Note: If you run this before adding the Bug to the world, the result is null, which means that the Bug doesn’t have a location yet.

Invoke the other accessor methods and print the bug’s attributes. Invoke the methods canMove, move and turn and be sure you understand what they do.

Exercises

Exercise

  1. Write a method named moveBug that takes a bug as a parameter and invokes move. Test your method by calling it from main.
  2. Modify moveBug so that it invokes canMove and moves the bug only if it can.
  3. Modify moveBug so that it takes an integer, n, as a parameter, and moves the bug n times (if it can).
  4. Modify moveBug so that if the bug can’t move, it invokes turn instead.

Exercise

  1. The Math class provides a method named random that returns a double between 0.0 and 1.0 (not including 1.0).

  2. Write a method named randomBug that takes a Bug as a parameter and sets the Bug’s direction to one of 0, 90, 180 or 270 with equal probability, and then moves the bug if it can.

  3. Modify randomBug to take an integer n and repeat n times.

    The result is a random walk, which you can read about at Wikipedia.

  4. To see a longer random walk, you can give ActorWorld a bigger stage. At the top of BugRunner.java, add this import statement:

    import info.gridworld.grid.UnboundedGrid;
    

    Now replace the line that creates the ActorWorld with this:

    ActorWorld world = new ActorWorld(new UnboundedGrid());
    

    You should be able to run your random walk for a few thousand steps (you might have to use the scrollbars to find the Bug).

Exercise

GridWorld uses Color objects, which are defined in a Java library. You can read the documentation here.

To create Bugs with different colors, we have to import Color:

import java.awt.Color;

Then you can access the predefined colors, like Color.blue, or create a new color like this:

Color purple = new Color(148, 0, 211);

Make a few bugs with different colors. Then write a method named colorBug that takes a Bug as a parameter, reads its location, and sets the color.

The Location object you get from getLocation has methods named getRow and getCol that return integers. So you can get the x-coordinate of a Bug like this:

int x = bug.getLocation().getCol();

Write a method named makeBugs that takes an ActorWorld and an integer n and creates n bugs colored according to their location. Use the row number to control the red level and the column to control the blue.

«  Conditionals and recursion   ::   Contents   ::   Value methods  »