Some opinion of whats "best" which you may disagree with.
Hlok wrote:
[1]First I want to define a Land Class which currently I only need a name, the current ruler residing, and the number of troops.
[2]Third I would like to randomly generate (4 for testing purposes) around 40 lands.
Ive read [2] to mean some kind of land mass, or grid square?
Names are important. Good names make understanding things easier. is "Land" really a good name for what your trying to describe? It sounds like your class is trying to manage multiple responsibilities.
For my model, id be tempted to name [1] something along the lines of "empire" or "territory".
and [2] "land" - with the meaning of the square or area of ground.
https://en.wikipedia.org/wiki/Single_responsibility_principleCode:
class RULER {
EMPIRE * territory; // Its a pointer, because the ruler of an empire may change.
};
// For me, an Empire "owns" its army, not the ruler.
class EMPIRE {
// Its a list pointing to each troop, as I guess you might want to do stuff with them at some point?
std::list<PERSON *> troops;
int army_size() {
return troops.size();
}
std::list<LAND *> empire_coverage;
void add_land_to_empire(LAND *l) {
empire_coverage.push_back(l);
}
int empire_size() {
return empire_coverage.size();
}
};
// A chunk of land doesn't care about "rulers" or "empires"
class LAND {
// Sounds abstract maybe?
};
class GRASS: inherit LAND { ... };
class SAND: inherit LAND { ... };
Quote:
Third I would like to randomly generate (4 for testing purposes) around 40 lands. I would like to generate (4 for testing) 10-15 Rulers and randomly assign them to the 40 lands.
Its normally good practise (In my opinion anyway) to have a class, which acts as a controller, that represents the system or sub-system.
Code:
class KINGDOM_WARS {
// Stereotype <<control>>
// Set up my interface objects (How i talk to the user).
// Have functions which react to signals from the interface objects.
KINGDOM_WARS() {
//Constructor will set up your lands and rulers.
};
void pick_ruler() {
// An event fired this function from the interface object.
};
void attack_this_land() {
//
};
};
Quote:
Fourth I would like to give the ability for you as the player to select a Ruler. This will place you in control of the lands which currently have your Ruler assigned.
Fifth I would like to be able to select another land to attack. This will run a basic battle function that uses a formula based on troops and power + a random value to dictate the winner. The loser will lose a percentage of troops based on the difference between the values.
I hope when reading the classes on LAND, EMPIRE and RULER, you've seen that multiple empires can "own" a LAND, and an EMPIRE can be ruled by multiple RULERS.
This is a business policy, These rules depend on your game/application. You could make a reference pointer in LAND and EMPIRE to its owner, but this is considered an OO "forgery" (I believe thats the correct term). Information should be kept in one place and one place only. You will discover forgeries causing you many issues down the road trying to keep them in sync and you will start to loose cohesion.
What you need to do is make a set of functions that will correctly alter the information so that it is correct to your rules. E.g. When some land is "won", the loser removes the land from its empire, and the "winner" adds the land to its empire.
All the best!
Mike