Using The System in Chapter 2

Students studying Chapter 2 of Karel J Robot are creating simple tasks for UrRobots without writing classes or methods. This is a suggestion of how their code will fit comfortably into the new simulator.

There are two parts to the solution. The world class and the program (main). There are several examples of world classes with the distribution. You can copy any of them and edit it to get the desired effect. Here is an example from Chapter 2.

The class will be named CarryBeeperNorthDemonstration so it is in a java file with that name. I've annotated the file with comments below.

package alice.kareluser; /// this is the package that the students use for their code

import org.alice.apis.moveandturn.AbstractCamera;

import alice.kareltherobot.UrRobot;
import alice.kareltherobot.Direction; 
import alice.kareltherobot.KarelWorld; /// These are typical imports from the simulator. 

/**
 * A simmple program in which a robot will pick up a beeper and carry it North (see Chapter 2 of the book). 
 * 
 * @author jbergin
 * 
 */
public class CarryBeeperNorthDemonstration extends KarelWorld /// You must extend the abstract class KarelWorld and provide the two methods setTheStage, and run. 
{

	private UrRobot karel; /// declare one or more UrRobots to carry out the task. 

	/**
	 * Execute the beeper carrying task
	 * 
	 */
	@Override
	public void run() /// This is the method that will be executed in the simulator. It is required.  The robot "task" goes here. 
	{
		karel.move();
		karel.move();
		karel.pickBeeper();
		karel.move();
		karel.turnLeft();
		karel.move();
		karel.move();
		karel.putBeeper();
		karel.move();
		karel.turnOff();
	}

	/**
	 * Set up the robots and other parts of the situation. 
	 * 
	 */
	@Override
	public void setTheStage()
	{
		addDecorations(); /// not essential, but adds some visual "candy"
		karel = new UrRobot(2, 2, Direction.EAST, 0); /// create a new robot
		karel.setTracing(true); /// Set it to print out its actions
		addRobot(karel); /// add it to this world - Don't forget this. It is essential. The constructor isn't enough to place it in the world, though it does know where it will go in the world.
		cameraFollow(karel); /// Tell the camera of the world to follow the robot as it moves.
		readWorld("worlds","fig2-3.kwld"); /// Read in the world file for this task. This just places the beeper in the correct location here. World files are provided for every figure in the book. 

	}

}

That is all we need in the World class. Note that it does not contain a "main". There is really only one main, but you must edit it to let it know about your world so that the setTheStage and run methods get invoked properly.

Here is the canonical Program class. Note that it is invariant except for the one line in the constructor. The file is provided in the distribution, but you must edit it.

package alice.kareluser;

import org.alice.apis.moveandturn.Program;


import alice.kareltherobot.KarelWorld;


/**
 * A framework in which to initialize a robot task. You can copy this file for your own
 * work, or simply edit it.
 * 
 * @author Cay Horstmann (with edits by Joe Bergin)
 * 
 */
public class KarelProgram extends Program
{
	
	private static final long serialVersionUID = 1L;
	private KarelWorld scene;

	/**
	 * Create a world of interest. Edit this for your own worlds. This is the only method
	 * you should need to edit. Create the world object and assign it to the scene variable.
	 * Make sure you send it the setTheStage message. 
	 */
	public KarelProgram()
	{
		// Create an object here from a class that extends KarelWorld.

		scene = new CarryBeeperNorthDemonstration(); /// This is the only line of the KarelProgram class that gets modified for any of the work with this simulator


		// leave the next statement here. It sets up your world. 
		scene.setTheStage();
		//scene.showScene(0, 0, 12, 12);
		
	}

	/**
	 * Tell the program which scene to "run"
	 * 
	 */
	@Override
	protected void initialize()
	{
//		System.out.println(this.getSimulationSpeedFactor());
//		this.setSimulationSpeedFactor(.1);
		setScene(scene);
	}
	
	
	/**
	 * Execute the simulation defined in the scene
	 * 
	 */
	@Override
	protected void run()
	{
		scene.run();
	}

	public static void main(String[] args)
	{
		KarelProgram theProgram = new KarelProgram();
		theProgram.showInJFrame(args, true);
	}
}

Now run KarelProgram.java as you main file and see the simulation. The first time you run it, it will likely fail. See the initial setup instructions on the downloads page.

Last Updated: March 16, 2010