我正在制作一个模拟幻想足球草案的C ++程序。我已经使用链接列表为每个参与选秀的人创建了一个团队名称列表。现在,我想知道我应该采用哪种方式将球队的球员加入到各自的球队。我把足球运动员放在一个读入档案中,可以弄清楚如何让他们选择哪一个,但无法弄清楚如何将它们存放在各自的团队中。
任何帮助表示赞赏 提前致谢
答案 0 :(得分:1)
嗯,你应该有一个团队课; Team类应该有一个容器来保存玩家名字(另一个链表,让我们说)。您现在拥有的列表应该包含Teams而不是Strings。
最终,玩家名称列表可能会升级为Player对象列表 - 而另一个类别是您想要定义的。
我知道这很模糊,但有帮助吗?
答案 1 :(得分:0)
看起来你只需要更好地理解基本的C ++容器。
一种方法可能是简单地从联盟中所有玩家的列表或阵列中移除玩家,并将其添加到幻想玩家的列表或玩家阵列中。
class Player; // ...
class FantasyPlayer
{
public:
std::vector< Player > picks; // array of my picks
};
std::vector< Player > all_players; // all available picks
std::vector< FantastyPlayer > fantasy_players; // all pickers
int iPicked = ...; // index of picked player in all_players
int iPicker = ...; // index of fantasy player currently picking
// add picked player to this fantasy player's pick list
fantasy_players[iPicker].picks.push_back(all_players[iPicked]);
// remove picked player from available players list
all_players.erase(iPicked);
另一种,也许更容易处理它的方法可能是直接从玩家本身引用“拣选者”。
class FantasyPlayer; // ...
class Player
{
public:
Player() : owner(0) { /* empty */ }
FantastyPlayer* owner; // pointer to fantasy player who picked me
};
std::vector< Player > all_players; // all available picks
std::vector< FantastyPlayer > fantasy_players; // all pickers
int iPicked = ...; // index of picked player in all_players
int iPicker = ...; // index of fantasy player currently picking
// create link between a player and its picker
all_players[iPicked].owner = &(fantasy_players[iPicker]);
此代码有意简短且不完整,但也许它会让您开始朝着正确的方向前进。祝你好运!