My requirements are as follows :
int Deal(int,CardSet&,CardSet&)
deals two hands into the two CardSet
arguments passed.
The number of cards to be placed into each hand is the first argument. The cards should be removed from the current set one at a time, placing the cards into alternate hands.
For example. if the current set held 2S, 3S, 4S, 5S, 6S, 7S
(the integers 0 to 5)and we had to deal 3 cards, then the two hands would get 2S, 4S, 6S
(integers 0, 2, 4) and 3S, 5S, 7S
(1, 3,5) respectively.
The two hands may already have cards in them, and the additional cards will require new memory. Do not create new memory more often than is required.
Remember that the current set has to be reduced in size as well. If there aren't enough cards in the current set to perform the deal, print an error message and terminate.
I don't know what is my mistake in Deal()
function. Program runs without any error but the output I get is wrong: Please help me in correcting my mistakes(if any).
My class objects defined in the main()
are:
CardSet CardSet1(104), CardSet2(12), CardSet3, CardSet4, CardSet5, CardSet6;
cout << "Dealing 3 Cards from CardSet1 into CardSet3 and CardSet4: " <<endl;
CardSet1.Deal(3,CardSet3,CardSet4);
cout << "Printout of CardSet1:" << endl;
CardSet1.Print();
cout << "Printout of CardSet3:" << endl;
CardSet3.Print();
cout << "Printout of CardSet4:" << endl;
CardSet4.Print();
cout << endl;
This is the Class:
class CardSet
{
public:
CardSet();
CardSet(int);
~CardSet();
int Size() const;
bool IsEmpty() const;
void Shuffle();
int Deal();
void Deal(int,CardSet&,CardSet&);
void Deal(int,CardSet&,CardSet&,CardSet&,CardSet&);
void AddCard(int);
void MergeShuffle(CardSet&);
void Print() const;
private:
int* Card;
int nCards;
void PrintCard(int c) const;
};
This is the function I have to work with:
CardSet::CardSet()
{
Card = NULL;
nCards =0;
}
CardSet::CardSet(int crd)
{
int nCard;
Card = new int[crd];
nCards=crd;
for(int i=0; i<nCards; i++)
{
Card[i]=i%52;
}
}
CardSet::~CardSet()
{
delete [] Card;
}
int CardSet::Size() const
{
return nCards ;
}
void CardSet::Shuffle()
{
int shuffle, tmp;
for (int i=0; i<nCards-1; i++)
{
shuffle = rand() % 52;
tmp = Card[i];
Card[i]=Card[shuffle];
Card[shuffle]= tmp;
}
}
int CardSet::Deal()
{
int a;
a = Card[0];
return a; // return Card[0]
int *newSet = NULL;
newSet = new int[nCards];
if(nCards==0)
{
cerr << "The set is empty ." << endl;
exit(1);
}
else
{
for(int i =0; i<nCards; i--)
{
newSet[i] = Card[i+1];
}
delete [] Card;
}
}
void CardSet::Deal(int ncard, CardSet& crdHand1, CardSet& crdHand2 )
{
int a;
a =nCards/3;
for(int i=0; i<a; i++)
{
for(int j=0; j<ncard; j++)
{
if(nCards==0)
{
cerr << "No more Cards. " <<endl;
exit(1);
}
else if(i%2==0)
{
crdHand1 = Card[i];
}
else if(i%2==1)
{
crdHand2 = Card[i];
}
}
}
}
This is the output: When i implement crdHand1 or crdHand2. The output should be like
for CardSet1:
2S 4S 6S 8S XS
QS AS 3C 5C 7C
.....
for CardSet4:
3S 5S 7S 9S JS
KS 2C 4C 6C 8C
......
and so on until cards are finished