Class KarelWorld

java.lang.Object
  extended by greenfoot.World
      extended by KarelWorld

public final class KarelWorld
extends greenfoot.World

The KarelWorld contains robots, beepers, and walls. It is a grid of streets (horizontal) and avenues (vertical). The standard world has 20 streets and 20 avenues.

First street is at the bottom and the numbers increase as you go up. First avenue is at the left and the numbers increase as you go to the right.

Robots and beepers are at the intersections. Horizontal walls cross avenues between streets. Vertical walls cross streets between avenues.

This class is final so no subclasses are possible.

Version:
1.0 (Greenfoot) January 2010
Author:
Joseph Bergin.

The explosion sound file came from code written by Poul Henriksen

The other sounds are from the Scratch system: Copyright (c) 2007 Massachusetts Institute of Technology and used under their permissive license.


Nested Class Summary
static class KarelWorld.Direction
          Directions in the Karel world.
 
Field Summary
static KarelWorld.Direction EAST
          The east direction in the world
static int INFINITE
          Value for beepers that represents an infinite number of beepers on a corner or in a beeper bag.
static KarelWorld.Direction NORTH
          The north direction in the world
static KarelWorld.Direction SOUTH
          The south direction in the world
static KarelWorld.Direction WEST
          The west direction in the world
 
Constructor Summary
KarelWorld()
          Constructor for a default world (20 Streets and 20 Avenues unless the value has been changed with another world constructor).
KarelWorld(int numberOfStreetsAndAvenues)
          Construct a world of any size 5 or larger with the current cell size.
KarelWorld(int streets, int avenues)
          Create a world with a specified number of streets and avenues and the current cell size
KarelWorld(int streets, int avenues, int cellSize)
          Construct a world with a given number of streets (>= 5) and avenues (>= 5)
 
Method Summary
 void clearWorldOfBeepers()
          Remove all of the beepers from the world
 void clearWorldOfProps()
          Remove all of the Props from the world
static int delay()
          Get the value of the delay between robot operations.
static void initializeThreads()
          Initialize the robot thread system for a new execution.
static KarelWorld itself()
          Obtain a reference to the current world object
static void pauseThreads()
          Suspend all the threads in the world.
 void placeBeepers(int street, int avenue, int howMany)
          Place any number of beepers on a corner
 void placeWallEastOf(int street, int avenue)
          Place a one segment vertical wall East of a given corner
 void placeWallNorthOf(int street, int avenue)
          Place a one segment horizontal wall North of a given corner
 void readWorld(java.lang.String filename)
          Read a world from a file in the current directory.
 void readWorld(java.lang.String directoryPath, java.lang.String filename)
          Read a world from a file in the given directory path.
 void rereadLastWorld()
          Read in the most recently read world file if any
static void resumeThreads()
          Resume all the threads after stopping them.
 void saveWorld(java.lang.String filename)
          Save a world to a file in the current directory.
 void saveWorld(java.lang.String directoryPath, java.lang.String filename)
          Save the world in a file in the given directory path.
protected static void scaleImage(greenfoot.GreenfootImage image, int divide)
          The image files are 40 pixels on a side.
static void setDelay(int delay)
          Set the value of the delay between robot operations.
static void setupThread(java.lang.Runnable runnableObject)
          Let a robot run in its own thread.
protected static void sleep()
          Pause the operation of robots briefly so that the user can follow the changes visually.
static void startThreads()
          Start all the threads initially.
 
Methods inherited from class greenfoot.World
act, addObject, getBackground, getCellSize, getColorAt, getHeight, getObjects, getObjectsAt, getWidth, numberOfObjects, removeObject, removeObjects, repaint, setActOrder, setBackground, setBackground, setPaintOrder, started, stopped
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EAST

public static final KarelWorld.Direction EAST
The east direction in the world


INFINITE

public static final int INFINITE
Value for beepers that represents an infinite number of beepers on a corner or in a beeper bag.

See Also:
Constant Field Values

NORTH

public static final KarelWorld.Direction NORTH
The north direction in the world


SOUTH

public static final KarelWorld.Direction SOUTH
The south direction in the world


WEST

public static final KarelWorld.Direction WEST
The west direction in the world

Constructor Detail

KarelWorld

public KarelWorld()
Constructor for a default world (20 Streets and 20 Avenues unless the value has been changed with another world constructor). The cell size is the value last set in another constructor here or 40 by default.


