Results 1 to 2 of 2
  1. #1
    Hector is offline Member
    Join Date
    Feb 2014
    Location
    Seattle, Washington
    Posts
    9
    Rep Power
    0

    Default GridWorld BinaryBug Issues

    This is the program i had assigned:
    The way this bug will work is that each time that it steps towards a direction it will be considered a 0 in a string of binary numbers. Whenever it runs into another bug or flower it is going to hop over the bug and that will add a 1 to the string on numbers. If there are multiple objects in a row that need to be hopped over, the binary bug will have the ability to do so. When the binary bug reaches the end of a bounded grid, or reaches a rock, it will display the string of the binary number. It will then turn to a direction in which it is free to move and start over. If the bug runs into an object that it needs to jump over, but the jump would put it off the grid, then it will also finish its path and convert the binary digits to decimal.

    What i am having trouble with is when my bug is in a bounded grid and enters the 9th column, the program freezes. i tried to fix this by making an else if statement to tell it to turn when in the ninth column, but it still doesn't work. i also want to make sure it turns 45 degrees instead of 90 in one turn. also, when the bug has to make two "jumps" in a row to avoid its obstacles(not when there are two obstacles next to each other and it has to jump over both), it also freezes. after i get these things to work, i feel like i will have a solid foundation to go off of.

    BinaryBug class:
    Java Code:
    import info.gridworld.grid.Grid;
    import info.gridworld.grid.Location;
    
    import java.awt.Color;
    import java.util.ArrayList;
    
    import info.gridworld.actor.Actor;
    import info.gridworld.actor.Bug;
    
    public class BinaryBug extends Bug{
    	 private int steps;
         private int sideLength;
         ArrayList<Location> getOccupiedLocations;
    	
         
    public BinaryBug(int length)
         {
             sideLength = length;
         }
     /**
      * Moves to the next location of the square.
      */
    public void act()
    {
    	Grid<Actor> gr = getGrid();
        Location loc = getLocation();
        Location next = loc.getAdjacentLocation(getDirection());
        int r;
    	int c;
    	r = loc.getRow();
    	c = loc.getCol();
    	System.out.println(r+", "+c);
            if (canMove()){
                move();
            }
            else{
            while(canMove() == false){
            	
            	
    			if(getDirection() == 90 && c < 8){				
    				Location newLoc = new Location(r, c + 2);
    				moveTo(newLoc);
    			}
    			else if(getDirection() == 90 && c == 9){
    				turn();
    			}
    			
    			
    			if(getDirection() == 180){
    				Location newLoc = new Location(r + 2, c);
    				moveTo(newLoc);
    			}
    			if(getDirection() == 270){
    				Location newLoc = new Location(r, c - 2);
    				moveTo(newLoc);
    			}
    			if(getDirection() == 360){
    				Location newLoc = new Location(r - 2, c);
    				moveTo(newLoc);
    			}}
    }}}
    BinaryBug runner:
    Java Code:
    import info.gridworld.actor.ActorWorld;
    import info.gridworld.actor.Bug;
    import info.gridworld.grid.Location;
    
    public class BinaryBugRunner {
    
    	public static void main(String[] args) {
    		ActorWorld world = new ActorWorld();
            BinaryBug x = new BinaryBug(2);
            Bug y = new Bug();
            world.add(new Location(0, 1), x);
            world.add(new Location(3,5), y);
            x.setDirection(90);
            world.show();
    
    	}
    
    }
    Thanks in advance for any help!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: GridWorld BinaryBug Issues

    It's very hard to tell what the problem is since I am unfamilar with the supporting third party code. However, I did notice this:

    Java Code:
    if(getDirection() == 90 && c < 8){
           Location newLoc = new Location(r, c + 2);
          moveTo(newLoc);
    } else if(getDirection() == 90 && c == 9){
           turn();
    }
    In the above code, you don't seem to handle when c == 8.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. GridWorld BinaryBug Program Help
    By Hector in forum New To Java
    Replies: 1
    Last Post: 04-10-2014, 05:33 PM
  2. Help with Critter in Gridworld?
    By Estrex in forum New To Java
    Replies: 0
    Last Post: 03-28-2013, 10:54 AM
  3. URGENT: Quick rundown on GridWorld?
    By Green_Ghoul in forum New To Java
    Replies: 2
    Last Post: 05-03-2011, 03:00 AM
  4. help with gridworld?
    By Samirjthe2nd in forum New To Java
    Replies: 1
    Last Post: 04-28-2011, 11:10 PM
  5. Need gridworld help
    By robertbob in forum New To Java
    Replies: 2
    Last Post: 05-03-2010, 06:18 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
  •