Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This is my first post here in programmers.stackexchange (I'm a regular on SO). I hope this isn't too general.

I'm trying a simple project to learn Java from something I've seen done in the past. Basically, it's an AI simulation where there are herbivorous and carnivorous creatures and both must try to survive. The part I am trying to come up with is that of the board itself.

Let's assume very simple rules. The board must be of size X by Y and only one element can be in one place at one time. For example, a critter cannot be in the same tile as a food block. There can be obstacles (rocks, trees..), there can be food, there can be critters of any type. Assuming these rules, what would be one good way to represent this situation ?

This is what I came up with and want suggestions if possible:

Use multiple levels of inheritance to represent all the different possible objects (AbstractObject -> (NonMovingObject -> (Food, Obstacle) , MovingObject -> Critter -> (Carnivorous, Herbivorous))) and use polymorphism in a 2D array to store the instances and still have access to lower level methods.

Many thanks.

Edit: Here is the graphic representation of the structure I have in mind.

enter image description here

share|improve this question
    
Is the simulation agent based or automata based? –  World Engineer Oct 8 '12 at 22:20
    
I'm sorry I'm afraid I'm not sure I know what you mean. Do you want to know if movement will be decided by the user vs with an algorithm ? I plan to try and make it possible to have an interface and make it possible to inherit from either Carnivorous or Herbivorous to define a new type of critter with a new AI algorithm. I am replicating a small contest that was held on a programming forum I used to go. Basically, everyone could write his own version of the code as either Carnivorous or Herbivorous and simulations were run to test which was the best. –  Alexandre P. Levasseur Oct 8 '12 at 23:08
    
My guess: "automata based" refers to cellular automata, where each location is represented by a finite state machine; whereas the state in an "agent based" would be represented by per-object datastructures. (Your question sounds more like "agent based", but I suppose it could be considered ambiguous because your locations are exclusive and can contain only one entity) –  comingstorm Oct 8 '12 at 23:50
    
I was referring to cellular automata. Namely that if you were going that route, you are massively over-complicating. Agent-based is a different story. –  World Engineer Oct 9 '12 at 0:40
add comment

1 Answer

Your proposal sounds reasonable, but:

  • beware of creating unnecessary levels of hierarchy
  • consider using a set of interfaces instead of a class hierarchy
  • if any of your operations require sorting out generic objects based on their dynamic subclass (e.g., by object casting or by using instanceof), you're almost certainly doing the wrong thing.

Specifically, I'd guess that resolving what happens when one object moves into a currently occupied space could be a conceptual problem, because it depends on the types of both objects. But (per the third point), don't try to solve it by dynamically determining the type of each and looking them up case by case using switch, if, or some kind of table! That "solution" is fragile, tedious, and doesn't scale well.

Instead, try basing your system on the interfaces needed to resolve your objects' behavior. For the above example, try providing your generic simulation object a set of "response" handling methods, such as handleIntrusion(intruder). If this handler needs more information about another object, it can call appropriate methods on that object, such as isVegetable() and perhaps handleBeingEaten(eater). (I suppose there's a fine line between using and abusing predicates like isVegetable(), but they are still preferable to dynamic casting operations...)

You should be able to find a reasonably small set of operations that will allow your simulation objects to interact with each other. A well-designed set of interfaces will allow you to modify and extend your simulation without reworking everything in it each time.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.