Sign in to follow this  
phil67rpg

shuffle array

Recommended Posts

phil67rpg    443

Well I am attempting to shuffle a deck of cards using the random_shuffle function out of the algorithm library in the STL. I am able to print out a sorted deck of cards. I am confused on how to use the random_shuffle function on the cards array.I have googled the random_shuffle function but I am still perplexed on how to integrate the cards array. I am trying to not use magic numbers and I am trying to write better code. By the way low_val is 2 and hi_val is 14. I don't need any code just a small explanation. I  will do more research on arrays.here is small snipet of code.

Also values is 15 and suits is 4.i am lost in the cards+low_val and cards+hi_val part of code.

 
 
int cards[suits][values]={0};
 
 
for(int i=low_val; i<=hi_val; i++)
{
cards[spades][i]=i;
cards[hearts][i]=i;
cards[diamonds][i]=i;
cards[clubs][i]=i;
}
random_shuffle(cards+low_val,cards+hi_val);

 

Share this post


Link to post
Share on other sites
King Mir    2490

random shuffle (and other stl algorithims) need a container of things being shuffled. In this case you'd want a 1D array of cards. But you don't have a 1D array of cards, you have a 2D array of integers. You could typecast the extra dimension away, but the better approach would be to just use a 1D array. You can use integers to represent cards if it suits you, but then you're not keeping track of the suit. Alternately, use an class or struct.

 

Also, the algorithm needs iterators to the beginning and end of the array. For a simple array that's the name of the array, and the name plus the size.

Share this post


Link to post
Share on other sites
Andy474    694

I gather this is wasted but hey, I'm bored.

 

Your using the Wrong structures imo. You either want to use a std::Map or use 4 1D Arrays

 

E.g.

//EXAMPLE 1
std::Map<string, std::vector<int>> CardsBySuit;
CardsBySuit["Clubs"] = new vector<int>(...) //{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, }
CardsBySuit["Spades"] = new vector<int>(...) //{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, }
CardsBySuit["Diamonds"] = new vector<int>(...) //{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, }
CardsBySuit["Hearts"] = new vector<int>(...) // {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, }
 
// OR Example 2
int Diamonds = new vector<int>(...);
int Hearts= new vector<int>(...);
int Spades= new vector<int>(...);
int Clubs= new vector<int>(...);

Then to shuffle you have in int[] stored foreach suit and just suffle like so:

//EXAMPLE 1
std::random_shuffle(CardBySuit["DIAMONDS"].begin(), _CardBySuit["DIAMONDS"].end());
 
//EXAMPLE 2
std::random_shuffle(Clubs.begin(), _Clubs.end());

However, there is a easier, and nicer Object Oriented way of Storing your data using ENUMS and classes you should look into.

Edited by Andy474

Share this post


Link to post
Share on other sites
BitMaster    8651

First, using a map or four 1D arrays for simulating cards is probably the worst way to do it. Second, if you feel the need to new any C++SL container (like a vector), you are probably doing something very wrong. Third, I highly doubt you can random_shuffle iterators into a container which has order as something intrinsic to its very being, like a map.

Share this post


Link to post
Share on other sites
frob    44904

so I should use vectors instead of arrays according to andys post.

 
Not quite.  You should use container classes when appropriate, but that was not the key point of his post.

 
Much of the standard library is designed to work on a range. Look closer at his code:

std::random_shuffle(CardBySuit["DIAMONDS"].begin(), _CardBySuit["DIAMONDS"].end());

The parameters are x.begin() and x.end(). This is a range of objects. It starts at x[0] and ends at x[14] exclusive.

Compare it again with your code sample:

int cards[suits][values]={0};
...
random_shuffle(cards+low_val,cards+hi_val);

This is also a range of objects, but obviously not the correct range. This range starts at cards[2] and ends at cards[14].
 

 

In more general terms, a range operation takes two parameters, the starting address and the address one past the end of the range.  The developer can then use loops like for(T* ptr = begin; ptr < end; ptr++) and it will work correctly for any type T.

This type of range operation is ubiquitous in the language. If you are not comfortable with this kind of operation, I strongly suggest you go get a good book about the C++ language, such as Accelerated C++ by Koenig and Moo, and read it carefully.  

Edited by frob

Share this post


Link to post
Share on other sites
Pufixas    1167

// OR Example 2
int Diamonds = new vector<int>(...);
int Hearts= new vector<int>(...);
int Spades= new vector<int>(...);
int Clubs= new vector<int>(...);

 

Um... Did you just assigned vector to an integer?

Edited by Edvinas Kilbauskas

Share this post


Link to post
Share on other sites
froop    642

 


// OR Example 2
int Diamonds = new vector<int>(...);
int Hearts= new vector<int>(...);
int Spades= new vector<int>(...);
int Clubs= new vector<int>(...);

 

Um... Did you just assigned vector to an integer?

 

 

A pointer to a vector on top of that. or is this not c++? maybe java? laugh.png

Share this post


Link to post
Share on other sites
Pufixas    1167

 

 


// OR Example 2
int Diamonds = new vector<int>(...);
int Hearts= new vector<int>(...);
int Spades= new vector<int>(...);
int Clubs= new vector<int>(...);

 

Um... Did you just assigned vector to an integer?

 

 

A pointer to a vector on top of that. or is this not c++? maybe java? laugh.png

 

You can't do that in java either :D

Maybe he just made his own programming language, haha

Share this post


Link to post
Share on other sites
Andy474    694

 

 

 


// OR Example 2
int Diamonds = new vector<int>(...);
int Hearts= new vector<int>(...);
int Spades= new vector<int>(...);
int Clubs= new vector<int>(...);

 

Um... Did you just assigned vector to an integer?

 

 

A pointer to a vector on top of that. or is this not c++? maybe java? laugh.png

 

You can't do that in java either biggrin.png

Maybe he just made his own programming language, haha

 

:p

 

big mistake, typo ...

 

the [ code] tags on here keep eating code when i type so i have to retype, retype retype.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this