Gridworld Part 3

We have played around with the sample projects for a bit and now we can start making our own. All that is needed is to tell our IDE (BlueJ or Eclipse) where gridworld.jar lives. It is a library that is quite complete. Details are in the Podcasts, but you can read about the installation of the library here at the College Board

Eclipse Instructions

  1. Start a New Project, and name it something (like "Bugs" for example)
  2. Select (Click once) your Project in the Package Explorer
  3. Select from the MENU Project-> Properties
  4. Click on the Java Build Path
  5. Click on the Libraries Tab
  6. Press the Add External JARs... button
  7. Select the file gridworld.jar (this was extracted from the file ap07_cs_gridworldcode.zip

BlueJ Instructions

You may find BlueJ a better IDE for this sort of activity. In order for the code to compile make sure the gridworld.jar file from ap07_cs_gridworldcode.zip is listed in the libraries.
  1. Open BlueJ Preferences
  2. Click the Libraries tab
  3. If gridworld.jar isnt already there, press the Add button and select the gridworld.jar file in the browser
  4. Click the OK button
  5. If you just added a library, you may wish to restart BlueJ

Part 3 (Set 3)

You will learn (and rememeber better) how things work by running code that tests your answers. For example, go ahead and read the first part of Part 3 (it starts on page 16) in the Student manual up to Did you know? Set 3 on page 19.

Try to answer these questions on your own. Use pp. 16-19 and Appendix B page B-1.

Now to see how you did, write a class to find out how you did. For example, here is a tiny program I wrote, copying and pasting from the Manual, then adding my own println statements to reveal the results of certain method calls. If you are lazy (but clever) you can use the Debugger to inspect the results instead of println statements. Go ahead and copy and paste the following.

Part3Set3.java

import info.gridworld.actor.ActorWorld;
import info.gridworld.grid.Location;
import java.awt.Color;

/**
 * This class helps test out answers to Set 3. 
 * 
 */
public class Part3Set3 
{
    public static void main(String[] args)
    {
        
        Location loc1 = new Location(4,3);
        Location loc2 = new Location(3,4);
        //Question 1
         System.out.println("1. The row value of loc1 is "+loc1.getRow() );
        //Question 2
         Boolean b= loc1.equals(loc2);
         System.out.println("2. The value of b is "+ b );
        //Question 3
         Location loc3 = loc2.getAdjacentLocation(Location.SOUTH); 
         System.out.println("3. The value of loc3 is "+ loc3 );
        //Question 4
        int dir = loc1.getDirectionToward(new Location(6,5));
         System.out.println("4. The direction of 6,5 from loc1 is "+ dir );
        //Question 5
        Location ne=loc1.getAdjacentLocation( Location.NORTHEAST );
         System.out.println("5. The northeast location from loc1 is location of a is "+ ne );
       
      
    }
}

The Grid Interface (Set 4)

Lets continue to write little classes that will demonstrate what you read. For example, look at page 20. Now here is a program you can modify to see the results of Grid<E> method isValid. Notice the import line at the top. Look at Appendix B. This is how I knew it needed the info.gridworld.grid.* imported. The * is a "wildcard" that will include all the Classes in info.gridworld.grid, including the Location class and the two implementations of the Grid<E> Interface, BoundedGrid and UnboundedGrid.

GridTest.java

import info.gridworld.grid.*;

/**
 * This class runs a world that helps test out answers to Set 4. 
 * 
 */
public class GridTest 
{
    public static void main(String[] args)
    {
        BoundedGrid gr=new BoundedGrid(10,10);
        Location loc1 = new Location(4,3);
        Location loc2 = new Location(11,4);
       
         System.out.println("Is 4,3 in gr? " + gr.isValid(loc1) );
         System.out.println("Is 11,4 in gr? " + gr.isValid(loc2) );
        
    }
}
Go ahead and change the GridTest code to see if you can get it to determine if 0,0 or 10,10 are valid locations.

Now lets turn to the Do You Know? Set 4. To see getOccupiedLocations() at work, we will populate a Grid and then call it. Since it returns an ArraryList, we can use the size() method to see how many locations there are!

Part3Set4.java

import info.gridworld.grid.*;
import java.util.ArrayList;
/**
 * This class runs a world that helps test out answers to Set 4. 
 * 
 */
public class Part3Set4 
{
    public static void main(String[] args)
    {
        BoundedGrid gr=new BoundedGrid(10,10);
        Location loc1 = new Location(4,3);
        Location loc2 = new Location(1,1);
         
        gr.put(loc1, "keys");
        gr.put(loc2, "coins");
        gr.put(new Location(2,1), 7);
        
        ArrayList stuff=gr.getOccupiedLocations();
        System.out.println(stuff);
        System.out.println(stuff.size());
        System.out.println( (String)gr.get(loc1) );
        System.out.println( gr.get(new Location (2,1) ) );
        System.out.println("There are "+gr.getOccupiedLocations().size()+" things in the grid");
    }
}
Can you see there are several other methods in the Grid Interface? Go to Appendix B page B-2 and see if you can build upon Part3Set4 to get results printed from calls to : You will get warnings when you compile, since we are using ArrayList without any wrappers, which in general is not very practical. In the case study, we will usually populate an ActorWorld with Actors. Right now we are merely experimenting with the Grid Interface.

The Actor Class (Set 5)

You should look carefully at the code in Appendix B starting at page B-3, and try to answer these questions. The following are a small Runner Classes to confirm your guesses.

ActorRunner.java


import info.gridworld.actor.*;
import info.gridworld.grid.*;
public class ActorRunner 
{ 
  public static void main(String[] args) 
  { 
    ActorWorld world = new ActorWorld(); 
    Actor a = new Actor(); 
    world.add(a);     
    Grid g = a.getGrid();  //must remember the grid for placement back in 
                           //the grid      
    world.add(new Rock()); 
    world.show(); 
     
  } 
} 
 
  1. Where do the actors show up if you dont specify where?
  2. What are the default colors?
  3. What is the default act() method do for an Actor?

Part3Set5Number4a.java

import info.gridworld.actor.*;
import java.awt.Color;
public class Part3Set5Number4a 
{ 
  public static void main(String[] args) 
  { 
    ActorWorld world = new ActorWorld(); 
    Bug b = new Bug(); 
    world.add(b); 
    b.putSelfInGrid(b.getGrid(),b.getLocation()); 
    world.add(new Rock()); 
    world.show(); 
  } 
} 

Part3Set5Number4b.java

import info.gridworld.actor.*;
import java.awt.Color;
public class Part3Set5Number4b 
{ 
  public static void main(String[] args) 
  { 
    ActorWorld world = new ActorWorld(); 
    Bug b = new Bug(); 
    world.add(b); 
    world.add(new Rock()); 
    world.show(); 
    b.removeSelfFromGrid(); 
    b.removeSelfFromGrid(); 

  } 
} 

Part3Set5Number4c.java

import info.gridworld.actor.*;
import info.gridworld.grid.*;
public class Part3Set5Number4c 
{ 
  public static void main(String[] args) 
  { 
    ActorWorld world = new ActorWorld(); 
    Actor a = new Actor(); 
    world.add(a);     
    Grid g = a.getGrid();  //must remember the grid for placement back in 
                           //the grid      
    world.add(new Rock()); 
    world.show(); 
    a.removeSelfFromGrid(); 
    a.putSelfInGrid(g,new Location(5,5)); //must specify a location here  
  } 
} 
 

The Bug Class (Set 6)

Try to answer these by looking at the Appendix C page C-1. Here is a class for experimenting:

Part3Set6Number9.java

import info.gridworld.actor.*;
import info.gridworld.grid.*;
public class Part3Set6Number9
{ 
  public static void main(String[] args) 
  { 
    ActorWorld world = new ActorWorld(); //ActorWorld is not Testable.. see page 27
    Bug b = new Bug(); 
    world.add(new Location(9,9), b);     
    world.show(); 
    b.removeSelfFromGrid();
  } 
} 

The Jumper Class (The Group Activity)

You can find the specifications for this Actor on page 26 of the Student Manual. You may wish to make a new project for this one. Here are some starter Classes for Making the new Actor called Jumper. Do not make a subclass of Bug. Instead, use the Bug Class as a "hint" to make your new subclass of Actor called Jumper.

Jumper.java


import info.gridworld.actor.*; 
import info.gridworld.grid.*;
import java.awt.Color; 
 
/** 
 * A Jumper is an actor that will jump over Rocks and Flowers  
 * and turn.  
 */ 
public class Jumper extends Actor 
{ 
  /** 
   * Constructs a pink Jumper. 
   */ 
  public Jumper() 
  { 
    
  } 
 
  /** 
   * Constructs a Jumper of a given color. 
   * @param JumperColor the color for this Jumper 
   */ 
  public Jumper(Color JumperColor) 
  { 
    
  } 
 
  public void act() 
  { 
    
  } 
 
  /** 
   * Turns the Jumper 45 degrees to the right without changing its  
   * location. 
   */ 
  public void turn() 
  { 
    
  } 

 
 

  /** 
   * Moves the Jumper forward two locations. 
   * The location two in front must be valid or the Jumper will remove  
   * itself from the grid. 
   */ 
  public void jump() 
  { 
    
  } 
 
  /** 
   * Tests whether this Jumper can move forward into a location two in  
   * front that is empty or contains a flower. 
   * The location one in front must be empty or contain a Rock or a  
   * Flower.
   *
   * Hint: You can tell what kind of Actor something is by using the
   * instanceof command.  For example, if Flower f=new Flower(), then 
   * f instanceof Flower returns true, and f instanceof Rock returns false.
   *
   * @return true if this Jumper can move. 
   */ 
  public boolean canJump() 
  { 
          
  } 
} 

JumperRunner.java

import info.gridworld.actor.*;

/** 
 * This class runs a world that contains a jumper, a bug, a flower, and a  
 * rock added at random locations.  
 */ 
public class JumperRunner 
{ 
  public static void main(String[] args) 
  { 
    ActorWorld world = new ActorWorld(); 
    world.add(new Jumper()); 
    world.add(new Rock()); 
    world.add(new Bug()); 
    world.add(new Flower()); 
    world.show(); 
  } 
}