10 Replies - 13579 Views - Last Post: 16 November 2010 - 03:30 PM Rate Topic: -----

#1 Freakzoyd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-October 10

AaaantFaarm

Posted 14 November 2010 - 07:25 PM

Hello guys, I'm in APCS and right now we are working with a gridworld study case called the ant farm. I had a lot of difficulties trying to finish it, I don't really understand what I have to do sometimes so I will apreciate if someone would (not make the code) guide me through it, as the only 2 guys that finish it on my class are selfish wount helps us and neither my teacher, who is not even prepared for giving that class. I know it might be really easy for you guys to complete this, but as english is my second language, it's difficult to make the tex into code translation. The methods and everything are already made, I just need someone that explains me what to do from the part I am. Here is the link of the study case:

http://ljhs.sandi.ne...nments/AntFarm/

Right now I'm on the worker ant class after getDesiredDirecton method, I was able to do that part, but I don't undertad what to do next.

This is the code I have so far:
Worker Ant Class
/**
 * WorkerAnt.java  05/10/07
 *
 * @author - Jane Doe
 * @author - Period n
 * @author - Id nnnnnnn
 *
 * @author - I received help from ...
 *
 */

import info.gridworld.actor.Actor;
import info.gridworld.actor.Critter;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;

import java.awt.Color;
import java.util.ArrayList;

/**
 * A <code>WorkerAnt</code> is a critter who's mission is
 * to take food from <code>Cake and Cookie</code> objects and to
 * deliver it to a <code>QueenAnt</code> object.
 * Initially it looks for food.  After it finds food, it looks for
 * a queen.
 * Worker ants share the location of food and the queen
 * with other ants they encounter.
 * Worker ants with food are red.  If they don't have food,
 * they are black.
 */
public class WorkerAnt extends Critter implements Processable
{
	/** Current amount of food being carried */


	/** Location of a <code>Food</code> object */


	/** Location of a <code>QueenAnt</code> object */


    /**
     * Constructs a <code>WorkerAnt</code> critter.
     * It is originally black (no food) and
     * its direction is chosen randomly from the
     * eight normal cardinal directions.
     */
    public WorkerAnt()
    {
    	setColor(Color.black);
    	int num=(int) (Math.random()*8);
    	if (num==0){
    		setDirection(getDirection()+Location.NORTH);
    	}
    	else if (num==1){
    		setDirection(getDirection()+Location.EAST);
    	}
    	else if (num==2){
    		setDirection(getDirection()+Location.SOUTH);
    	}
    	else if (num==3){
    		setDirection(getDirection()+Location.WEST);
    	}
    	else if (num==4){
    		setDirection(getDirection()+Location.SOUTHEAST);
    	}
    	else if (num==5){
    		setDirection(getDirection()+Location.NORTHEAST);
    	}
    	else if (num==6){
    		setDirection(getDirection()+Location.SOUTHWEST);
    	}
    	else{
    		setDirection(getDirection()+Location.NORTHWEST);
    	}
    }

	/**
	 * Gives current food and queen locations to <code>ant</code>.
	 * @param ant the calling <code>WorkerAnt</code>
	 */
    public void process(WorkerAnt ant)
    {
    	ant.shareFoodLocation(getLocation());
    	ant.shareQueenLocation(getLocation());
    }

	/**
	 * Takes <code>fQty</code> amount of food from the
	 * calling <code>Food</code>.
	 * @param fQty the amount of food to take.
	 */
    private int foodQuantity;
    public void takeFood(int fQty)
    {
    	foodQuantity+=fQty;
    }

	/**
	 * Gives food to the calling <code>QueenAnt</code>.
	 * @return the amound of food to give.
	 */
    public int giveFood()
    {
		return foodQuantity;
    }

	/**
	 * Receives the <code>fLoc</code> food location from a
	 * <code>Food</code> object.  Saves this location if
	 * it doesn't already have one.
	 * @param fLoc the location of the food.
	 */
    private Location foodLoc;
    public void shareFoodLocation(Location fLoc)
    {
    	if (getLocation()==null){
    		foodLoc=fLoc;
    	}
    	else {
    		foodLoc=getLocation();
    	}
    }

	/**
	 * Receives the <code>qLoc</code> queen location from a
	 * <code>QueenAnt</code> object.  Saves this location if
	 * it doesn't already have one.
	 * @param qLoc the location of the queen.
	 */
    private Location queenLoc;
    public void shareQueenLocation(Location qLoc)
    {
    	if (getLocation()==null){
    		queenLoc=qLoc;
    	}
    	else {
    		queenLoc=getLocation();
    	}
    }

