Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualsooner123

Posted 16 December 2012 - 07:47 AM

I want to write a simplistic predator-prey simulation in which predators and prey move about on a grid, avoiding/devouring each other.

For the first iteration, I want an Entity base class, a Creature class which inherits from Entity. (later on a Food class which inherits from Entity as well), and Predator and Prey classes which inherit from Entity.

Predator and Prey will move about on the grid randomly (for now) and if a predator happens to stumble on a prey's square, said prey gets deleted and said predator's fitness is augmented.

I'm having some trouble coming up with a good design for the logic of everything.

Basically my first idea was to have 'grid' be a 2d array of pointers to type Entity and 'entities' be an array of pointers to type Entity. This way I can loop through the grid to output the state to the screen and loop through entities to perform their actions AND have access to the state of a specific square via 'grid' in order to determine the result of a member of 'entities' moving to its target square.

This seems redundant and runs into the following issue: what if two predators or two prey occupy the same square. An element of 'grid' is a pointer to only one Entity. Is the solution to make 'grid' be a 2d array of pointers to type ListOfEntities? I can't decide how to handle this issue because the other solution seems to be to have Entities being aware of their x,y location, and then when I'm outputting the grid, I need to scan through every entity, for every square, in order to determine where everything is. And in the logic of predator/prey moving around, I have to scan through every entity to see which entities are at the target square.

Also my thinking is that an Entity should not be aware of its x and y location and that that logic should be handled fully by the indexing of the 'grid' array. Does this seem like good design of the data structures?

#1sooner123

Posted 16 December 2012 - 07:43 AM

I want to write a simplistic predator-prey simulation in which predators and prey move about on a grid, avoiding/devouring each other.

For the first iteration, I want an Entity base class, a Creature class which inherits from Entity. (later on a Food class which inherits from Entity as well), and Predator and Prey classes which inherit from Entity.

Predator and Prey will move about on the grid randomly (for now) and if a predator happens to stumble on a prey's square, said prey gets deleted and said predator's fitness is augmented.

I'm having some trouble coming up with a good design for the logic of everything.

Basically my first idea was to have 'grid' be a 2d array of pointers to type Entity and 'entities' be an array of pointers to type Entity. This way I can loop through the grid to output the state to the screen and loop through entities to perform their actions AND have access to the state of a specific square via 'grid' in order to determine the result of a member of 'entities' moving to its target square.

This seems redundant and runs into the following issue: what if two predators or two prey occupy the same square. An element of 'grid' is a pointer to only one Entity. Is the solution to make 'grid' be a 2d array of pointers to type ListOfEntities?

Also my thinking is that an Entity should not be aware of its x and y location and that that logic should be handled fully by the indexing of the 'grid' array. Does this seem like good design of the data structures?

PARTNERS