Hey everyone, I got bored and decided I wanted to make a 'pitch' cards game in c++, however I came across the problem of generating all 52 cards in a deck without duplicating the same card.

So basically, I just want to print out every single card in the deck without duplicating anything. Here is what I have so far:

#include<iostream>
#include<time.h>
using namespace std;
int main()
{
	int number[52], suit[52];
	char suitname[4][25] = {"Hearts", "Diamonds", "Clubs", "Spades"}, numbername[13][25] = {"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"};
	srand ((unsigned)time(0));
	for(int x=0;x<52;x++)
	{
		number[x] = (rand()%13);
		suit[x] = (rand()%4);
		cout << "You picked a " << numbername[number[x]] << " of " << suitname[suit[x]] << "!\n\n";
	}
}

The answer is a shuffle algo.

Pick two random numbers ( 1 - 52 ) then swap 'em.

Lather rinse and repeat until happy.

Set up a 52 int array called deck. Load each value with it's index: for (i=0; i<52; i++) deck[i]=i; Now do what iamthwee suggested. This becomes your shuffled deck.

Each entry in deck can be converted into a number/suit pair with / and %. Then use your suitname and numbername arrays for output.

Set up a 52 int array called deck. Load each value with it's index: for (i=0; i<52; i++) deck[i]=i; Now do what iamthwee suggested. This becomes your shuffled deck.

Each entry in deck can be converted into a number/suit pair with / and %. Then use your suitname and numbername arrays for output.

Well, I looked at what you guys said, and sadly I am not sure that I get it. I initialized 'deck', but am quite confused as to how I am supposed to implement it into the output. I am not very quick to catch on. x[

You seem to be over complicating things, too many variables holding information that doesn't need to be separate. Why not use a built in STL container to hold all the information? You could easily use a vector of strings to store "King of Diamonds" and "Ace of Spades" instead of all of those arrays. Then you could pick a random element from the vector and then use the push_back() function that is built in to the STL container to put it at the end of the vector. Run it enough and the "deck" will be as shuffled as any other.

Look up vectors on www.cplusplus.com for all the details you need to use it.

Edited 6 Years Ago by Acrimonus: n/a

Comments
Bad Acrimonous, baaad.

You seem to be over complicating things, too many variables holding information that doesn't need to be separate. Why not use a built in STL container to hold all the information? You could easily use a vector of strings to store "King of Diamonds" and "Ace of Spades" instead of all of those arrays. Then you could pick a random element from the vector and then use the push_back() function that is built in to the STL container to put it at the end of the vector. Run it enough and the "deck" will be as shuffled as any other.

Look up vectors on www.cplusplus.com for all the details you need to use it.

This statement is intrinsically still incorrect.

1) It doesn't really matter if you use a vector as opposed to a standard array, assuming of course, you initialise the array properly and make sure you don't exceed its bounds you're still good to go.

2) Picking a random number then pushing it back into the vector still has to problem you could be left with a shuffled deck with duplicate cards. . . In fact, this is probably going to be a certainty.

There are, of course many ways to do this. . .

You could use a struct/class to separate the deck into suit and number. You could then use a vector and even use the standard library's random_shuffle() function. . . Or you could write your own shuffle function using the logic I have outlined previously in this thread. . .

There are many ways to skin a cat. Meeowww.

Edited 6 Years Ago by iamthwee: n/a

Comments
Thanks for killing my reputation with your misinterpreted reading of my post, nice place this is.

Choosing a random number between 1 and 52, grabbing that element in the vector, removing and pushing it back at with push_back() will create duplicates? I think not. Even if it chose 1, put it at the end, and then picked 52 it may the be the same card but it hasn't been duplicated. Even if it randomized the same number twice it would be different cards being moved. And my comment about the arrays was that it isn't necessary to split apart the suit and value, not that vector was somehow superior to arrays.

However I like the idea of a vector of structs from the last reply to this thread.

You seem to be over complicating things, too many variables holding information that doesn't need to be separate. Why not use a built in STL container to hold all the information? You could easily use a vector of strings to store "King of Diamonds" and "Ace of Spades" instead of all of those arrays. Then you could pick a random element from the vector and then use the push_back() function that is built in to the STL container to put it at the end of the vector. Run it enough and the "deck" will be as shuffled as any other.

Look up vectors on www.cplusplus.com for all the details you need to use it.

That may have worked and such, but I kind of needed to separate the cards by suits and number values, since 'pitch' is an intricate card game that deals with both. Creating a 'suit' value helps with the game mechanics significantly. However, I just can't seem to get the cards unduplicable. (yes I know that is not a word.)

But based upon what you guys are saying, it sounds as though I am going to have to look into vectors. -- which I have never dealt with at all until now. So I guess I should dive on in. Thanks for the help everyone.

You don't need vectors.
To shuffle,

// get two random numbers: r1 and r2
temp = deck[r1];
deck[r1]=deck[r2];
deck[r2]=temp;

Repeat this until you like the randomness.

Choosing a random number between 1 and 52, grabbing that element in the vector, removing and pushing it back at with push_back() will create duplicates? I think not. Even if it chose 1, put it at the end, and then picked 52 it may the be the same card but it hasn't been duplicated. Even if it randomized the same number twice it would be different cards being moved. And my comment about the arrays was that it isn't necessary to split apart the suit and value, not that vector was somehow superior to arrays.

However I like the idea of a vector of structs from the last reply to this thread.

You may have a valid point. Sorry, but I thought you was just doing rand ( max number = 52 ), 52 times to generate a shuffled deck.

This article has been dead for over six months. Start a new discussion instead.