    /**
     * Processes each of the neighboring Ant Farm actors.
     * Implemented to get food from <code>Cake and Cookie</code> actors,
     * give food to <code>QueenAnt</code> actors, and to share locations
     * with other <code>WorkerAnt</code> actors.<br />
     * Precondition: All objects in <code>actors</code>
     * are contained in the same grid as this critter.
     * @param actors the actors to be processed
     */
    @Override
    public void processActors(ArrayList<Actor> actors)
    {
    	
    }

    /**
     * Gets the possible locations for the next move.
     * Implemented to return the empty neighboring locations
     * that are roughly in the direction of the current goal
     * (food or queen).  Calles getDesiredDirection to get the
     * direction to the goal.  Then it considers locations which
     * are in that direction or +- Location.HALF_RIGHT degrees.<br />
     * Postcondition: The locations must be valid in the grid
     * of this critter.
     * @return a list of possible locations for the next move
     */
    @Override
    public ArrayList<Location> getMoveLocations()
    {
		return null;	// Replace with appropriate implementation !
    }

    /**
     * Moves this critter to the given location, sets its direction,
     * and sets its color (red = has food, black = does not have food).
     * Implemented to call moveTo.<br />
     * Precondition: <code>loc</code> is valid in the grid of this critter
     * @param loc the location to move to (must be valid)
     */
    @Override
    public void makeMove(Location loc)
    {
    	
    }

	/**
	 * Returns the direction that the ant wants to go.
	 * @return the direction to the queen (if there is food
	 * and a queen's location is known); the direction to the
	 * food (if there is no food and a food's location is known);
	 * the current ant's direction otherwise.
	 */
    private int getDesiredDirecton()
    {
    	int directT=getDirection();
		if (queenLoc!=null && foodQuantity!=0){
			directT=queenLoc.getDirectionToward(getLocation());
		}
		else if (foodLoc!=null && foodQuantity==0){
			directT=foodLoc.getDirectionToward(getLocation());
		}
		return directT;
	}

    /**
     * Creates a string that describes this actor.
     * @return a string with the <code>Actor</code> information
     * plus the current amount of food and any known
     * <code>Food</code> and <code>QueenAnt</code> locations.
     */
    @Override
	public String toString()
	{
		return null;	// Replace with appropriate implementation !
	}
}


Cookie Class
/**
 * Cookie.java  05/10/07
 *
 * @author - Jane Doe
 * @author - Period n
 * @author - Id nnnnnnn
 *
 * @author - I received help from ...
 *
 */

/**
 * A <code>Cookie</code> sits around and waits for a
 * <code>WorkerAnt</code> to get food from it.
 * It does not act.
 */
public class Cookie extends Food
{
	/** The size of a bite */


    /**
     * Constructs a <code>Cookie</code> actor.  The Cookie.gif is
     * displayed for this actor without tinting.
     */
	private static final int BITE=1;
    public Cookie()
    {
    	super(BITE);
    }
}


Food Class
/**
 * Food.java  05/10/07
 *
 * @author - Jane Doe
 * @author - Period n
 * @author - Id nnnnnnn
 *
 * @author - I received help from ...
 *
 */

import info.gridworld.actor.Actor;

/**
 * <code>Food</code> is the top level abstract class for different
 * types of food.  A food object waits for a
 * <code>WorkerAnt</code> to get food from it.  It does not act.
 */
public abstract class Food extends Actor implements Processable
{
	/** The size of a bite */


	/** The total amount of food eaten */


    /**
     * Constructs a <code>Food</code> actor.  The image is
     * displayed for this actor without tinting.
     */
	private final int BITE_SIZE;
	private int foodEaten;
	public Food(int bite)
	{
		BITE_SIZE=bite;
		foodEaten=0;
		setColor(null);
	}
	/**
	 * Gives food and current location to <code>ant</code>.
	 * @param ant the calling <code>WorkerAnt</code>
	 */
	public void process(WorkerAnt ant)
	{
		ant.takeFood(BITE_SIZE);
		ant.shareFoodLocation(getLocation());

	}

    /**
     * "Do nothing" act method.  This is provided for the Ant Farm
     * <code>Cake, Cookie, and QueenAnt</code> classes which do not act.
     */
    @Override
    public void act()
    {
    }

    /**
     * Creates a string that describes this actor.
     * @return a string with the <code>Actor</code> information
     * plus the amount of food eaten from this <code>Cake</code>
     */
    @Override
	public String toString()
	{
    	return super.toString() +", BSize=" + BITE_SIZE+", FEaten="+foodEaten;
	}
}

Queen Ant Clas
/**
 * Processable.java  05/10/07
 *
 * @author - Jane Doe
 * @author - Period n
 * @author - Id nnnnnnn
 *
 * @author - I received help from ...
 *
 */

import info.gridworld.actor.Actor;

import java.awt.Color;

