2 Replies - 10558 Views - Last Post: 19 April 2012 - 11:12 AM Rate Topic: -----

#1 boredsomeone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-March 12

Gridworld - MotherBug

Posted 23 March 2012 - 01:08 AM

In my AP computer science class, we were all tasked to design our own custom bug as a final project in Gridworld. After a day, we were asked what kind of bug we were making, and I chose to make a "Mother Bug".

Mother bug would have a bug appear every 1-3 moves behind it, and it would behave as a normal bug (move straight, turn at walls/rocks, leave flowers behind). The mother bug would eat flowers in front of it, eat rocks in front of it, and turn at walls.....It would also eat other bugs (This includes it's kids). Only doing that to get a kick out of the teacher.

The reason I chose this is, in all honesty, I don't know how to program...at all really. The fact that I'm passing the class is a miracle. I chose this because I believed I would be able to take the code from other, completed bugs (a normal bug spawning flowers, crabs eating flowers, etc)

But, after (very shortly) copy and pasting, fixing a few things I knew how to fix, I'm still getting errors. I only have tomorrow to finish working on the bug, and I need a passing grade on this to keep my grade in the class from dropping below a C.


In short, I would like help identifying the current error I'm getting on this bug. I have yet to try much (adding some imports, fixed a few things).

I'm guessing my issue is I'm trying to bring in methods from the critter project, meaning some terms aren't defined or something. I'll be adding the methods to have the bug remove adjacent bugs and rocks from the grid.

The current issue is in the canMove() method. About 4 or 5 errors (unable to find symbol if I remember correctly) pop up through the method (2 in line 44, I can't remember the rest off the top of my head).

 
import info.gridworld.actor.Bug;
import info.gridworld.actor.ActorWorld;
import info.gridworld.grid.Location;
import info.gridworld.actor.Rock;
import info.gridworld.actor.Actor;
import java.util.ArrayList;
import java.awt.Color;

public class MotherBug extends Bug
{

	 public void act()
    {
        if (canMove())
            move();
        else
            turn();
    }


    public void turn()
    {
        setDirection(getDirection() + Location.HALF_RIGHT);
    }


	public void move()
    {
        Grid<Actor> gr = getGrid();
        if (gr == null)
            return;
        Location loc = getLocation();
        Location next = loc.getAdjacentLocation(getDirection());
        if (gr.isValid(next))
            moveTo(next);
        else
            removeSelfFromGrid();
        Bug child = new Bug(getColor());
        bug.putSelfInGrid(gr, loc);
    }

    public boolean canMove()
    {
        Grid<Actor> gr = getGrid();
        if (gr == null)
            return false;
        Location loc = getLocation();
        Location next = loc.getAdjacentLocation(getDirection());
        if (!gr.isValid(next))
            return false;
        Actor neighbor = gr.get(next);
        return (neighbor == null) || (neighbor instanceof Flower);

    }

    public void processActors(ArrayList<Actor> actors)
	{
		for (Actor a : actors)
		{
			if (a instanceof Rock)
				a.removeSelfFromGrid();
		}
	}
} 



Any tips or pointers would be appreciated. If I can't copy and paste or anything, I'll appreciate knowing that as well. I'll try to post again while I'm in class tomorrow (to get the exact errors and what not) if I can get help here. I'll ask around class, and worst case scenario just have the new bug spawn other bugs.

If someone could also tell me which method of CrabCritter I can use, that would be amazing, but I don't think I'll need it (at most, just the imports).

Thank you for your time.

Is This A Good Question/Topic? 0
  • +

Replies To: Gridworld - MotherBug

#2 boredsomeone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-March 12

Re: Gridworld - MotherBug

Posted 23 March 2012 - 10:55 AM

Fixed the errors I was getting. Hit a new wall.

When I tried putting MotherBug into the grid (via BoxBugRunner), it seems something with the runner broke (?). When I run it, it doesn't show any bugs (even the others that USED to work), the grid is unclickable, and trying to run while implementing MotherBug, causes Java to give me a "unable to find symbol" error.

I tried making a new runner (MotherRunner), but when I run that (no errors either!), no bug shows and the grid is again unclickable.

import info.gridworld.actor.Bug;
import info.gridworld.actor.ActorWorld;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;
import info.gridworld.actor.Rock;
import info.gridworld.actor.Actor;
import java.util.ArrayList;
import java.awt.Color;

public class MotherBug extends Bug

{

	 public void act()
    {
        if (canMove())
            move();
        else
            turn();
    }


    public void turn()
    {
        setDirection(getDirection() + Location.HALF_RIGHT);
    }


    public void move()
    {
        Grid<Actor> gr = getGrid();
        if (gr == null)
            return;
        Location loc = getLocation();
        Location next = loc.getAdjacentLocation(getDirection());
        if (gr.isValid(next))
            moveTo(next);
        else
            removeSelfFromGrid();
        Bug baby = new Bug(getColor());
        baby.putSelfInGrid(gr, loc);
    }

    public boolean canMove()
    {
        Grid<Actor> gr = getGrid();
        if (gr == null)
            return false;
        Location loc = getLocation();
        Location next = loc.getAdjacentLocation(getDirection());
        if (!gr.isValid(next))
            return false;
        Actor neighbor = gr.get(next);
        return (neighbor == null) || (neighbor instanceof Bug);

    }

    public void processActors(ArrayList<Actor> actors)
	{
		for (Actor a : actors)
		{
			if (a instanceof Rock)
				a.removeSelfFromGrid();
		}
	}
}



Mother Runenr
import info.gridworld.actor.ActorWorld;
import info.gridworld.grid.Location;
import info.gridworld.grid.Grid;
import java.awt.Color;

/**
 * This class runs a world that contains box bugs. <br />
 * This class is not tested on the AP CS A and AB exams.
 */
public class MotherRunner
{
    public static void main(String[] args)
    {
        ActorWorld world = new ActorWorld();

  	    MotherBug mom = new MotherBug();
        mom.setColor(Color.BLUE);

        world.show();
    }

}



Thank you for any help.
Was This Post Helpful? 0
  • +
  • -

#3 welshej  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 82
  • Joined: 18-November 11

Re: Gridworld - MotherBug

Posted 19 April 2012 - 11:12 AM

Try adding, after setting your color... I don't know why your grid is unclickable, but this should at least solve your other errors:


world.add(new Location(2, 2), mom);

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1