Hey everyone,

I wrote some code to create a deck of cards, described the deck and randomly deals 5 cards. First, I want to let everyone know that this is homework for my C-II college class, so I am not looking for any answers, but solutions or guidance in figuring out what I am trying to do. What I want to do with this deck of cards is to shuffle the deck and then deal 5 cards, while not dealing the same cards in that hand.

IE:
Program deals 5 cards. The 5 cards are as follow (I actually ran the program to get this scenario).

Your card is 6 of Spades. <-- This card is identical to the one below.
Your card is 11 of Diamonds.
Your card is 9 of Diamonds.
Your card is 6 of Spades. <-- Identical card to the one above.
Your card is 13 of Spades.

/IE

So, as stated above, how can I fix this while shuffling, randomly dealing, and only producing 1 card each in a hand? Any help will be appreciated. Thanks!

P.S. Almost forgot, here's my code.

#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <time.h>
#include <string.h>

      struct deck{
       int pips;
       char suit;
       }deck[52];
       
main()
{
      int i;
      int j;
      
      srand(time(NULL));
      
      char C[6]="Clubs";
      char D[9]="Diamonds";
      char H[7]="Hearts";
      char S[7]="Spades";
      
      for(i=0; i<13; i++)
      { deck[i].pips=i+1;
      deck[i+13].pips=i+1;
      deck[i+26].pips=i+1;
      deck[i+39].pips=i+1;
      
      deck[i].suit='C';
      deck[i+13].suit='D';
      deck[i+26].suit='H';
      deck[i+39].suit='S';
      
      }
      
      for(i=0;i<5;i++)
      {
                      j=rand()%53;
                      
          printf("Your card is %d of ", deck[j].pips);
          
          switch(deck[j].suit)
          {
                              case 'C': printf("Clubs\n");break;
                              case 'D': printf("Diamonds\n");break;
                              case 'H': printf("Hearts\n");break;
                              case 'S': printf("Spades\n");break;
                              default : printf("Out of bounds.\n");break;}
          
          } 
          
          
          system("PAUSE");
          }

There are many ways to do this. Here are two ideas:

In your code above, each time you draw a card, check to see if you've alredy drawn that one. If you have, reject it and draw another.

Create all the cards in some suitable container - vector springs to mind. What you call a deck struct above clearly is not actually modelling a deck, but a single card. Your array of 52 decks (that you have named deck) IS a deck of cards, except that you have called the cards "decks", so you've got a deck of decks. Anyway, decide on your container, be it a vector or a simple array as above, and then shuffle that array (http://www.cplusplus.com/reference/algorithm/random_shuffle/ springs to mind) and then just read the cards one a at a time from that shuffled container.

Edited 4 Years Ago by Moschops: n/a

There are many ways to do this. Here are two ideas:

In your code above, each time you draw a card, check to see if you've alredy drawn that one. If you have, reject it and draw another.

Create all the cards in some suitable container - vector springs to mind. What you call a deck struct above clearly is not actually modelling a deck, but a single card. Your array of 52 decks (that you have named deck) IS a deck of cards, except that you have called the cards "decks", so you've got a deck of decks. Anyway, decide on your container, be it a vector or a simple array as above, and then shuffle that array (http://www.cplusplus.com/reference/algorithm/random_shuffle/ springs to mind) and then just read the cards one a at a time from that shuffled container.

How would I check after each card is drawn? Using an if statement?

Either compare the card drawn with every card already drawn, or as each card is drawn mark it in some way (perhaps with a new field in the struct, or a whole new data structure of some kind) and then each time check to see if you have already made the mark. It will be easier to simply create a deck of cards and shuffle it, and then draw them one at a time in sequence.

As for whether you should use an if statement; that really is doing your homework for you.

Edited 4 Years Ago by Moschops: n/a

Food for thought:
How many cards in a deck?
How many cards in a suit?
How many cards of a rank? (same pips)

Can you see a correlation between all these numbers?
Are the following mathematical operators useful in calculating these numbers? * / %

You can make the job easier by thinking through these questions and coming up with a new representation of your deck.

Next:
Once you deal a card, as Moschops suggests, you need to remove the card from the deck or mark it as used.

This code works, but somewhere in it I am getting a 'Your card is 0 of Out of Bounds.' Which it should give me the out of bounds, but what I'd like to do is remove the out of bounds and get no hand to produce a 0. Still trying to figure out how not to duplicate the same card in a hand. Ugh! I may need to ask for some examples.

This question has already been answered. Start a new discussion instead.