Class Robot

java.lang.Object
  |
  +--Robot

public class Robot
extends java.lang.Object

Represents simple simulated robots. These robots occupy rooms with walls and tiled floors. Robots walk around on the floors (one tile at a time), changing and sensing the colors of tiles if they wish. But robots can't move through walls, thus they have to stay in their room. Robots can face in any of four directions, which are described as compass directions in analogy to a map: north is towards the top of the robot's room as drawn on a computer screen, east is towards the right, etc.


Field Summary
static int EAST
          The heading a robot has when facing east.
static int NORTH
          The heading a robot has when facing north.
static int SOUTH
          The heading a robot has when facing south.
static int WEST
          The heading a robot has when facing west.
 
Constructor Summary
Robot()
          Initialize a robot and a room for it to occupy.
Robot(int col, int row, int heading, RobotRoom room)
          Initialize a robot from its position, orientation, and room.
 
Method Summary
 java.awt.Color colorOfTile()
          Find out what color the tile under a robot is.
protected  void draw(java.awt.Graphics context, int x, int y)
          Draw a robot.
 int heading()
          Find out what direction a robot is facing.
 void move()
          Move a robot one tile forward, unless the way is blocked by a wall or other robot.
 boolean okToMove()
          Find out whether a robot can move forward.
 void paint(java.awt.Color newColor)
          Change the color of the tile under a robot.
 void setSpeed(int speed)
          Set the speed at which a robot moves and turns.
 void turnLeft()
          Turn a robot 90 degrees to its left (i.e., counterclockwise about its center).
 void turnRight()
          Turn a robot 90 degrees to its right (i.e., clockwise about its center).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EAST

public static final int EAST
The heading a robot has when facing east.

See Also:
Constant Field Values

NORTH

public static final int NORTH
The heading a robot has when facing north.

See Also:
Constant Field Values

SOUTH

public static final int SOUTH
The heading a robot has when facing south.

See Also:
Constant Field Values

WEST

public static final int WEST
The heading a robot has when facing west.

See Also:
Constant Field Values
Constructor Detail

Robot

public Robot()
Initialize a robot and a room for it to occupy. The room will be a default room, and this robot will be its only occupant. The robot will have the default position and heading (center of the room, facing north). For example

Robot r = new Robot();


Robot

public Robot(int col,
             int row,
             int heading,
             RobotRoom room)
Initialize a robot from its position, orientation, and room. For example

Robot r = new Robot( 1, 3, Robot.NORTH, someRoom );

Parameters:
col - The horizontal coordinate of the tile this robot is on (i.e., the tile column the robot is in). This must be within the range of columns for the room the robot is in, and, with the row parameter, must specify an unobstructed tile.
row - The vertical coordinate of the tile this robot is on (i.e., the tile row the robot is in). This must be within the range of rows for the room the robot is in, and, with the column parameter, must specify an unobstructed tile.
heading - The robot's heading. This should be one of the four headings defined for robots.
room - The room the robot is in.
Method Detail

colorOfTile

public java.awt.Color colorOfTile()
Find out what color the tile under a robot is. For example

if ( r.colorOfTile() == java.awt.Color.red ) {...

Returns:
The color of the tile under the robot.

draw

protected void draw(java.awt.Graphics context,
                    int x,
                    int y)
Draw a robot.

Parameters:
context - The graphics context in which to draw.
x - The horizontal coordinate at which to place the robot's center.
y - The vertical coordinate at which to place the robot's center

heading

public int heading()
Find out what direction a robot is facing. For example

if ( r.heading() == Robot.NORTH ) {...

Returns:
The robot's direction, encoded as one of the values Robot.NORTH, Robot.EAST, Robot.SOUTH, or Robot.WEST.

move

public void move()
Move a robot one tile forward, unless the way is blocked by a wall or other robot. If the way is blocked, the robot will flash briefly to indicate that it can't move. For example

r.move();


okToMove

public boolean okToMove()
Find out whether a robot can move forward. A robot can move whenever the tile in front of it doesn't contain a wall or another robot. For example

if ( r.okToMove() ) {...

Returns:
True if the robot can move, false if moving would cause the robot to collide with a wall or another robot.

paint

public void paint(java.awt.Color newColor)
Change the color of the tile under a robot. For example

r.paint( java.awt.Color.yellow );

Parameters:
newColor - The color the tile changes to.

setSpeed

public void setSpeed(int speed)
Set the speed at which a robot moves and turns. For example

r.setSpeed( 100 );

Parameters:
speed - An integer between 1 and 1000, which indicates approximately how many moves or turns per second the robot should do. Values below 1 or above 1000 are treated as if they were 1 and 1000, respectively.

turnLeft

public void turnLeft()
Turn a robot 90 degrees to its left (i.e., counterclockwise about its center). For example

r.turnLeft();


turnRight

public void turnRight()
Turn a robot 90 degrees to its right (i.e., clockwise about its center). For example

r.turnRight();