kareltherobot
Class ur_Robot

kareltherobot.ur_Robot
All Implemented Interfaces:
java.lang.Runnable

public class ur_Robot
implements java.lang.Runnable

This is the root of the robot hierarchy. All robots automatically extend this class unless another is stated. All of the methods here may be overridden. A few of the methods are advanced and discussed only late in the book. The basic functionality used throughout is discussed in Chapter 2 (move, turnLeft, pickBeeper, putBeeper, turnOff). To get robots moving simultaneously you need to use threads and the run method as discussed in Chapter 8. Most of the others are for advanced concurrent robot operation similar to CSP (Communicating Sequential Processes) and implement message channels between pairs of robots.


Inner Class Summary
static interface ur_Robot.ConnectStrategy
          Used so that tailored communication strategies may be developed.
 
Constructor Summary
ur_Robot(int street, int avenue, kareltherobot.Direction direction, int beepers)
          Create and display a new ur_Robot on Street street, at Avenue avenue, facing Direction, with beeprs in Beeperbag.
 
Method Summary
 void acceptConnection(java.io.PipedOutputStream s, ur_Robot.ConnectStrategy strat)
           
 void acceptConnectionFrom(ur_Robot sender, java.io.PipedOutputStream s, ur_Robot.ConnectStrategy strat)
          Executed when another robot connects to the receiver.
 java.io.BufferedWriter connectTo(ur_Robot other, ur_Robot.ConnectStrategy strat)
          Robot connects to another robot and is listed in that other robot's sender list.
 java.lang.String getNextCommunication()
          Robot immediately gets the next available communication.
 void move()
          Robot moves one block forward if front is not blocked.
 void pickBeeper()
          If robot is on a corner with a beeper it picks up one beeper.
 void putBeeper()
          If robot has a beeper in its beeperbag it puts one on the current corner.
 void run()
          All robots implement runnable.
 void send(java.io.BufferedWriter other, java.lang.String s)
          Sends communication s to robot other.
 void showState(java.lang.String s)
          Write a trace to standard output of the robot's state (position, direction...)
protected  void sleep()
           
 java.lang.String toString()
           
 void turnLeft()
          Robot turns left 90 degrees
 void turnOff()
          Robot turns off and will accept no more instructions.
 java.lang.String waitForCommunication()
          Robot waits if necessary for the next incoming communication.
 java.lang.String waitForNextCommunication()
          Robot waits if necessary for incoming communication from the next sender.
Ê

Constructor Detail

ur_Robot

public ur_Robot(int street,
                int avenue,
                kareltherobot.Direction direction,
                int beepers)
Create and display a new ur_Robot on Street street, at Avenue avenue, facing Direction, with beeprs in Beeperbag.
Parameters:
street - The street on which the new robot will be created.
avenue - The avenue on which the new robot will be created.
direction - The direction in which it will face: North, South, East, or West from the Directions interface.
beepers - Initial number of beepers in beeper bag (can be infinity).
Method Detail

turnLeft

public void turnLeft()
Robot turns left 90 degrees

move

public void move()
Robot moves one block forward if front is not blocked. It generates an error shutoff if its front is blocked.

pickBeeper

public void pickBeeper()
If robot is on a corner with a beeper it picks up one beeper. Otherwise it generates an error shutoff.

putBeeper

public void putBeeper()
If robot has a beeper in its beeperbag it puts one on the current corner. Otherwise it generates an error shutoff.

turnOff

public void turnOff()
Robot turns off and will accept no more instructions.

getNextCommunication

public java.lang.String getNextCommunication()
Robot immediately gets the next available communication. This never blocks, but may return null. This is used only in Chapter 11.
 
Returns:
The communication string.
 

In the future this may be generalized to return an arbitrary Object.


waitForCommunication

public java.lang.String waitForCommunication()
Robot waits if necessary for the next incoming communication. It blocks until someone sends it a message. This is used only in Chapter 11.
Returns:
The communication string. Returns null if no one is known as a sender.
 

In the future this may be generalized to return an arbitrary Object.


waitForNextCommunication

public java.lang.String waitForNextCommunication()
Robot waits if necessary for incoming communication from the next sender. It blocks until that sender sends it a message. This is used only in Chapter 11.
Returns:
The communication string. Returns null if no one is known as a sender.
 

In the future this may be generalized to return an arbitrary Object.


connectTo

public java.io.BufferedWriter connectTo(ur_Robot other,
                                        ur_Robot.ConnectStrategy strat)
                                 throws java.io.IOException
Robot connects to another robot and is listed in that other robot's sender list. It may then send to that robot. If the second parameter is null a default strategy will be used for communication. This is used only in Chapter 11.
Parameters:
other - The robot to connect to.
strat - The communication strategy to use in the communication.
Returns:
The Writer that can be used to communicate with other.

acceptConnectionFrom

public void acceptConnectionFrom(ur_Robot sender,
                                 java.io.PipedOutputStream s,
                                 ur_Robot.ConnectStrategy strat)
                          throws java.io.IOException
Executed when another robot connects to the receiver. If strat is null the sender is just added to the sender list. If it is not null, the action method of the strategy is executed instead. This is used only in Chapter 11.
Parameters:
sender - The robot that is connecting.
s - The pipe overwhich the robots can communicate.
strat - The communication strategy.

acceptConnection

public void acceptConnection(java.io.PipedOutputStream s,
                             ur_Robot.ConnectStrategy strat)
                      throws java.io.IOException
This is experimental and unused.

send

public void send(java.io.BufferedWriter other,
                 java.lang.String s)
          throws java.io.IOException
Sends communication s to robot other. This is used only in Chapter 11.
Parameters:
other - The robot that this one is trying to communicate with.
s - The communication.
 

In the future this may be generalized to permit an arbitrary Object to be sent.


run

public void run()
All robots implement runnable. The default method here does nothing. To use this create a new thread with an overridden run method for your robot. See Chapter 8.
 
Specified by:
run in interface java.lang.Runnable

toString

public final java.lang.String toString()

sleep

protected void sleep()
See Chpater 8.

showState

public final void showState(java.lang.String s)
Write a trace to standard output of the robot's state (position, direction...)