Hello all. First up, I've been using this site alongside coding books and tutorials to learn C++. I'm enjoying it, but am still at the early stages of learning the language. Be gentle. ;-)
I'm trying to write a very basic football (soccer) simulation to teach me some of the fundamentals of using data, pointers, vectors and so on. It's all console at the moment, with the intention of looking at Win32/DirectX GUIs once I have learnt the basics. I'd like some advice as to whether I'm setting myself up for a massive fall.
I have loaded the team data from a CSV file and created a vector of objects (called Clubs). I have done the same for all the countries in the world (called Nations). This works fine in terms of loading it into the memory. I have also been able to create Competition objects which can load into their own internal ClubsList vector the indexes of the clubs whose "Clubs.competition" variable has the ID number for that competition.
What this means is that the Competitions.ClubsList vector holds the index of the specific club in the Clubs vector. So, when pulling this from a function I pass the address of the Clubs vector and then call Clubs[x] to find that club's information.
This works (for now!), but I wonder if it will fall apart once the vectors start changing in size and being moved around the memory?
I am now trying to generate schedules (using the Berger system) which works perfectly well (mathematically). I will be creating new Match objects, and then I will pass the index of that match within a Matches vector to both the club (so the club can know who they are playing) and to the competition (so it knows which results affect its standings).
The question is - is using indexes to reference these objects a good idea or not? What potential problems am I creating for myself? And is there a standard way of approaching this problem which I've managed to miss?
By way of an example, he is the code which checks the clubs vector to see which clubs belong to the specific competition:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
void league::FindMembers(std::vector<club>& clubsVector)
{
// Loops through the club database and checks to see which
// belong to this league competition
unsigned int sizeOfClubVector;
sizeOfClubVector = clubsVector.size();
int ClubsInLeague = 0;
for (unsigned int i = 0; i < sizeOfClubVector; i++) {
if (clubsVector[i].GetClubComp() == 1) { // uses "1" at the moment, because I only have one competition, but this will become a function parameter
MemberClubs.push_back(i);
ClubsInLeague++;
}
}
NumberOfTeams = ClubsInLeague;
club ClubToPrint;
int index;
for (int i = 0; i < ClubsInLeague; i++)
{
index = MemberClubs[i];
ClubToPrint = clubsVector[index];
std::cout << ClubToPrint.GetClubName() << "\n";
}
return;
}
|