java.lang.Object | +--Robot
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 |
public static final int EAST
public static final int NORTH
public static final int SOUTH
public static final int WEST
Constructor Detail |
public Robot()
Robot r = new Robot();
public Robot(int col, int row, int heading, RobotRoom room)
Robot r = new Robot( 1, 3, Robot.NORTH, someRoom );
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 |
public java.awt.Color colorOfTile()
if ( r.colorOfTile() == java.awt.Color.red ) {...
protected void draw(java.awt.Graphics context, int x, int y)
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 centerpublic int heading()
if ( r.heading() == Robot.NORTH ) {...
Robot.NORTH
, Robot.EAST
,
Robot.SOUTH
, or Robot.WEST
.public void move()
r.move();
public boolean okToMove()
if ( r.okToMove() ) {...
public void paint(java.awt.Color newColor)
r.paint( java.awt.Color.yellow );
newColor
- The color the tile changes to.public void setSpeed(int speed)
r.setSpeed( 100 );
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.public void turnLeft()
r.turnLeft();
public void turnRight()
r.turnRight();