Anyone know this year's GridWorld code? I need help on making a class.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Energizer100
    New Member
    • Nov 2007
    • 60

    Anyone know this year's GridWorld code? I need help on making a class.

    Code:
    public class fly extends Actor
    {
    	public fly()
    	{
    		setColor(Color.BLACK);
    	}
    	
    	public fly(Color flyColor)
    	{
    		setColor(flyColor);
    	}
    	
    	public void act()
    	{
    		if (canMove())
    		{
    			move();
    		}
    		else
    		{
    			turn();
    		}
    	}
    	
    	public void move()
    	{
    		if (getGrid() == null)
    		{
    			return;
    		}
    		ArrayList<Actors> actors = getActors();
    		processActors(actors);
    		ArrayList<Location> moveLocs = getMoveLocations();
    		Location loc = selectMoveLocation(moveLocs);
    		makeMove(loc);
    	}
    	
    	public ArrayList<Actor> getActors()
    	{
    		return getGrid().getOccupiedLocations(getLocation());
    	}
    	
    	public void processActors(ArrayList<Actor> actors)
    	{
    		for (Actor a : actors)
    		{
    			if (!(a instanceof Rock) && !(a instanceof Critter) && !(a instanceof fly) && !(a instanceof Bug))
    			{
    				a.removeSelfFromGrid();
    			}
    		}
    	}
    	
    	public ArrayList<Location> getMoveLocations()
    	{
    		return getGrid().get //NEED HELP HERE!
    	}
    	
    	public Location selectMoveLocation(ArrayList<Location> locs)
    	{
    		int n = locs.size();
    		if (n == 0)
    		{
    			return getLocation();
    		}
    		int r = (int)(Math.random() * n);
    		return locs.get(r);
    	}
    	
    	public void makeMove(Location loc)
    	{
    		if (loc == null)
    		{
    			removeSelfFromGrid();
    		}
    		else
    		{
    			moveTo(loc);
    		}
    	}    
    }
    The above code is my idea for a fly class that moves around a bounded grid randomly. I cannot "eat" rocks, bugs, critters, and other flies. My problem is in the getMoveLocation s method. I want it to be able to move randomly to any spot. But there is no method in the actor or grid class that says getValidMoveLoc ations. I think you need the getOccupiedLoca tion and subtract it from the grid? It's just an idea though.

    Thanks
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    I don't know what GridWorld is; are you talking about this one?

    kind regards,

    Jos

    Comment

    • Energizer100
      New Member
      • Nov 2007
      • 60

      #3
      Originally posted by JosAH
      I don't know what GridWorld is; are you talking about this one?

      kind regards,

      Jos

      Yeah it is. I basically have to create a new type of Actor.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Energizer100
        Yeah it is. I basically have to create a new type of Actor.
        I skimmed a bit through the API documentation for the Grid and found the method
        getEmptyAdjacen tLocations(Loca tion loc). That should be a starting point.

        kind regards,

        Jos

        Comment

        • Energizer100
          New Member
          • Nov 2007
          • 60

          #5
          Originally posted by JosAH
          I skimmed a bit through the API documentation for the Grid and found the method
          getEmptyAdjacen tLocations(Loca tion loc). That should be a starting point.

          kind regards,

          Jos

          Yeah, my idea for it was to make a new ArrayList.

          So it would be something like,

          Code:
          ArrayList<Actor> valLocs = getGrid - getOccupiedLocations.
          something along those lines

          Comment

          Working...