File: cardgroup.cpp

package info (click to toggle)
monopd 0.9.3-4.1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 788 kB
  • ctags: 575
  • sloc: cpp: 5,666; sh: 712; makefile: 27
file content (82 lines) | stat: -rw-r--r-- 1,935 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (c) 2002-2003 Rob Kaper <cap@capsi.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// version 2 as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING.  If not, write to
// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.

#include <algo.h>

#include "card.h"
#include "cardgroup.h"
#include "game.h"

CardGroup::CardGroup(int id, Game *game, std::string name) : GameObject(id, GameObject::Unknown, game)
{
	setProperty("name", name, game);
}

CardGroup::~CardGroup()
{
	while (!m_cards.empty()) { delete *m_cards.begin(); m_cards.erase(m_cards.begin()); }
}

Card *CardGroup::newCard(unsigned int id, const std::string name)
{
	Card *card = new Card(this, id);
	card->setProperty("name", name, m_game);
	m_cards.push_back(card);
	return card;
}

Card *CardGroup::nextCard()
{
	Card *card = 0;

	if (m_cards.size() > 0)
	{
		// Fetch first card
		card = m_cards[0];

		// Remove first card
		m_cards.erase(m_cards.begin());
	
		// Reinsert it at the back
		m_cards.push_back(card);
	}

	return card;
}

Card *CardGroup::findCard(unsigned int id)
{
	Card *card = 0;
	for (std::vector<Card *>::iterator it = m_cards.begin() ; it != m_cards.end() && (card = *it) ; ++it)
		if (card->id() == id)
			return card;
	return 0;
}

void CardGroup::pushCard(Card *card)
{
	m_cards.push_back(card);
}

void CardGroup::popCard()
{
	m_cards.pop_back();
}

void CardGroup::shuffleCards()
{
	random_shuffle(m_cards.begin(), m_cards.end());
}