2

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

enter image description here

muzzi
  • 382
  • 3
  • 10
  • 1
    Once you call return, your method finished its execution – Denis Sheremet Apr 13 '18 at 05:33
  • what is Card. you didn't provide the definition for it. and you are setting CardSet = Card. – Arkady Godlin Apr 13 '18 at 05:37
  • Card is the particular CardSe – muzzi Apr 13 '18 at 05:42
  • 1
    The return statement ends a function, so the code after return a will never execute – Vivek Subramanian Apr 13 '18 at 05:42
  • i tried to make return statement a comment but still output is wrong – muzzi Apr 13 '18 at 05:44
  • 4
    A reasonable compiler with jacked up warnings should tell you all that code after that `return a;` will have no effect and never be executed. if yours didn't, time to turn up you warnings. If it did, don't ignore warnings. Once fixed, if the output is not correct, that is where you crack open your *debugger* and start single stepping through your program, hitting breakpoints, examining variables, and seeing where things go off the rails. – WhozCraig Apr 13 '18 at 05:44
  • Show how you've implemented crdHand1 = Card[I]; – super Apr 13 '18 at 05:47
  • And if you are getting wrong output you should show that too. – super Apr 13 '18 at 05:48
  • I just added the output have a look – muzzi Apr 13 '18 at 06:04
  • You aren't running into the bad effects here, but I recommend familiarizing yourself with [The Rule of Three](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). – user4581301 Apr 13 '18 at 06:11
  • It isn't quite clear what `CardSet::Deal()` (with no arguments) is expected to do. Use comments. Also `crdHand1 = Card[i];` doesn't do what you apparently think it does. I recommend you write a method `CardSet::addCard(int card)` and work with it. Removing `CardSet::CardSet(int)` should also help. You probably want to construct either an empty card set or a full deck, never anything in between. If you don't want to remove this constructor, mark it `explicit`. – n. m. could be an AI Apr 13 '18 at 06:23

1 Answers1

1

You could use std::shuffle() for <vector> to shuffle deck of cards

private void setCardDeck() {
    for (int i = 0; i < Card.Rank.values().length; i++) {
        for (int j = 0; j < Card.Suit.values().length; j++) {
            cardDeck.push_back(new Card(Card.Rank.values()[i], Card.Suit.values()[j]));
        }
    }

    std::shuffle(cardDeck.begin(), cardDeck.end());
}

From my poker implementation on Java. Rank and Suit are enums.

Thanks to Bob__ for improvement.

ivanjermakov
  • 1,131
  • 13
  • 24
  • I tried a function for shuffle which is not giving a shuffled output.. just added in th equestion. please have a look – muzzi Apr 13 '18 at 07:19
  • 4
    Please note that [`std::random_shuffle`](http://en.cppreference.com/w/cpp/algorithm/random_shuffle) was deprecated in C++14 and removed in C++17. You should use `std::shuffle` instead. – Bob__ Apr 13 '18 at 08:54