Problem with random shuffle

I used to be using a vector as my card pack which would use a random shuffle function fine, but I recently changed it to an array which has brought up a few problems for me

1
2
3
4
5
6
#include algorithm
void Deck::Shuffle()
{	
	srand(time(0));
	random_shuffle(s_CardPack[0][0], s_CardPack[51][0]);
}


when I try to compile it opens up #algorithm and highlights line 63, I'm not sure why.
Did I write it wrong?

Also, I want to make sure that, for example, if [2][0] was shuffled to [30][0], that [2][1] would also be moved to [30][1].

Is this possible with the random shuffle function or do I have to code a for loop myself?
Last edited on
- It's #include <algorithm> . Don't forget the <angle brackets>.

- Can't tell you if you're calling random_shuffle incorrectly without knowing what s_CardPack is. Can you show us how that is defined?

and highlights line 63


Which line is line 63? I only see 6 lines.


Also, I want to make sure that, for example, if [2][0] was shuffled to [30][0], that [2][1] would also be moved to [30][1].
Is this possible with the random shuffle function or do I have to code a for loop myself?


You should not try to keep multiple different arrays in sync. If you have lots of different information that needs to stay together, put it in a struct, then have an array of that struct:

1
2
3
4
5
6
7
8
9
10
11
struct Card
{
  int suit;
  int rank;
};

Card deck[52];

 // initialize your deck here

random_shuffle(deck, deck + 52);
Oops i did include brackets I just typed that really fast before I pressed enter to avoid getting responses saying that was my problem :P So much for that

static string s_CardPack[52][3]; <-- thats how I defined s_CardPack

When I said it highlights line 63, I meant in the algorithm include file:
#include <bits/stl_algo.h>
that is highlighted with no error to tell me what I am doing wrong.


Anyway I haven't heard of a struct before but it looks like it could save me a lot of time. So does your example mean that creating deck[52] will be a single object and suit and rank will also be arrays? or are you creating 52 different deck "structs" each with their own individual suit and rank?
Last edited on
When I said it highlights line 63, I meant in the algorithm include file:
#include <bits/stl_algo.h>


I don't know what header that is... but the header that has random_shuffle in it is this:
 
#include <algorithm> 

So I would erase what you have there and replace it with that.

or are you creating 52 different deck "structs" each with their own individual suit and rank?

This.

1
2
3
4
Card deck[52];

deck[0].suit = /*spade*/;
deck[0].rank = /*Ace*/
You need to learn a LOT about iterators.
1. Random_shuffle() accepts iterators as inputs.
2. Vector::begin() and end() return iterators.
3. Iterators are basicly pointers
1.+2.+3. Don't dereference the begin and end of your array when using them as iterators!!!
Use this:
1
2
3
4
5
6
#include <algorithm>
void Deck::Shuffle()
{	
	srand(time(0));
	random_shuffle(s_CardPack[0], s_CardPack[51]+3);
}

PS: Why do you even have 2 dimensions in your array???
Last edited on
BTW Disch, bits/stl_algo.h is where the actual functions are defined. Algorithm is there just to include some extra needed headers and for the name to be simpler
Awesome, thanks Disch for the info on structs. Looks a lot easier and cleaner to work with

@viliml
I had two dimensions in my array because I hadn't used structs before, and I want more than just a simple "A\3" for my card. I want to be able to compare them with other cards, which is why I had the other dimension to store an integer value and suit in it as well as the face appearance. Now that I know about structs though it looks like I can just use that

Also thanks for the info on iterators. The book never explained why arrays were made into iterators but not vectors, but now I know :P
Topic archived. No new replies allowed.