矢量没有插入

时间:2014-05-18 05:54:08

标签: c++ vector

我有两个类,即Player和Deck。 Player和Deck都有一个Card类型的向量。我试图从牌组中取出牌,然后将牌中的卡牌移除,然后将其添加到牌手牌中。但是,卡片中的卡片已成功移除,但我的播放器中的卡片未被添加。播放器中卡片的矢量大小保持为0。

代码:

(甲板)

void Deck::GiveCards(std::vector<Player> pv) {
    for (int i = 0; i < 7; i++) {
        for each(Player p in pv) {
            int a = rand() % Deck::deckCards.size();
            CardType card = Deck::deckCards[a];
            p.DrawCard(card, p.GetHands());
            Deck::deckCards.erase(Deck::deckCards.begin()+a);
        }
    }
}

(播放器)

void Player::DrawCard(CardType card, std::vector<CardType>& hand) {
    hand.push_back(card);
}

std::vector<CardType> Player::GetHands() {
    return Player::handCards;
}

我确信Deck中的代码运行正常。它在Deck中执行整个代码。但是,出于某种原因,它并没有将值推送到Player类中的向量中。

完整代码:

(甲板)

Deck::Deck() {
    for (int i = 0; i < 4; i++) { // i refer to color
        CardType card(EnumValue::ZERO, EnumValue::NONE, i);
        Deck::InsertCardToDeck(card);
        for (int j = 1; j <= 18; j++) {
            int cardValue = ceil(j / 2.0);
            CardType card(cardValue, EnumValue::NONE, i);
            Deck::InsertCardToDeck(card);
        }
        for (int j = 0; j < 6; j++) {
            if (j < 2) {
                CardType card(EnumValue::SKIP, EnumValue::SKIP_EFFECT, i);
                Deck::InsertCardToDeck(card);
            }
            else if (j < 4) {
                CardType card(EnumValue::REVERSE, EnumValue::REVERSE_EFFECT, i);
                Deck::InsertCardToDeck(card);
            }
            else {
                CardType card(EnumValue::DRAW2, EnumValue::DRAW2_EFFECT, i);
                Deck::InsertCardToDeck(card);
            }
        }
    }
    for (int j = 0; j < 4; j++) {
        CardType card(EnumValue::DRAW4, EnumValue::DRAW4_EFFECT, EnumValue::RAINBOW);
        Deck::InsertCardToDeck(card);
    }
    for (int j = 0; j < 4; j++) {
        CardType card(EnumValue::WILD, EnumValue::WILD_EFFECT, EnumValue::RAINBOW);
        Deck::InsertCardToDeck(card);
    }
}

void Deck::InsertCardToDeck(CardType card) {
    Deck:deckCards.insert(deckCards.end(), card);
}

std::string Deck::PrintDeck() {
    std::string textOutput = "";
    std::vector<int>::size_type sz = Deck::deckCards.size();
    for (int i = 0; i < sz; i++) {
        textOutput += std::string("Card ") + std::to_string(i) + ": It's a " + GetColorValue(deckCards[i].GetColor()) + " " + GetNumberValue(deckCards[i].GetValue()) + "\r\n";
    }
    return textOutput;
}

std::string Deck::GetColorValue(int color) {
    switch (color) {
    case 0:
        return "RED";
    case 1:
        return "YELLOW";
    case 2:
        return "GREEN";
    case 3:
        return "BLUE";
    default:
        return "RAINBOW";
    }
}

std::string Deck::GetNumberValue(int color) {
    switch (color) {
    case 0:
        return "ZERO";
    case 1:
        return "ONE";
    case 2:
        return "TWO";
    case 3:
        return "THREE";
    case 4:
        return "FOUR";
    case 5:
        return "FIVE";
    case 6:
        return "SIX";
    case 7:
        return "SEVEN";
    case 8:
        return "EIGHT";
    case 9:
        return "NINE";
    case 10:
        return "SKIP";
    case 11:
        return "REVERSE";
    case 12:
        return "DRAW 2";
    case 13:
        return "DRAW 4";
    default:
        return "WILD";
    }
}

void Deck::ShuffleDeck() {
    std::random_shuffle(Deck::deckCards.begin(), Deck::deckCards.end());
}

void Deck::GiveCards(std::vector<Player>& pv) {
    for (int i = 0; i < 7; i++) {
        for(Player p:pv) {
            int a = rand() % Deck::deckCards.size();
            CardType card = Deck::deckCards[a];
            p.DrawCard(card, p.GetHands());
            Deck::deckCards.erase(Deck::deckCards.begin()+a);
        }
    }
}

(播放器)

void Player::DrawCard(CardType card, std::vector<CardType>& hand) {
    hand.push_back(card);
}

std::vector<CardType>& Player::GetHands() {
    return handCards;
}

Player::Player() {
    int a = rand() % 10;
    Player::name = "Bot Player " + std::to_string(a);
}

Player::Player(std::string names) {
    Player::name = names;
}

std::string Player::PrintCard() {
    std::string c = Player::name;
    for(CardType a:handCards) {
        c += std::string(" has: ") + Deck::GetColorValue(a.GetColor()) + " " + Deck::GetNumberValue(a.GetValue()) + "\r\n";
    }
    return c;
}

2 个答案:

答案 0 :(得分:2)

在此功能中,

std::vector<CardType> Player::GetHands() {
    return Player::handCards;
}

您将返回Player::handCard的副本。更改它以返回引用:

std::vector<CardType>& Player::GetHands() {
    return handCards;
}

<强>更新

更改

    for each(Player p in pv) {

    for each(Player& p in pv) {

更新2

回应@Ophitect的最新评论。

看起来像

   for each(Player p in pv) {
无法使用

获取pv中对象的非const引用。我会将该循环更改为:

    std::vector<Player>::iterator iter = pv.begin();
    std::vector<Player>::iterator end = pv.end();
    for (; iter != end; ++iter ) {
        Player& p = *iter;
        int a = rand() % Deck::deckCards.size();
        CardType card = Deck::deckCards[a];
        p.DrawCard(card, p.GetHands());
        Deck::deckCards.erase(Deck::deckCards.begin()+a);
    }

答案 1 :(得分:0)

请尝试使用此代码:

void Deck::GiveCards(std::vector<Player> &pv) {
    for (int i = 0; i < 7; i++) {
        for each(Player& p in pv) {
            int a = rand() % deckCards.size();
            p.GiveCard(deckCards[a]);
            deckCards.erase(deckCards.begin()+a);
        }
    }
}

void Player::GiveCard(const CardType& card) {
    handCards.push_back(card);
}

或者这个:

void Deck::GiveCards(std::vector<Player> &pv) {
    for (int i = 0; i < 7; i++) {
        for each(Player& p in pv) {
            p.DrawRandomCard(*this);
        }
    }
}

CardType Deck::DrawRandomCard()
{
    int a = rand() % deckCards.size();
    CardType card = deckCards[a];
    deckCards.erase(deckCards.begin()+a);
    return card;
}

void Player::DrawRandomCard(Deck& deck) {
    handCards.push_back(deck.DrawRandomCard());
}