I'm building a sports text-based game and have a beginner level question. Say I have 100 different teams (I've set them up as classes) and each team has 10 players (I've set the players up as structures within the team class). Each of the player strucures has 10 members (these are player attributes like height, weight, etc). 2 questions:
1) How do I set up my code so I can call a specific attribute of a specific player on a specific team?
2) I want more than one team to have the same player be one of its players. How do I do this?
Below is my code. As an example to my first question, I tried totalteams[2].totalplayers[3].position but that format doesn't work because the totalplayers array is not recognized as one of the members of the totalteams array. I don't know how you call structures within a class.
//create team class
class team {
public:
int teamNumber;
string name;
int currentPlayerCount;
int budget;
float powerRank;
struct player {
int rank;
string name;
string position;
string state;
string height;
int weight;
string school;
float forty_time;
float visitScore;
int visitNumber;
};
};
//create totalteams array
constint maxteams = 100;
team totalteams[maxteams];
//create totalplayers array
constint maxplayers = 100;
player totalplayers[maxplayers];
struct player {
int rank;
string name;
string position;
string state;
string height;
int weight;
string school;
float forty_time;
float visitScore;
};
class team {
public:
int teamNumber;
string name;
int currentPlayerCount;
int budget;
float powerRank;
player playersInfo[]; //I would store the information of each player in this array
};
In the long run, you might not want to use an array, but instead something that can increase in size as you add/remove/trade players like a vector or something along those lines.
You could look at teams and players as being in separate structures. That way, your problem of having the same player in more than one team is solved.
For example, in the teams class, you store your players as an array of player indices. When you need to render information about a team's players, just iterate through the players array using the index value of each player. Here is a very "thin" example of what I'm trying to convey. I have made the team class public for simplicity (please don't do it this way):