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.
4
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.