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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
#include <iostream>
#include <string>
using namespace std;
class card
{
public:
int number;
string suit;
card(int n, string s);
card();
};
card::card(int n, string s)
{
number = n;
suit = s;
}
card::card()
{
number = 0;
suit = "";
}
class deck
{
private:
//array to hold some cards. Array is allocated by constructor
card * cardArray;
//size of cardArray, set by constructor
int maxSize;
//Number of cards in deck. Should not exceed maxSize.
int numCards;
public:
//default constructor: create the standard 52 card deck
deck();
//parameterized constructor: create a special deck that has
//card numbers from 1 to numberRange and numSuits different suits.
//In addition, the names of the different suits will be provided
//by the array of strings 'suitList'. This constructor should
//create a card for each possible pairing of a number and a suit.
//Therefore, there should be numberRange*numSuits total cards.
//If numberRange=13, numSuits=4, and
//suitList = {"heart","club","spade","diamond"}, then this constructor
//should create the same deck that would be created by
//the default constructor.
deck(int numberRange, int numSuits, string * suitList);
//place input card onto top of deck
void addCardTop(card);
//remove and return the card at the top of the deck
card dealCardFromTop();
//remove and return a random card from the deck
card dealRandomCard();
//rearrange all the cards in the deck into random order
void shuffle();
//Cut the deck at position'p': swap the lower portion
//of the card array (from 0 to p-1) with the
//top portion of the deck (position p to numCards-1).
void cut(int p);
//arrange the cards into increasing order by card number.
void sort();
void printDeck();
};
deck::deck()
{
maxSize = 52;
numCards = 0;
cardArray = new card[maxSize];
string suitArray[]={"Spades", "Clubs","Hearts","Diamonds"};
for(int x=0;x<4;x++)
{
cout << "Making suit: " << suitArray[x] << endl;
for(int i=0; i < 13; i++)
{
cardArray[x * 13+i].number = i + 1;
cardArray[x * 13+i].suit = suitArray[x];
numCards++;
}
}
}
void deck::addCardTop(card c)
{
if(numCards==maxSize)
{
card* tempArray= new card [maxSize+5];
for(int i=0;i<maxSize;i++)
{
tempArray[i]=cardArray[i];
}
delete[] cardArray;
cardArray=new card[maxSize+5];
for(int i=0;i<maxSize+5;i++)
{
cardArray[i]=tempArray[i];
}
cardArray[maxSize]=c;
numCards++;
maxSize+=5;
}
else
{
cardArray[numCards]=c;
numCards++;
}
printDeck();
}
void deck::printDeck()
{
for(int i = 0; i < numCards; i++)
{
cout << cardArray[i].suit << " " << cardArray[i].number << endl;
}
}
card deck::dealCardFromTop()
{
}
card deck::dealRandomCard()
{
int temp = numCards - 1;
numCards--;
return cardArray[temp];
}
|