Hi, I've been having trouble with this problem. I can't seem to make the robot move for very long or go many places, but I need him to go everywhere and pick everything up. All help would be greatly appreciated.
/**
Main Task: Pick up all the things inside the wall.
Note: The robot does not know where all the things are.
nor does it know if there is anthing obstructing its way.
Use selection, iteration, and sequence to create this algorithm.
Notes:
1. The robot should be able to pick up everything within the walls
2. The size of the walls and the number of objects WILL be different
when running the program
4. It is possible to have several Things on the same space
3. Please do not change the name of the class
*/
import becker.robots.*;
import java.math.*;
import java.awt.Rectangle;
import java.awt.Color;
import java.awt.geom.Rectangle2D;
public class Problem
{
public static void main(String[] arg)
{
// Initialize the setup randomly
int height = 0, width = 0, top = 1, left = 1, thingCount = 5, startx = 0, starty = 0;
height = (int)(Math.random() * 5) + 3;
width = (int)(Math.random() * 5) + 3;
startx = (int)(Math.random() * width) + left;
starty = (int)(Math.random() * height) + top;
City newYork = new City();
Robot guy = new Robot(newYork, starty, startx, Direction.EAST);
Wall[] cityWalls = makeWalls(newYork, width, height, top, left);
Thing[] things = fillWalls(newYork, width, height, top, left, thingCount);
// Your code goes here
while(guy.frontIsClear())
{
guy.move();
}
while(guy.canPickThing())
{
guy.pickThing();
}
guy.turnLeft();
while (guy.frontIsClear())
{
guy.move();
while (guy.canPickThing())
{
guy.pickThing();
uTurn(guy);
}
}
uTurn(guy);
}
private static void faceDirection(Robot r, Direction dir)
{
while(r.getDirection() != dir)
{
r.turnLeft();
}
}
private static void uTurn(Robot r)
{
for (int i = 0; i < 2; i++)
{
r.turnLeft();
}
}
private static Wall[] makeWalls(City city, int width, int height, int top, int left){
Wall[] walls = new Wall[width * 2 + height * 2];
int i = 0;
int index = 0;
// create top and bottom
for (i = 0; i < width; i++){
walls[i] = new Wall(city, top, left + i, Direction.NORTH);
walls[i + width] = new Wall(city, top + height - 1, left + i, Direction.SOUTH);
}
index = i + width;
// create sides
for (i = 0; i < height; i++){
walls[index + i] = new Wall(city, top + i, left, Direction.WEST);
walls[index + i + height] = new Wall(city, top + i, left + width - 1, Direction.EAST);
}
return walls;
}
private static Thing[] fillWalls(City city, int width, int height, int top, int left, int objects){
Thing things[] = new Thing[objects];
int avenue = 0, street = 0;
for (int i = 0; i < objects; i++){
avenue = (int)(Math.random() * width) + left;
street = (int)(Math.random() * height) + top;
things[i] = new Thing(city, street, avenue);
}
return things;
}
}