Jump to content

  • Log In with Google      Sign In   
  • Create Account

shuffle array


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 13 June 2013 - 09:01 PM

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);

 



Sponsor:

#2 King Mir   Members   -  Reputation: 2000

Like
0Likes
Like

Posted 13 June 2013 - 10:00 PM

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.



#3 Andy474   Members   -  Reputation: 683

Like
-3Likes
Like

Posted 14 June 2013 - 04:08 AM

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, 14 June 2013 - 04:10 AM.


#4 BitMaster   Crossbones+   -  Reputation: 4083

Like
1Likes
Like

Posted 14 June 2013 - 04:26 AM

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.



#5 Waterlimon   Crossbones+   -  Reputation: 2564

Like
0Likes
Like

Posted 14 June 2013 - 05:09 AM

Make a class or struct to represent your cards

 

struct card

{

int suit;

int value;

};

 

then use a container of cards and shuffle it.


o3o


#6 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 14 June 2013 - 11:07 AM

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



#7 frob   Moderators   -  Reputation: 21297

Like
2Likes
Like

Posted 14 June 2013 - 11:45 AM

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, 14 June 2013 - 11:46 AM.

Check out my personal indie blog at bryanwagstaff.com.

#8 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 14 June 2013 - 02:29 PM

actually I am reading the c++ primer 5th edition



#9 _greyfox()   Members   -  Reputation: 1133

Like
0Likes
Like

Posted 15 June 2013 - 01:42 AM


// 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, 15 June 2013 - 10:13 AM.

“There are thousands and thousands of people out there leading lives of quiet, screaming desperation, where they work long, hard hours at jobs they hate to enable them to buy things they don't need to impress people they don't like.”― Nigel Marsh

#10 froop   Members   -  Reputation: 636

Like
0Likes
Like

Posted 15 June 2013 - 10:07 AM

 


// 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



#11 _greyfox()   Members   -  Reputation: 1133

Like
0Likes
Like

Posted 15 June 2013 - 10:13 AM

 

 


// 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


“There are thousands and thousands of people out there leading lives of quiet, screaming desperation, where they work long, hard hours at jobs they hate to enable them to buy things they don't need to impress people they don't like.”― Nigel Marsh

#12 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 15 June 2013 - 02:31 PM

thanks for all the input



#13 Andy474   Members   -  Reputation: 683

Like
0Likes
Like

Posted 16 June 2013 - 06:13 AM

 

 

 


// 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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS