alice.kareltherobot
Class UrRobot

java.lang.Object
  extended by KoalaRobot
      extended by alice.kareltherobot.UrRobot
Direct Known Subclasses:
Robot

public class UrRobot
extends KoalaRobot

A UrRobot can carry out the five basic operations: move, turnLeft, pickBeeper, putBeeper, and turnOff. Extend this class to solve some interesting problems. If you click on a robot in the world, it will tell you about its current state.

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

Constructor Summary
UrRobot(int street, int avenue, Direction direction)
          Constructs a robot at a given corner facing a given direction, with no beepers.
UrRobot(int street, int avenue, Direction direction, int beepers)
          Create a robot on a specific street and avenue, facing a given direction with a given number of beepers.
UrRobot(int street, int avenue, Direction direction, int beepers, Color color)
          Create a robot on a specific street and avenue, facing a given direction with a given number of beepers and a given head color.
 
Method Summary
 void assertFacingEast()
          Assert that the robot is facing east.
 void assertFacingNorth()
          Assert that the robot is facing north.
 void assertFacingSouth()
          Assert that the robot is facing south.
 void assertFacingWest()
          Assert that the robot is facing west.
 void assertFrontIsBlocked()
          Assert that the robot's front is blocked.
 void assertFrontIsClear()
          Assert that the robot's front is clear.
 void assertHasBeepers()
          Assert that the robot as at least one beeper in its beeper bag Throw an exception otherwise
 void assertHasNoBeepers()
          Assert that the robot's beeper bag is empty Throw an exception otherwise
 void assertNextToABeeper()
          Assert that the robot's corner has at least one beeper Throw an exception otherwise
 void assertNextToARobot()
          Assert that the robot's corner has at least one other robot Throw an exception otherwise
 void assertOnAvenue(int avenue)
          Assert that the robot is on a given avenue.
 void assertOnStreet(int street)
          Assert that the robot is on a given street.
 java.lang.String currentState()
          Return the state of this robot
 AbstractCamera getCamera()
          Get a reference to a camera from the world of this robot
 KarelWorld getWorld()
          Get the world to which this object has been added (but only after you add it).
 void move()
          The robot will move to the next intersection in the direction it is facing, provided that its front is not blocked.
 void pickBeeper()
          The robot will pick exactly one beeper from the current corner provided there is at least one there to pick.
 void putBeeper()
          The robot will put exactly one beeper on the current corner provided that it has at least one in its beeper bag.
 void setSpeed(double newSpeed)
          Set the speed with which this robot operates.
 void setTracing(boolean trace)
          Turn tracing on or off.
 void turnLeft()
          The robot will turn 90 degrees to the left (counter-clockwise) from its current direction.
 void turnOff()
          The robot will turn off.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UrRobot

public UrRobot(int street,
               int avenue,
               Direction direction)
Constructs a robot at a given corner facing a given direction, with no beepers.

Parameters:
street - the street of the robot (>= 1)
avenue - the avenue of the robot (>= 1)
direction - the original direction that the robot faces

UrRobot

public UrRobot(int street,
               int avenue,
               Direction direction,
               int beepers)
Create a robot on a specific street and avenue, facing a given direction with a given number of beepers.

Parameters:
street - the street on which to place the robot (>= 1)
avenue - the avenue on which to place the robot (>= 1)
direction - the direction the robot will initially face
beepers - the initial number of beepers in the beeper bag

UrRobot

public UrRobot(int street,
               int avenue,
               Direction direction,
               int beepers,
               Color color)
Create a robot on a specific street and avenue, facing a given direction with a given number of beepers and a given head color.

Parameters:
street - the street on which to place the robot (>= 1)
avenue - the avenue on which to place the robot (>= 1)
direction - the direction the robot will initially face
beepers - the initial number of beepers in the beeper bag
color - the color of this robot's head
Method Detail

setSpeed

public void setSpeed(double newSpeed)
Set the speed with which this robot operates. The default is 1.0 and it takes about a second to move one block. A value < 1 will slow down the robot. A large value will speed it up. When a robot is added to a world, it gets the default speed of that world. For this to be effective, invoke it after adding the robot to the world.

Parameters:
newSpeed - the new speed >0.0 and <= 100.0

getWorld

public KarelWorld getWorld()
Get the world to which this object has been added (but only after you add it).


turnOff

public void turnOff()
The robot will turn off. After it turns off, it will no longer respond to the UrRobot commands.


getCamera

public AbstractCamera getCamera()
Get a reference to a camera from the world of this robot

Returns:
a camera (if any) in this world.

setTracing

public void setTracing(boolean trace)
Turn tracing on or off. When tracing is on (argument = true) the robot will print its state to standard output for each action. The action shown is the most recent one completed.

Parameters:
trace - true to turn tracing on.

move

public void move()
The robot will move to the next intersection in the direction it is facing, provided that its front is not blocked. If it is blocked it will generate an error and turn off. If it is not running it does nothing.


turnLeft

public void turnLeft()
The robot will turn 90 degrees to the left (counter-clockwise) from its current direction. If it is not running it does nothing.

If timing is important to your simulation, note that a robot can turn completely around (four turnLeft instructions) in about the same time as it can move one block.


pickBeeper

public void pickBeeper()
The robot will pick exactly one beeper from the current corner provided there is at least one there to pick. If there are no beepers on the corner, an error will be issued and the robot will turn off. If it is not running it does nothing.


putBeeper

public void putBeeper()
The robot will put exactly one beeper on the current corner provided that it has at least one in its beeper bag. If it has no beepers an error will be generated and the robot will turn off. If it is not running it does nothing.


currentState

public java.lang.String currentState()
Return the state of this robot

Returns:
a String giving the state of the robot.

assertFacingEast

public void assertFacingEast()
Assert that the robot is facing east. Throw an exception otherwise

Throws:
java.lang.RuntimeException - if the robot is not facing east

assertFacingNorth

public void assertFacingNorth()
Assert that the robot is facing north. Throw an exception otherwise

Throws:
java.lang.RuntimeException - if the robot is not facing north

assertFacingSouth

public void assertFacingSouth()
Assert that the robot is facing south. Throw an exception otherwise

Throws:
java.lang.RuntimeException - if the robot is not facing south

assertFacingWest

public void assertFacingWest()
Assert that the robot is facing west. Throw an exception otherwise

Throws:
java.lang.RuntimeException - if the robot is not facing west

assertFrontIsClear

public void assertFrontIsClear()
Assert that the robot's front is clear. Throw an exception otherwise

Throws:
java.lang.RuntimeException - if the robot's front is blocked by a wall

assertFrontIsBlocked

public void assertFrontIsBlocked()
Assert that the robot's front is blocked. Throw an exception otherwise

Throws:
java.lang.RuntimeException - if the robot's front is clear

assertHasBeepers

public void assertHasBeepers()
Assert that the robot as at least one beeper in its beeper bag Throw an exception otherwise

Throws:
java.lang.RuntimeException - if the robot's beeper bag is empty

assertHasNoBeepers

public void assertHasNoBeepers()
Assert that the robot's beeper bag is empty Throw an exception otherwise

Throws:
java.lang.RuntimeException - if the robot has any beepers in its beeper bag

assertOnAvenue

public void assertOnAvenue(int avenue)
Assert that the robot is on a given avenue. Throw an exception otherwise

Parameters:
avenue - the avenue the robot is supposed to be on
Throws:
java.lang.RuntimeException - if the robot is not on the given avenue

assertOnStreet

public void assertOnStreet(int street)
Assert that the robot is on a given street. Throw an exception otherwise

Parameters:
street - the street the robot is supposed to be on
Throws:
java.lang.RuntimeException - if the robot is not on the given street

assertNextToABeeper

public void assertNextToABeeper()
Assert that the robot's corner has at least one beeper Throw an exception otherwise

Throws:
java.lang.RuntimeException - if the robot's corner has no beepers

assertNextToARobot

public void assertNextToARobot()
Assert that the robot's corner has at least one other robot Throw an exception otherwise

Throws:
java.lang.RuntimeException - if the robot's corner has no other robots