gpt4 book ai didi

c++ - 创建一个数组以迭代一个成员的两个数组

转载 作者:行者123 更新时间:2023-11-30 04:00:12 24 4
gpt4 key购买 nike

我正在开发纸牌游戏。

在这个游戏中,我有玩家,他们是一个团队的成员(每个团队 2 名玩家),他们可以加入一个大厅。

所以大厅有一个团队成员,两个团队都有一个玩家成员。我想直接遍历球员,而不是每次都经过球队。

相关C++代码:

lobby.h

class Lobby {

public:
Lobby(std::string const& name, std::vector<Team> const& teams = std::vector<Team>());
Lobby(){};
~Lobby();

// give cards to all players
void deal();

std::vector<std::shared_ptr<Player> > getPlayers(); // this is the one I'm trying to implement

[...]

private:
std::string _name;
std::vector<Team> _teams;
};

lobby.cpp

[...]
void Lobby::deal()
{
vector<Card> cards = Card::getAllCardsShuffled();

for (int i = 0; i < cards.size(); i++) {
getPlayers()[i % 4].addCard(cards[i]); // this is how I'd like to use getPlayers()
}
cout << _teams[0].getPlayers().at(0).getCards().size() << endl;
// this compiles but I'm not getting any cards in my players (prints 0)
// I think a copy is made somewhere, so I'm always getting new players
}


// this is the method I'm trying to implement...
vector<shared_ptr<Player> > Lobby::getPlayers()
{
return {
make_shared<Player>(_teams[0].getPlayers().at(0)),
make_shared<Player>(_teams[0].getPlayers().at(1)),
make_shared<Player>(_teams[1].getPlayers().at(0)),
make_shared<Player>(_teams[1].getPlayers().at(1))
};
}
[...]

团队.h

class Team {
public:
Team();
Team(std::string name);
~Team();
void addPlayer(Player const& player);
void addTrick(Trick const& trick);
void setScore(int const& newScore);
void addPoints(int const& score);
int getScore();
std::vector<Trick> getTricks();

std::vector<Player>& getPlayers();
bool isTeamDealing();

private:
std::string _name;
std::shared_ptr<std::vector<Player> > _players;
int _score;
std::vector<Trick> _wonTricks;
};

团队.cpp

Team::Team(string name)
: _name(name)
, _score(0)
, _players(vector<Player>())
{
}

void Team::addPlayer(Player const& player)
{
if (_players.size() < 2) {
_players.push_back(player);
}
}

vector<Player>& Team::getPlayers()
{
return _players;
}
[...]

现在代码在遍历团队和玩家时可以正常工作:

void Lobby::deal()
{
vector<Card> cards = Card::getAllCardsShuffled();

for (int i = 0; i < cards.size(); i += 4) {
_teams[0].getPlayers().at(0).addCard(cards[i]);
_teams[0].getPlayers().at(1).addCard(cards[i + 1]);
_teams[1].getPlayers().at(0).addCard(cards[i + 2]);
_teams[1].getPlayers().at(1).addCard(cards[i + 3]);
}
cout << getPlayers().at(0)->getCards().size() << endl;
}

...但我希望能够直接遍历玩家,而不使用 2 个嵌套循环。有没有简单的方法可以做到这一点?

TL;DR 有没有一种实现 getPlayers() 的方法可以让我迭代玩家 vector 并直接改变我的玩家?

编辑:我更新了 Team 中的代码,让 Team.getPlayers() 返回对 vector 的引用而不是指针。

最佳答案

所以我找到了一种方法来满足我的需求,使用 std::reference_wrapper:

vector<reference_wrapper<Player> > Lobby::getPlayers()
{
return {
_teams[0].getPlayers().at(0),
_teams[0].getPlayers().at(1),
_teams[1].getPlayers().at(0),
_teams[1].getPlayers().at(1)
};
}

void Lobby::deal()
{
vector<Card> cards = Card::getAllCardsShuffled();

for (int i = 0; i < cards.size(); i++) {
getPlayers().at(i % 4).get().addCard(cards[i]);
}
}

如果有人看到更好的方法,我会洗耳恭听。

关于c++ - 创建一个数组以迭代一个成员的两个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26450730/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com