diff --git a/cockatrice/src/game/cards/card_list.cpp b/cockatrice/src/game/cards/card_list.cpp index 09eb77e6a..828aa4a95 100644 --- a/cockatrice/src/game/cards/card_list.cpp +++ b/cockatrice/src/game/cards/card_list.cpp @@ -9,21 +9,26 @@ CardList::CardList(bool _contentsKnown) : QList(), contentsKnown(_co { } -CardItem *CardList::findCard(const int id) const +/** + * @brief Finds the CardItem with the given id in the list. + * If contentsKnown is false, then this just returns the first element of the list. + * + * @param cardId The id of the card to find. + * + * @returns A pointer to the CardItem, or a nullptr if not found. + */ +CardItem *CardList::findCard(const int cardId) const { - if (!contentsKnown) { - if (empty()) - return 0; - CardItem *temp = at(0); - return temp; - } else - for (int i = 0; i < size(); i++) { - CardItem *temp = at(i); - if (temp->getId() == id) { - return temp; + if (!contentsKnown && !empty()) { + return at(0); + } else { + for (auto *cardItem : *this) { + if (cardItem->getId() == cardId) { + return cardItem; } } - return 0; + } + return nullptr; } class CardList::compareFunctor diff --git a/cockatrice/src/game/cards/card_list.h b/cockatrice/src/game/cards/card_list.h index be9b266c8..9d72aeaf2 100644 --- a/cockatrice/src/game/cards/card_list.h +++ b/cockatrice/src/game/cards/card_list.h @@ -20,7 +20,7 @@ public: SortByType = 2 }; CardList(bool _contentsKnown); - CardItem *findCard(const int id) const; + CardItem *findCard(const int cardId) const; bool getContentsKnown() const { return contentsKnown;