|
Sample Code: GraphBot.java
package examples;
import becker.robots.*;
/** A GraphBot creates a "graph" with Thing objects. The "data" are piles
of things, all on consecutive intersections on a street. The graph is
constructed by spreading them out on consecutive intersections on the
same avenue, one thing per intersection. The GraphBot is assumed to
start on the north-most pile of things, facing EAST, with an empty
backpack. */
public class GraphBot extends Robot
{
/** Construct a new graphing robot. */
public GraphBot(City aCity, int anAvenue, int aStreet, int aDirection)
{ super(aCity, anAvenue, aStreet, aDirection);
}
/** Make the graph. */
public void makeGraph()
{ while(this.isBesideThing())
{ this.makeBar();
this.moveToNextDataPile();
}
}
/** Display the data by putting a thing on each intersection until we
run out of things. */
private void makeBar()
{ this.getData();
int barLength = this.countThingsInBackpack();
this.putData(barLength);
this.returnToBeginningOfBar(barLength);
}
/** Get the data for this bar of the graph by picking up all the things on this intersection. */
private void getData()
{ while(this.isBesideThing())
{ this.pickThing();
}
}
/** Put the data down to form a bar.
@param barLength the length of this bar */
private void putData(int barLength)
{ this.move();
for(int i=0; i<barLength-1; i++)
{ this.putThing();
this.move();
}
this.putThing();
}
/** Return to the beginning of the bar.
@param barLength the length of this bar */
private void returnToBeginningOfBar(int barLength)
{ this.turnAround();
for(int i=0; i<barLength; i++)
{ this.move();
}
}
/** Move to the next data pile. */
private void moveToNextDataPile()
{ this.turnLeft();
this.move();
this.turnLeft();
}
/** Turn around and face the opposite direction. */
private void turnAround()
{ this.turnLeft();
this.turnLeft();
}
}
Contact: bwbecker@learningwithrobots.com. |