Do you want to make your own playing cards? Just download the blank playing card template PDF below, and start designing. The cards contain all the standard elements of playing cards, such as the numbers and suites. The center area of the cards, however, is left blank, allowing you to draw a picture or place a design. Make Your Own Playing Cards Pg 8. Title: Playing-Card-Template Created Date: 5/2/2014 7:34:59 PM.
I am trying to generate a deck of cards using C++. I have already written all of the code, but there is a problem that I can't seem to figure out:
In my constructor for the Deck class, I have a for() loop which generates all 52 cards and makes sure that the deck contains no matching cards. At least it should. The thing is, I can't make the loop iterate more than 47 times and still have it work. Any number over 47 causes the console screen to be empty upon run time, except for the blinking cursor. I am not quite sure what it is about numbers greater than 47 that cause it to stop working. I have tested it extensively and every number between 0 and 48 works.
Maybe I have some tiny error somewhere else in my code that I'm just not seeing. I don't really know. But I would really appreciate any help that I can get.
Here is my full code:
QT3.14QT3.14
5 Answers
There are 13 values in a 52 cards deck
12 * 4
->
48
13 * 4
->
52
Your original code with 12 values can only produce 48 different cards, this is why you get an infinite loop when you try to generate 52.
Edited :
By the way, you should follow Eric's and Hans Passant's (see comments on your question) advice. The way you do the shuffling is the wrong way to do it in the sense that there exists a much simpler / more natural / cleaner way. See below,
then you would have to generate all the 52 different cards once, like this
and finally shuffle the deck
More references on this common issue : http://bost.ocks.org/mike/shuffle and http://blog.codinghorror.com/the-danger-of-naivete.
Also please consider (as drescherjm sugested in his comment) to put the call to srand outside of this class. The call to srand resets the seed for the rand function and should in a very basic scheme only be called once at the very beginning of your main function. In your case, without a call to setCard() for each card you have, you might end up with 52 times the same card even though they are generated randomly ( see http://en.wikipedia.org/wiki/Pseudorandomness ).
I you have time you should look at the C++ random standard library header which provides way more the C rand lib. There even is a shuffle method in <algorithm>!
Community♦
Aurélien OomsAurélien Ooms
You are generating cards and discarding those already present. A better way would be to generate all of them linearly and them shuffle the deck.
The more your deck grows, the longer it gets to find a new valid card
EricEric
Just start off with a fresh pack. And then go through them swapping the current one with a random one. Do this seven times them done.
See https://www.math.hmc.edu/funfacts/ffiles/20002.4-6.shtml
Sort of code
This removes the problem as the pack is loaded and this shuffles it.
Ed HealEd Heal
Here is a fully functional example that I did in class last semester, it works pretty well.
Dave CribbsDave Cribbs
I would rather prepare full unshuffled deck first then every time user draws a card, pick random one from deck, replace its place in deck with last card on deck and decrement size of deck.
AnonymousAnonymous