I'm just starting to work on a fairly simple game just for programming practice/fun and I'm trying to start doing things in a "proper" and efficient fashion rather than my usual "do whatever is most convenient at the time" method that ends up in a confusing mess.
One thing I'm really unsure about is how to use classes. By that I mean should a class be completely self-sufficient or is it OK for it to rely on external classes? Here's an example of what I'm talking about:
I have a map which has locations that have lots of information I need to access such as terrain. There are a finite number of terrain types while the number of locations is variable and unknown until the program runs.
Remembering the idea that having multiple copies of the same data is really bad (you have to find and change every occurance) I've usually done something like this (pseudo code done badly for simplicity)
Then when I create a location I figure out which terrain it is and assign that terrain's id to the location and later reference it like
Though this seems kind of long winded and prone to typos
I considered just having all the terrain data be part of the location class but there are so many repeats of the same information that it would waste a lot of memory (ok, not really significant compared to modern amount of RAM) and if I wanted to change something about the terrain I'd have to change it in a large number of places.
I also thought about encapsulating the terrain class inside the location class but that doesn't seem very different.
At the same time, when terrain is external it seems like Location isn't stand alone
What's a good way of handing this kind of situation? I'm open to entirely different ideas from this - although I've been programming for decades I've never really be "expert" in any way. Its entirely likely I'm barking up the wrong tree.
Thanks for any help or insights
One thing I'm really unsure about is how to use classes. By that I mean should a class be completely self-sufficient or is it OK for it to rely on external classes? Here's an example of what I'm talking about:
I have a map which has locations that have lots of information I need to access such as terrain. There are a finite number of terrain types while the number of locations is variable and unknown until the program runs.
Remembering the idea that having multiple copies of the same data is really bad (you have to find and change every occurance) I've usually done something like this (pseudo code done badly for simplicity)
C#:
Public List<TerrainClass> terrains = new List<TerrainClass>();
Public List<LocationClass> terrains = new List<LocationClass>();
Class TerrainClass()
{
int _id = 0;
string _name = "";
int _moveCost = 1;
<and more stuff>;
Public String TerrainName()
{
return _name;
}
}
Class LocationClass ()
{
int _id = 0;
int _terrainID = 0;
<and more stuff>
Public Int TerrainID ()
{
return _terrainID;
}
}
Then when I create a location I figure out which terrain it is and assign that terrain's id to the location and later reference it like
C#:
terrainName = terrains(thisLocation(index).TerrainID()).TerrainName()
Though this seems kind of long winded and prone to typos
I considered just having all the terrain data be part of the location class but there are so many repeats of the same information that it would waste a lot of memory (ok, not really significant compared to modern amount of RAM) and if I wanted to change something about the terrain I'd have to change it in a large number of places.
I also thought about encapsulating the terrain class inside the location class but that doesn't seem very different.
At the same time, when terrain is external it seems like Location isn't stand alone
What's a good way of handing this kind of situation? I'm open to entirely different ideas from this - although I've been programming for decades I've never really be "expert" in any way. Its entirely likely I'm barking up the wrong tree.
Thanks for any help or insights