Farm simulation

Started by
4 comments, last by freeworld 13 years ago
I'm trying to create a farm simulation in c++. Right now I'm just trying to implement some basic structure. I want to have pigs and cow classes that inherit from an animal parent class. I also want a map class that has an array of animal object structs which contain a pointer to an animal and a 2d point location for the animal. I'm having trouble with the array portion. Any suggestions? I'll upload some code when I get home from work.
Advertisement
Personally I'd put the location coordinates into the animal class (every animal has one and only one location), and store a vector of smart pointers to the animals (to avoid dealing with memory management). Don't know if that fits into your code though.

Then optimize later if it becomes a problem.
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
Thanks for the suggestion: With implementing the solution this way, in the future, would I be able to make the animals communicate with one another? Right now I want some basic functionality , such as the animals being able to move, and when an animal moves, I dont want it to walk through another animal. I was thinking that when an animal moves it would talk to the map object, ask it if there is an animal in the path of where it wants to move, and if not, the map will move it there.

Also, I'm curious what you mean by smart pointers? I'm kind of a noob, so I'm not sure what you meant.

My biggest problem right now is being to create an array that contains both the children of the "animal" superclass, pigs, and cow. I'm sure it's possible with polymorphism but I haven't quite figure it out yet.

For example, how would I implement this?

Animal *animalArray[10]; [declares the animal array]
animalArray[1] = new Pig; [creates a pig at index 1]
animalArray[2] = new Cow;

for(int i; i < 10; i++)
animalArray.printMe
I want to have pigs and cow classes that inherit from an animal parent class.[/quote]

This is sort of wrong, and it's not your fault for wanting to do this because a lot of learner material gives you examples like this, but it's sort of an incorrect characterization of what you actually want to use OOP for.


Odds are, pig and cows are going to primarily differ in terms of their data but not their internal structure or behaviors, which means that you could represent both quite well with a single Animal class. Consider this:

Let's say your animals have a species, a name, a health score, an age, a buy price, a selling price, and some sprite. And you want pigs and cows. You could have separate pig and cow classes but the data is going to be almost identical across. They might have slightly different behaviors for certain things, but nothing that would need inheritance.

A better way of looking at it is as such: What is going to be data shared for ALL animals of a given species, and which is going to vary on a per-animal basis?

So, all pigs are going to have the same species name, same sprite, and maybe same base statistics, whereas individual pigs will have their own unique health values, monetary worth, etc.

Consider something like: http://en.wikipedia....yweight_pattern

You could do a lightweight implementation of this by having an Animal_SharedData class and an Animal_IndividualData class. Objects of the Animal_IndividualData class could then have a pointer to the Animal_SharedData class so that it could get at the shared data when it needed, while still maintaining its own unique state. At the same time, you won't have unnecessary inheritance and all of the pitfalls that come with it. In other words, you could just have an Animal_IndividualData array or vector and probably wouldn't have to worry about anything getting weird.

Also, I'm curious what you mean by smart pointers? I'm kind of a noob, so I'm not sure what you meant.


I don't have much experience with smart pointers, but I believe I know what one is well enough to explain it.

A smart pointer is a pointer-like object (or struct or something) that is meant to give you some extra functionality that a pointer doesn't provide.

Normal pointers are simply variables that hold the memory address of another variable (they point to that variable by its memory address). C and C++ offer little hand-holding when using pointers, so you have to be responsible when coding with them. Probably the two biggest problems you'll run into using pointers are memory leaks and going out of the bounds of a pointer. These are often the source of extremely cryptic and hard to track down bugs.

A smart pointer can help protect against this. A lot of them use reference counting to see how many active pointers are pointing to a memory address, and will automatically release memory if the reference count drops to zero. While not completely foolproof, it's a great safety net that can help guard against memory leaks.


My biggest problem right now is being to create an array that contains both the children of the "animal" superclass, pigs, and cow. I'm sure it's possible with polymorphism but I haven't quite figure it out yet.

For example, how would I implement this?

Animal *animalArray[10]; [declares the animal array]
animalArray[1] = new Pig; [creates a pig at index 1]
animalArray[2] = new Cow;

for(int i; i < 10; i++)
animalArray.printMe


Yeah, what you have listed should work (although I imagine you'd say animalArray->printMe() since you need to call the function from a pointer. Can't us the . on something that's a pointer). In fact, the C++ example of Polymorphism on Wikipedia is pretty much exactly your scenario.

http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming

However, I would agree with Fox that you can likely just have one animal class that is customized for each animal type, and this would greatly streamline things if you're planning on having tons of different animal types.

Also, I'm curious what you mean by smart pointers? I'm kind of a noob, so I'm not sure what you meant.


google "boost shared/smart pointer". It's a nice way of making sure you won't have any left over memory when your app closes.

Also as far as collision is concerned, I would separate collisions vs the map, and collision vs other animals. The animals would ask the map for a path, but they wouldn't ask the map if they are about to collide with another animal.
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.

This topic is closed to new replies.

Advertisement