UCL Logo

Robot Program

The robot program allows you to write programs to guide a robot around a room, similar to the examples given in the lectures and the problem class questions (but note there are some differences).

The robot program consists of a small number of classes, which you need to copy and compile (this also gives an example of how to work with a program consisting of more than one class). To do this follow these steps:

1. Create a new directory to put your robot programs in. Change to that directory.

2. Copy each of the classes below into a file in your new directory, using the correct file name:

Remember that the file name must be of the form <classname>.java and the upper/lower case letters must match the class name. You can do the copying by creating a new file from BlueJ or JEdit with the correct name and then copying and pasting the code from the web page. Don't forget to save the file once pasted.

3. Compile the program.

From an xterm window compile all the files using:

javac *.java

If you get errors, check that you copied the source code correctly. Delete files with errors and try again if you can't spot the error.

If you have configured JEdit to compile classes then click your compile button, otherwise use the xterm window command line.

If you are using BlueJ either click the compile button in the editor window for each class, or right-click on each class icon and select compile, or use the Tools>Compile menu option to compile all files.

4. Run the program from the command line using:

java MyRobot

This will move the robot around so you can see that the program works.

If you have configured JEdit to run programs then click the run button, otherwise use the xterm window command line.

From BlueJ right-click on the MyRobot icon and select void main(args) to run the program. When the Method Call dialog box appears just click OK. Alternatively, right-click and select new MyRobot(), and click OK on the Create Object dialog box. Then right-click on the red MyRobot icon and select run from the menu.

The Robot program will run without problems on your own PC/Mac if you have Java installed. On a PC use a DOS (Command Prompt) window in place on an xterm window. On a Mac use a Terminal window in place of an xterm (or use an xterm if you have the X-Window server installed).


Writing your own robot programs

Class MyRobot is a template to show how to write robot programs. For each robot program you write, make a copy of this file and save it under a new name, e.g., AnotherRobot.java. In your new file, change all occurrences of the class name MyRobot to your class name (e.g., AnotherRobot).

Read the comments in the file (or your copy of it) to see where to modify the code.

The room coordinate system starts at (0.0) in the top left corner. The wall around the room is not part of the room.

Note, invalid use of the robot or room may cause strange things to happen - you need to get the details correct!

Robot Methods

A robot has the following methods:

  • robot.forward() - move forward one square.
  • robot.left() - turn left.
  • robot.right() - turn right.
  • robot.done() - change robot colour to green to indicate your program has finished.
  • robot.canMoveForward() - returns true if the robot can move forward.
  • robot.atDoor() - returns true if the robot is next to the door (in any direction).
  • robot.setSpeed(seconds) - sets the speed of the robot (the pause between each movement). Try 0.5 for faster movement.
  • robot.atWidget() - return true if robot is at the same location as a widget.
  • robot.pickUpWidget() - pick up a widget if the robot is on top of one.
  • robot.dropWidget() - drop a widget if the robot is carrying one.

Room Methods

Directions are specified using:

  • Room.NORTH
  • Room.SOUTH
  • Room.EAST
  • Room.WEST

The room has the following methods:

  • room.setDoorPosition(wall,postion) - set the position of the door in a wall (e.g., room.setDoorPosition(Room.NORTH,5) - in north wall 5 from the left).
  • room.setObstacle(x,y) - set an obstacle at (x,y) position. The robot cannot occupy the same location as an obstacle and obstacles cannot be removed. (Remember the room coordinate system works from (0,0) in the top left corner of the room.)
  • room.setWidget(x,y) - place a widget at (x,y) position. The robot can move onto a location containing a widget and optionally pick it up.
Last updated: August 31, 2006

Computer Science Department - University College London - Gower Street - London - WC1E 6BT - Telephone: +44 (0)20 7679 7214 - Copyright 1999-2006 UCL


 Search by Google