2D grid with multiple types of objects

Posted by Alexandre P. Levasseur on Programmers See other posts from Programmers or by Alexandre P. Levasseur
Published on 2012-10-08T21:55:05Z Indexed on 2012/10/09 3:50 UTC
Read the original article Hit count: 797

Filed under:
|

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

© Programmers or respective owner

Related posts about java

Related posts about data-structures