KarelWorld

public KarelWorld(int numberOfStreetsAndAvenues)
Construct a world of any size 5 or larger with the current cell size. Too large a world will fail with a memory error.

Parameters:
numberOfStreetsAndAvenues - the number of streets and avenues (>= 5)

KarelWorld

public KarelWorld(int streets,
                  int avenues)
Create a world with a specified number of streets and avenues and the current cell size

Parameters:
streets - the number of streets
avenues - the number of avenues

KarelWorld

public KarelWorld(int streets,
                  int avenues,
                  int cellSize)
Construct a world with a given number of streets (>= 5) and avenues (>= 5)

Parameters:
streets - the number of streets in the world >= 5
avenues - the number of avenues in the world >= 5
cellSize - the cell size of the world (> 20)
Method Detail

clearWorldOfBeepers

public void clearWorldOfBeepers()
Remove all of the beepers from the world


clearWorldOfProps

public void clearWorldOfProps()
Remove all of the Props from the world


delay

public static int delay()
Get the value of the delay between robot operations.


initializeThreads

public static void initializeThreads()
Initialize the robot thread system for a new execution. After a run using the thread system, this must be invoked before the next run or else nothing will happen. But call it before you add any runnable robots to the world.


itself

public static KarelWorld itself()
Obtain a reference to the current world object

Returns:
the current (most recently created) world

pauseThreads

public static final void pauseThreads()
Suspend all the threads in the world.


placeBeepers

public void placeBeepers(int street,
                         int avenue,
                         int howMany)
Place any number of beepers on a corner

Parameters:
street - the street on which to place the beepers
avenue - the avenue on which to place the beepers
howMany - how many beepers to place (-1 means infinity)

placeWallEastOf

public void placeWallEastOf(int street,
                            int avenue)
Place a one segment vertical wall East of a given corner

Parameters:
street - the street that the wall will cross
avenue - the avenue that will be just west of the new wall segment

placeWallNorthOf

public void placeWallNorthOf(int street,
                             int avenue)
Place a one segment horizontal wall North of a given corner

Parameters:
street - the street that will be just south of the new wall segment
avenue - the avenue that the wall will cross

readWorld

public final void readWorld(java.lang.String filename)
Read a world from a file in the current directory. It must be a legal world file

Parameters:
filename - the name of the file in the current directory to read

readWorld

public final void readWorld(java.lang.String directoryPath,
                            java.lang.String filename)
Read a world from a file in the given directory path.

Parameters:
directoryPath - a full path to the file containing the world
filename - the nameof the file to read

rereadLastWorld

public void rereadLastWorld()
Read in the most recently read world file if any


resumeThreads

public static final void resumeThreads()
Resume all the threads after stopping them.


saveWorld

public final void saveWorld(java.lang.String filename)
Save a world to a file in the current directory.

Parameters:
filename - the name of the file to write

saveWorld

public final void saveWorld(java.lang.String directoryPath,
                            java.lang.String filename)
Save the world in a file in the given directory path.

Parameters:
directoryPath - a full path to the directory in which to save the file
filename - the name of the file to be saved

scaleImage

protected static void scaleImage(greenfoot.GreenfootImage image,
                                 int divide)
The image files are 40 pixels on a side. This is used to scale them to fit worlds with a user determined cellSize. Currently divide is 1 for everything except beepers, where it is a 2. If you use your own images they should likely also be 40 pixels and should be scaled so that they fit in a cell. If they don't fit in a cell they will "overlap" with robots in adjacent cells.

Parameters:
image - the image to scale
divide - the factor to shrink the image by (>= 1).

setDelay

public static void setDelay(int delay)
Set the value of the delay between robot operations. A value near 15 is usually good. But between 0 and 100. In effect it provides a scale value for the speed slider. A value of about 15 (the default) is usually good but it depends on computer speed.


setupThread

public static final void setupThread(java.lang.Runnable runnableObject)
Let a robot run in its own thread. Put its "task" into the run method and pass the robot to this method. Runnable objects added like this can be started and stopped with the speed dialog. Robots can have their speed controlled by it also.

Parameters:
runnableObject - any runnable object, not just a robot

sleep

protected static void sleep()
Pause the operation of robots briefly so that the user can follow the changes visually.


startThreads

public static final void startThreads()
Start all the threads initially. Call this only once in an execution.