Grid World 1

<< Elevens Lab | HomeworkTrailIndex | GridWorld 2 >>

Understanding GridWorld

QuickTime Movie on How to install Gridworld

Chapter 1 Text

Download a Answer Form

Reference Guide You'll have to use during the exam

The Gridworld code

Complete Text

Demo 0

Time to get re-aquatinted with Gridworld, this time with knowledge about Java and Computer Science!

To get aquatinted with the methods available make an Eclipse Project, go to Properties, Java Build Path, Libraries, and Add the External JAR gridworld.jar (You can get this from Gridword Code )

BlueJ

BlueJ opens external jar libraries for all all the projects at once, so this only needs to be done once.

  1. Make a project Window active (close any file you're editing)
  2. Select the "Tools" menu (3rd one over)
  3. Select "Preferences..."(Ctrl-Comma)
  4. Click the "Libraries" tab (4th tab over)
  5. Press the "Add" button
  6. Select the gridworld.jar file you extracted from the Gridworld.zip file you downloaded
  7. Press the "Open" button (you should see the file listed now)
  8. Press the Ok" button
  9. restart BlueJ

Eclipse

Installing the grid word.jar library needs to be done for each Gridword Project.

  1. Right-click the Project in the Package Explorer, and select Properties (Alt-Enter)
  2. Click on "Java Build Path" (third one down)
  3. Click on the Libraries Tab (Third one over)
  4. Click on "Add External JARs..." (second button down)
  5. choose the gridworld.jar file you extracted from the Gridworld.zip file you downloaded
  6. Press "Open" (you should see the file listed now)
  7. Press OK

Once it is up and running, make an instance of Bug called amy and make here a custom Color, pointing South, and get the instance of the Grid that myWorld has, so that you can tell amy to put herself in the grid.

Try to predict how may steps it takes before both bugs are in the opposite corners from where they started.

import java.awt.Color;

import info.gridworld.actor.*;
import info.gridworld.grid.BoundedGrid;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;

public class Demo0 
{
	public static void main (String[] args)
	{
		ActorWorld  myWorld = new ActorWorld();
		myWorld.setGrid(new BoundedGrid<Actor>(4,5));
		myWorld.setMessage("Welcome to my World");
		myWorld.add(new Location(3,4), new Bug());
		// start dealing with the grid
		Grid<Actor> gr = myWorld.getGrid();
		//your code here



		myWorld.show();
	}
}

Exercises

These are in the Gridworld Student Manual

  1. Set 1, Nos. 1-10, page 6
  2. Exercises 1-4, page 8

Download a Answer Form

BoxBug.java

/* 
 * AP(r) Computer Science GridWorld Case Study:
 * Copyright(c) 2005-2006 Cay S. Horstmann (http://horstmann.com)
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * @author Cay Horstmann
 * @author Chris Nevison
 * @author Barbara Cloud Wells
 */

import info.gridworld.actor.Bug;

/**
 * A <code>BoxBug</code> traces out a square "box" of a given size. <br />
 * The implementation of this class is testable on the AP CS A and AB exams.
 */
public class BoxBug extends Bug
{
    private int steps;
    private int sideLength;

    /**
     * Constructs a box bug that traces a square of a given side length
     * @param length the side length
     */
    public BoxBug(int length)
    {
        steps = 0;
        sideLength = length;
    }

    /**
     * Moves to the next location of the square.
     */
    public void act()
    {
        if (steps < sideLength && canMove())
        {
            move();
            steps++;
        }
        else
        {
            turn();
            turn();
            steps = 0;
        }
    }
}

BoxBugRunner.java

/* 
 * AP(r) Computer Science GridWorld Case Study:
 * Copyright(c) 2005-2006 Cay S. Horstmann (http://horstmann.com)
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * @author Cay Horstmann
 * @author Chris Nevison
 * @author Barbara Cloud Wells
 */

import info.gridworld.actor.ActorWorld;
import info.gridworld.grid.Location;

import java.awt.Color;

/**
 * This class runs a world that contains box bugs. <br />
 * This class is not tested on the AP CS A and AB exams.
 */
public class BoxBugRunner
{
    public static void main(String[] args)
    {
        ActorWorld world = new ActorWorld();
        BoxBug alice = new BoxBug(6);
        alice.setColor(Color.ORANGE);
        BoxBug bob = new BoxBug(3);
        world.add(new Location(7, 8), alice);
        world.add(new Location(5, 5), bob);
        world.show();
    }
}

BugRunner.java

/* 
 * AP(r) Computer Science GridWorld Case Study:
 * Copyright(c) 2005-2006 Cay S. Horstmann (http://horstmann.com)
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * @author Cay Horstmann
 */

import info.gridworld.actor.ActorWorld;
import info.gridworld.actor.Bug;
import info.gridworld.actor.Rock;

/**
 * This class runs a world that contains a bug and a rock, added at random
 * locations. Click on empty locations to add additional actors. Click on
 * populated locations to invoke methods on their occupants. <br />
 * To build your own worlds, define your own actors and a runner class. See the
 * BoxBugRunner (in the boxBug folder) for an example. <br />
 * This class is not tested on the AP CS A and AB exams.
 */
public class BugRunner
{
    public static void main(String[] args)
    {
        ActorWorld world = new ActorWorld();
        world.add(new Bug());
        world.add(new Rock());
        world.show();
    }
}

Review Exercises


Programming Exercises