summaryrefslogtreecommitdiffstats
path: root/cardpile.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cardpile.cpp')
-rw-r--r--cardpile.cpp88
1 files changed, 88 insertions, 0 deletions
diff --git a/cardpile.cpp b/cardpile.cpp
new file mode 100644
index 0000000..f5bed61
--- /dev/null
+++ b/cardpile.cpp
@@ -0,0 +1,88 @@
+/*
+ * cardpile.cpp
+ *
+ * Notes: This class is inherited multiple of times, so virtual keyword will need
+ * to be used to avoid making multiple instances of this class and to avoid
+ * ambiguities.
+ *
+ */
+
+#include "cardpile.h"
+
+CardPile::CardPile(void) : deckCurrIndex(51), discardPileCurrIndex(0)
+{
+ /* generate deck of 52 cards */
+ int i, j;
+ int k = 0;
+ string suit[4] = {"C","D","H","S"};
+ string c[13] = {"A","2","3","4","5","6","7","8","9","T","J","Q","K"};
+ string tmp;
+
+ for (i = 0; i < 4; i++)
+ for (j = 0; j < 13; j++, k++)
+ {
+ tmp = c[j] + suit[i];
+ deck[k] = new Card(tmp);
+ deck[k]->rankCard();
+ }
+}
+
+CardPile::~CardPile(void)
+{
+ int i;
+ for (i = 0; i < 52; i++)
+ delete deck[i];
+}
+
+Card CardPile::getCard(int index)
+{
+ if ((index >= 0) && (index < 52))
+ return *deck[index];
+ else
+ return Card();
+}
+
+void CardPile::printPile(void)
+{
+ int i;
+ for (i = 1; i <= deckCurrIndex + 1; i++)
+ {
+ if (deck[i])
+ cout << deck[i-1]->getType() << " ";
+
+ if ( (i % 13) == 0 && i != deckCurrIndex+1)
+ cout << endl;
+ }
+
+ cout << endl;
+}
+
+Card *CardPile::getCardFromDeck(int index)
+{
+ return deck[index];
+}
+
+void CardPile::putCardToDeck(int index, Card *card)
+{
+ deck[index] = card;
+}
+
+void CardPile::putCardToDiscardPile(Card *card)
+{
+ discardPile[discardPileCurrIndex] = card;
+ discardPileCurrIndex++;
+}
+
+Card *CardPile::PopCardFromDeck(void)
+{
+ if (deckCurrIndex >= 0)
+ return deck[deckCurrIndex--];
+ else
+ return NULL;
+}
+
+void CardPile::resetDeck(void)
+{
+ deckCurrIndex = 51;
+}
+