Results 1 to 2 of 2
  1. #1
    L67Pontiac is offline Member
    Join Date
    Jul 2007
    Posts
    1
    Rep Power
    0

    Default Need help with becker robots problem please.

    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
    */

    Java Code:
    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;
    	}
    
    }
    Last edited by JavaBean; 07-06-2007 at 07:45 PM. Reason: Codes should be placed inside [code] tag.

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    We need the Robot class. What are the capabilities (methods) of a robot?

    Basically, you will need to do four things at each time step. You can do that in a while loop:

    1. Check if the robot can pick things. If robot can pick things, pick them.
    2. Check if there is a wall in front of the robot. If there is a wall, u-turn.
    3. Select a random direction and move a little bit.
    4. Return to step 1.

Similar Threads

  1. How to control robots and other devices
    By francojava in forum Advanced Java
    Replies: 0
    Last Post: 01-24-2008, 04:03 PM
  2. becker.robots problem.
    By jesse in forum New To Java
    Replies: 1
    Last Post: 11-26-2007, 10:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •