Debian Codegraph @

src/cardgroup.cpp

raw
1 // Copyright © 2002-2003 Rob Kaper <[email protected]>
2 // 2010-2015 Sylvain Rochet <[email protected]>
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // version 2 as published by the Free Software Foundation.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; see the file COPYING. If not, see
15 // <http://www.gnu.org/licenses/>.
16
17 #include "config.h"
18
19 #include <algorithm>
20
21 #include "card.h"
22 #include "cardgroup.h"
23 #include "game.h"
24
25 CardGroup::CardGroup(int id, Game *game, std::string name) : GameObject(id, GameObject::Unknown, game)
26 {
27 setProperty("name", name, game);
28 }
29
30 CardGroup::~CardGroup()
31 {
32 while (!m_cards.empty()) { delete *m_cards.begin(); m_cards.erase(m_cards.begin()); }
33 }
34
35 Card *CardGroup::newCard(int id, const std::string name)
36 {
37 Card *card = new Card(this, id);
38 card->setProperty("name", name, m_game);
39 m_cards.push_back(card);
40 return card;
41 }
42
43 Card *CardGroup::nextCard()
44 {
45 Card *card = 0;
46
47 if (m_cards.size() > 0)
48 {
49 // Fetch first card
50 card = m_cards[0];
51
52 // Remove first card
53 m_cards.erase(m_cards.begin());
54
55 // Reinsert it at the back
56 m_cards.push_back(card);
57 }
58
59 return card;
60 }
61
62 Card *CardGroup::findCard(int id)
63 {
64 Card *card = 0;
65 for (std::vector<Card *>::iterator it = m_cards.begin() ; it != m_cards.end() && (card = *it) ; ++it)
66 if (card->id() == id)
67 return card;
68 return 0;
69 }
70
71 void CardGroup::pushCard(Card *card)
72 {
73 m_cards.push_back(card);
74 }
75
76 void CardGroup::popCard()
77 {
78 m_cards.pop_back();
79 }
80
81 void CardGroup::shuffleCards()
82 {
83 std::shuffle(m_cards.begin(), m_cards.end(), m_game->m_randomEngine);
84 }