1

I am currently working on an agents project that involves controlling the movement of various agents (dinosaurs in this case). Each "turn" all of the dinosaurs will determine what 'food' source they need to eat and where they need to move to get it. I already have the methods to determine what it is going to move towards next-

public Object wut2Eat(ArrayList<Dinosaur> dinosaurs, ArrayList<Grass> grasses, ArrayList<Water> waters) { //0=dino 1=grass for food type if(foodStorage >= waterStorage) { //i need 2 eets da foodies if(isHerbivore()) { //i eet grassies return closestGrass(grasses); } else { //i eet dinoes return closestDinosaur(dinosaurs); } } else { //i'm a thirsty mofo return closestWater(waters); } } 

The three methods closestDinosaur/Grass/Water each look exactly like the following:

public Water closestWater(ArrayList<Water> waters) { Water closestWater = null; double distance; double minDistance = MAX_VALUE; for (Water water : waters) { distance = calculateDistance(this.getxLoc(), this.getyLoc(), water.getxLoc(), water.getyLoc()); if(distance < minDistance) { closestWater = water; minDistance = Double.min(minDistance, distance); } } return closestWater; } 

Now I have to determine which direction the current dinosaur has to move, and based off of that where it will move to.

public void move(ArrayList<Dinosaur> dinosaurs, ArrayList<Grass> grasses, ArrayList<Water> waters) { Object food = wut2Eat(dinosaurs, grasses, waters); } 

Each of the three objects (Dinosaur, Grass, Water) have an xLoc and yLoc that determine their position on a field. How do I get those values so that I can determine where the current dinosaur should move? I know that whatever wut2Eat returns is going to be an object with xLoc and yLoc variables with the setters and getters to go along with it. Should I put wut2Eat inside of move?

2
  • You will get a better answer if you clarify your question. Commented Mar 19, 2018 at 1:38
  • Where do I need to clarify? @EvanWeissburg Commented Mar 19, 2018 at 1:42

1 Answer 1

1

You need a base type, or perhaps an interface which your other types can implement.

interface Positioned { int getX(); int getY(); } class Water implements Positioned { int getX() { return xLoc; } ... } 

Perhaps an Edible implements Positioned interface, etc.

Then, wut2Eat() could return an Edible instead of an Object.


Here is a more concrete example (Java8)

interface Positioned { int getX(); int getY(); default int distanceTo(Positioned pos) { int dx = pos.getX() - getX(); int dy = pos.getY() - getY(); return (int) Math.sqrt(dx*dx + dy*dy); } } interface Edible extends Positioned { int getNutritionalValue(); } interface Agent extends Positioned { Edible wat2Eat(); default Edible closestFood(List<Edible> food) { // code to find closest edible food in given list } } abstract class AbstractFood implements Edible, Positioned { int locX, locY; int getX() { return locX; } // ... etc ... } class Water extends AbstractFood { // ... } class Dinosaur extends AbstractFood, implements Agent { Edible wat2Eat() { if ( hungry ) { if (isHerbivore()) return closestFood(grasses); else return closestFood(dinosaurs); else return closestFood(waters); // ... } 

No need to repeat code in closestGrass() / closestDinosaur() / closestWater().

Adapt as required.

Sign up to request clarification or add additional context in comments.

6 Comments

So should I make Dinosaur/Grass/Water all extend an Edible object that has xLoc and yLoc with setters/getters and then have wut2Eat() return an Edible?
Yup. Pretty much what I was suggesting. At least, wrt to the getters. Positioned should not have setters, although another interface Positionable which extends Positioned could. Water and Grass would just extend the Positioned variant, where as Dinosaur would be Positionable because it can move. Is this a school project or a fun project?
Is the interface necessary if you have parent class Edible that has the variables and getters/setters for xLoc and yLoc?
You can have a base class Edible, certainly. Can you have items which have a position which are not editable? If so, then you'd need a Positioned base class, Edible could extend Positioned, and Water, Grass and Dinosaur could all extend Edible. That is a reasonable first attempt at a class heirarchy. The "is-a" test shows some awkwardness. If you ask whether a Dinosaur is-a Edible, it becomes apparent that the names aren't the best. Adjectives, like "Edible" are better for interfaces. You might want Location and Food instead of Positioned and Edible. But Food has-a Location, not is one
You might consider making class Carnivore extends Dinosaur and class Herbivore extends Dinosaur. Your Dinosaur could decide whether it is hungry or thirsty, and call findFood() or findWater(). The two derived classes would, of course, have differing implementations for findFood()
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.