alice.kareltherobot
Class KarelWorld

java.lang.Object
  extended by Scene
      extended by alice.kareltherobot.KarelWorld

public abstract class KarelWorld
extends Scene

A basis for creating worlds in which robots may perform tasks.

You must create a subclass and override two methods to use this. Set up all of the walls, beepers, and robots in your setTheStage method and put the task for the robots in the run method.

Version:
1.0 (Alice) February 2010
Author:
Joseph Bergin based on earlier work of Cay Horstmann

Field Summary
static int INFINITE
          Represents infinitely many beepers either in the world or in some robot's beeper bag.
 
Constructor Summary
KarelWorld()
          Performs basic set up of the world.
KarelWorld(GalleryModel groundImage)
          Create a world with a given background image for the ground.
 
Method Summary
 void addComponent(Transformable item)
          If the component is a robot it will set that robot's speed to the default speed of the world.
 void addDecorations()
          Add some decoration to the world.
 UrRobot addRobot(UrRobot karel)
          Add a robot to this world (usually invoked in setTheStage) and give the robot a speed equal to the current default speed of this world.
 void cameraFollow(AbstractCamera camera, Model model)
          Specify an object (in class Model) to be tracked by a camera.
 void cameraFollow(Model model)
          Specify an object (in class Model) to be tracked by the standard camera.
 void cameraStayFocusedOn(int street, int avenue)
          Point the camera at a particular corner and keep it pointed there if the camera moves
 void cameraStayFocusedOn(int street, int avenue, double height)
          Point the camera at a spot above or below a particular corner and keep it pointed there if the camera moves
 void clearBeepers(int street, int avenue)
          Remove all beepers from a given corner
static KarelWorld currentWorld()
          Obtain a reference to the most recently constructed world
 double defaultSpeed()
          Get the current speed of the simulation
 SymmetricPerspectiveCamera getCamera()
          Obtain a reference to the main camera of this world.
 void moveCameraTo(int street, int avenue, double height)
          Move the camera to a point above (or below) a particular corner
 void moveCameraTo(int street, int avenue, double height, double duration)
          Move the camera to a point above (or below) a particular corner
 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 directory given by the path.
 void removeWallEastOf(int street, int avenue)
          Remove any wall segment one half block east of a given corner
 void removeWallNorthOf(int street, int avenue)
          Remove any wall segment one half block north of a given corner
 void reset()
          Remove all walls and beepers from the world.
abstract  void run()
          Override this for the task to be performed in your own world.
 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.
 void say(java.lang.String message)
          Ask the ground image to say something
 void setSpeed(double defaultSpeed)
          Set the default speed of robots created in this world.
abstract  void setTheStage()
          Override this to set up your robots, walls, and beepers for the task to be executed by the run command
 void show2DScene(int minStreet, int minAvenue, int maxStreet, int maxAvenue)
          Show a static but saveable window with the current world components (walls, beepers, and robots) It is useful for printing a visual representation (map) of the world.
 void think(java.lang.String message)
          Ask the ground image to think something
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INFINITE

public static final int INFINITE
Represents infinitely many beepers either in the world or in some robot's beeper bag.

See Also:
Constant Field Values
Constructor Detail

KarelWorld

public KarelWorld()
Performs basic set up of the world. The background is the default plain beige gridded world.


KarelWorld

public KarelWorld(GalleryModel groundImage)
Create a world with a given background image for the ground. The suitable values new objects of any of the classes: DirtGround, GrassyGround, MoonSurface, SandyGround SeaSurface, or SnowyGround. Others might be possible if they are added to Alice 3. Any of these can be imported from:
org.alice.apis.moveandturn.gallery.environments.grounds

If you click on the background, then the world will respond to the following keys
Up-Arrow doubles the speed of the simulation
Down-Arrow halves the speed
Left-Arrow moves the camera back (out) a bit
Right-Arrow moves the camera forward (in) a bit

Parameters:
groundImage - the GaleryModel ground image to use for the background
Method Detail

currentWorld

public static KarelWorld currentWorld()
Obtain a reference to the most recently constructed world

Returns:
the most recently created world

getCamera

public SymmetricPerspectiveCamera getCamera()
Obtain a reference to the main camera of this world. The camera is 2 blocks behind the south boundary wall, across the wall from first avenue from a height of 4 meters (blocks). It is inclined downward (forward) 30 degrees. It looks in direction roughly North-East by North (33.75 degrees East of Karel's North -- not Alice's north) It has a vertical viewing angle of .15.

Returns:
the camera

setSpeed