/**
 * A <code>QueenAnt</code> sits around and waits for a
 * <code>WorkerAnt</code> to bring it food.
 * It does not act.
 */
public class QueenAnt extends Actor implements Processable
{
	/** Total amount of food that the queen has received. */


    /**
     * Constructs a <code>QueenAnt</code> actor.
     * The queen is colored magenta.
     */
	private int foodQty;
    public QueenAnt()
    {
    	foodQty=0;
    	setColor(Color.magenta);
    	
    }

	/**
	 * Gets food from and gives current location to <code>ant</code>.
	 * @param ant the calling <code>WorkerAnt</code>
	 */
	public void process(WorkerAnt ant)
	{
		ant.giveFood();
		ant.shareQueenLocation(getLocation());
	}

    /**
     * "Do nothing" act method.  This is provided for the Ant Farm
     * <code>Cake, Cookie, and QueenAnt</code> classes which do not act.
     */
    @Override
    public void act()
    {
    }

    /**
     * Creates a string that describes this actor.
     * @return a string with the <code>Actor</code> information
     * plus the amount of food given to this <code>QueenAnt</code>
     */
    @Override
	public String toString()
	{
    	return super.toString() +", FQty=" + foodQty;
	}
}


Processable
/**
 * Processable.java  05/10/07
 *
 * @author - Jane Doe
 * @author - Period n
 * @author - Id nnnnnnn
 *
 * @author - I received help from ...
 *
 */

/**
 * Provides the process method that allows <code>WorkerAnt</code>
 * actors to polymorphically get food from the <code>Food</code> objects
 * (<code>Cake</code> and <code>Cookie</code>) and give food to
 * <code>QueenAnt</code> objects.  This method also enables the
 * various actors to share their locations.
 */
public interface Processable
{
	/**
	 * Called by <code>ant</code> to get/give food and to
	 * get/give food and queen locations.
	 * @param ant the calling <code>WorkerAnt</code>
	 */
}


Ant Farm Runner
/**
 * AntFarmRunner.java  05/10/07
 *
 * @author - Robert Glen Martin
 * @author - School for the Talented and Gifted
 * @author - Dallas ISD
 *
 * Ant Farm GridWorld project:
 * Copyright(c) 2007 Robert Glen Martin
 * (http://www.martin.apluscomputerscience.com)
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

import info.gridworld.actor.Actor;
import info.gridworld.actor.ActorWorld;
import info.gridworld.grid.Grid;
import info.gridworld.grid.BoundedGrid;
import info.gridworld.grid.Location;

/**
 * This class runs a world that contains Ant Farm actors.
 * A cookie, a cake, and a queen are added at specific locations
 * in a 19x19 grid.  25 worker ants are added at random locations.
 */
public class AntFarmRunner
{
	public static void main(String[] args)
	{
		Grid<Actor> grid = new BoundedGrid<Actor>(19, 19);
        ActorWorld world = new ActorWorld(grid);

        world.add(new Location(2, 2), new Cookie());
        world.add(new Location(17, 2), new Cake());
        world.add(new Location(9, 15), new QueenAnt());

        for (int i = 0; i < 25; i++)
        	world.add(new WorkerAnt());
        world.show();
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: AaaantFaarm

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10694
  • View blog
  • Posts: 39,805
  • Joined: 27-December 08

Re: AaaantFaarm

Posted 14 November 2010 - 09:19 PM

It seems like you are supposed to define these methods as described in the assignment and comments in the code. They seem really specific to me, so I don't have any general expansion. Do you have a specific question regarding one or more of these methods?
Was This Post Helpful? 0
  • +
  • -

#3 Freakzoyd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-October 10

Re: AaaantFaarm

Posted 14 November 2010 - 09:42 PM

This part:

processActors processes each of the neighboring ant farm actors. This method should be very short. It needs a loop to traverse (loop through) the actors ArrayList. An enhanced for loop works well for this. Each actor in actors needs to call its process method. The parameter for each call will be this, the reference to the worker ant executing the processActors method.

How should I make the for loop?
Was This Post Helpful? 0
  • +
  • -

#4 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: AaaantFaarm

Posted 14 November 2010 - 09:49 PM

An enhanced For loop has the form:
for(ObjectType variableName : arrayName)
{}

//for example, to traverse an arraylist of actors
for(Actor a : actorsArray)
{
  // the a is your variable so you can use it for whatver
  // e.g. calls the toString() method of the actor and prints the result
  System.out.println(a);
}


This post has been edited by bcranger: 14 November 2010 - 09:50 PM

Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10694
  • View blog
  • Posts: 39,805
  • Joined: 27-December 08

Re: AaaantFaarm

Posted 14 November 2010 - 09:55 PM

To iterate through any array or Collection, you want to make sure you do not exceed the length-1 (size()-1 with Collections) element, as they are indexed from 0 through length-1. So if I have a List named myList, I can iterate through it by doing:
for(int i = 0; i < myList.size(); i++)



Your assignment did mention the enhanced for loop as an option. This loop iterates through an array or Collection, examining each element exactly once. bcranger showed you the basic syntax, though note that it is not just limited to arrays.
Was This Post Helpful? 0
  • +
  • -

#6 Freakzoyd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-October 10

Re: AaaantFaarm

Posted 15 November 2010 - 05:06 PM

Ok I got my enhanced for loop:

for (Actor a: actors){
    		actors.process(this);
    	}



It's wrong, I don't understand this part:

"Each actor in actors needs to call its process method. The parameter for each call will be this, the reference to the worker ant executing the processActors method."

How can I use the variable of the loop to acces in each actor and then use the process method?
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10694
  • View blog
  • Posts: 39,805
  • Joined: 27-December 08

Re: AaaantFaarm

Posted 15 November 2010 - 05:10 PM

Invoke process from a rather than actors. Keep in mind that actors is the List, and Lists don't have a process() method.
a.process(this);


Was This Post Helpful? 0
  • +
  • -

#8 Freakzoyd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-October 10

Re: AaaantFaarm

Posted 15 November 2010 - 05:14 PM

Thx it worked, it just need type casting but the IDE(Eclipse), helped me with that.

Thx it worked, it just need type casting but the IDE(Eclipse), helped me with that.
Was This Post Helpful? 0
  • +
  • -

#9 Freakzoyd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-October 10

Re: AaaantFaarm

Posted 15 November 2010 - 05:31 PM

Ok so im working on the getMoveLocations, it's different from the ProcessActors because it used the ArrayList name also as the Method name, this is what it has to be done
1. getMoveLocations does the following:
a. Calls the private getDesiredDirecton method to get the general direction the ant wants to move.
b. Creates a list with up to three adjacent locations that are in the general direction of the one returned by getDesiredDirection. Locations are included if they meet all of the following criteria. They must be:
i. Adjacent to the current location.
ii. In the desired direction, or 45 degrees to the left of the desired direction, or 45 degrees to the right of the desired direction.
iii. Valid (in the grid).
iv. Empty.
c. Returns the list of locations.
Replace the getMoveLocations body as discussed above. For part b, use Location 's HALF_LEFT and HALF_RIGHT constants and the getAdjacentLocation method. You will want to use Grid's isValid to see if a given location is valid (is in the grid) and get to help see if the location is empty (get returns null).

It doesn't say I have to use a loop to go through the ArrayList but I used it, I just have part 1 but It mark me an arror

public ArrayList<Location> getMoveLocations()
    {
    	for (Location b: getMoveLocations()){
    		b.getDesiredLocation();
    	}
    }


Oh no, I understand everything now, so I have to add 3 locations into the ArrayList isnt it?

Ok so, I tried to add the first Location and is marked as an error:

getMoveLocations.add(getDesiredDirection);

Was This Post Helpful? 0
  • +
  • -

#10 Freakzoyd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-October 10

Re: AaaantFaarm

Posted 15 November 2010 - 06:27 PM

I notice that my bigger problem is that i don't read enough to understand the problem. So what I really have to do is to get 3 adjacent locations. But I can just return them if they acomplish some characteristics. I already made 3 Location variables that get the 3 adjacent locations, and check if they could be added to the ArraList (locations). Here is the code I have:

Grid grid= new Grid();
    	ArrayList<Location> locations=new ArrayList<Location>();
    	Location loc=getLocation();
    	Location loc1=loc.getAdjacentLocation(getDesiredDirection());
    	Location loc2=loc.getAdjacentLocation(Location.HALF_LEFT);
    	Location loc3=loc.getAdjacentLocation(Location.HALF_RIGHT);
    	if (grid.isValid(loc1) && grid.get(loc1)==null){
    		locations.add(loc1);
    	}
    	if (grid.isValid(loc2) && grid.get(loc2)==null){
    		locations.add(loc2);
    	}
    	if (grid.isValid(loc3) && grid.get(loc3)==null){
    		locations.add(loc3);
    	}
    	return locations;


The only problem is that it mark me as an error when I made the Grid object, it says it can't be instantiated. Please help, I now I have been annoying but it's the last thing I need.
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10694
  • View blog
  • Posts: 39,805
  • Joined: 27-December 08

Re: AaaantFaarm

Posted 16 November 2010 - 03:30 PM

All Actors have a getGrid() method, which returns a Grid<Actor>. You may also want to use the Grid getEmptyAdjacentLocations() method, which accepts a Location param and returns an ArrayList<Location> for empty Locations.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1