public void setSpeed(double defaultSpeed)
Set the default speed of robots created in this world. Normally the speed is 1.0. As a side-effect it sets the speed of all currently added robots to this new value.

Parameters:
defaultSpeed - the default speed of robots in this world.

defaultSpeed

public double defaultSpeed()
Get the current speed of the simulation

Returns:
the current speed

show2DScene

public void show2DScene(int minStreet,
                        int minAvenue,
                        int maxStreet,
                        int maxAvenue)
Show a static but saveable window with the current world components (walls, beepers, and robots) It is useful for printing a visual representation (map) of the world. The file menu of the window has a save option (ctrl-S) from which you can choose a filename.

If you use extension ".ps" it will create a PostScript file suitable for printing or translating to a PDF. If you use something like ".gif" it will create a graphic file in that format if possible. (gif, png, jpg, possibly others).

Note that the world is drawn rotated with the origin at the upper left corner of the page, so that north points to the left, and east downward.

This was written by Cay Horstmann with a few minor edits by Joe Bergin. It is useful for producing handouts for students or documenting your work by taking a "snapshot" of the world at the point at which it is invoked.

Parameters:
minStreet - the first (southern-most) street (with a 1 street buffer provided)
minAvenue - the first (western-most) avenue (with a 1 avenue buffer provided)
maxStreet - the last street
maxAvenue - the last avenue

cameraStayFocusedOn

public void cameraStayFocusedOn(int street,
                                int avenue)
Point the camera at a particular corner and keep it pointed there if the camera moves

Parameters:
street - the street at which to point
avenue - the avenue at which to point

cameraStayFocusedOn

public void cameraStayFocusedOn(int street,
                                int avenue,
                                double height)
Point the camera at a spot above or below a particular corner and keep it pointed there if the camera moves

Parameters:
street - the street at which to point
avenue - the avenue at which to point
height - the height of the focus of the camera

moveCameraTo

public void moveCameraTo(int street,
                         int avenue,
                         double height)
Move the camera to a point above (or below) a particular corner

Parameters:
street - the street to move the camera to
avenue - the avenue to move the camera to
height - the new height of the camera

moveCameraTo

public void moveCameraTo(int street,
                         int avenue,
                         double height,
                         double duration)
Move the camera to a point above (or below) a particular corner

Parameters:
street - the street to move the camera to
avenue - the avenue to move the camera to
height - the new height of the camera
duration - the amount of time it takes the camer to move

cameraFollow

public void cameraFollow(AbstractCamera camera,
                         Model model)
Specify an object (in class Model) to be tracked by a camera. This is usually a Robot.

Parameters:
camera - the camera that will track
model - the object that will be tracked

cameraFollow

public void cameraFollow(Model model)
Specify an object (in class Model) to be tracked by the standard camera. This is usually a Robot.

Parameters:
model - the robot that will be tracked

setTheStage

public abstract void setTheStage()
Override this to set up your robots, walls, and beepers for the task to be executed by the run command


run

public abstract void run()
Override this for the task to be performed in your own world.


say

public void say(java.lang.String message)
Ask the ground image to say something

Parameters:
message - what to say

think

public void think(java.lang.String message)
Ask the ground image to think something

Parameters:
message - what to think

addRobot

public final UrRobot addRobot(UrRobot karel)
Add a robot to this world (usually invoked in setTheStage) and give the robot a speed equal to the current default speed of this world.

Parameters:
karel - the robot to be added
Returns:
the robot added

addComponent

public void addComponent(Transformable item)
If the component is a robot it will set that robot's speed to the default speed of the world.

See Also:
org.alice.apis.moveandturn.Composite#addComponent(org.alice.apis.moveandturn.Transformable)

addDecorations

public void addDecorations()
Add some decoration to the world. A compass, etc.


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 directory given by the path. It must be a legal world file

Parameters:
directoryPath - a valid path to the directory containing the file (may be null for current directory).
filename - the name of the file in the current directory to read

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

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)

clearBeepers

public void clearBeepers(int street,
                         int avenue)
Remove all beepers from a given corner

Parameters:
street - the street from which to remove beepers
avenue - the avenue from which to remove beepers

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

removeWallEastOf

public void removeWallEastOf(int street,
                             int avenue)
Remove any wall segment one half block east of a given corner

Parameters:
street - the street that the wall crosses (if any)
avenue - the avenue just west of the wall (if any)

removeWallNorthOf

public void removeWallNorthOf(int street,
                              int avenue)
Remove any wall segment one half block north of a given corner

Parameters:
street - the street just south of the wall (if any)
avenue - the avenue that the wall crosses (if any)

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

reset

public void reset()
Remove all walls and beepers from the world.