Shuffling a deck of cards

Hi all,

im working on a simple deck of cards project and i've got to the shuffle bit and hit a brick wall. Im not sure how i am meant to do this as our tutor has instructed us to use pointers to pointers like this e.g.
PlayingCard**_playing_cards;

i have my build deck function which works just fine and it is..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Deck::build_deck()
{

    for( int suit = 1; suit <= 4 && _deal_next != 51; suit++ && _deal_next+13)
        {
            for( int fvalue = 1; fvalue <= 13; fvalue++ )
                {
                    _playing_cards[_deal_next] = new PlayingCard( fvalue, suit );
                   /* cout << PlayingCard( fvalue, suit ) << endl; */
                }
           
        }

}


Then this is what i have so far for the shuffle function..
1
2
3
4
5
6
7
8
9
10
11
void Deck::shuffle_deck()
{

 for( int i = 0; i < 52; i++)
 {
      int random_card01 = rand() % 52;
               
      _playing_cards[random_card01]*
 }
     
}


Thanks in advance for any help you can give, and i dont mind being told stuff is wrong haha :).
I doubt build_deck is working correctly. _deal_next doesn't change anywhere so only one element in the array will be set. Why not start _deal_next as zero and increment it in the inner loop ++_deal_next;?

You can use std::random_shuffle to shuffle the deck.
std::random_shuffle(_playing_cards, _playing_cards + 52);
Last edited on
Sorry, I have set _deal_next = 0 as default in the class constructor so it works, I tested it :). And thanks for the shuffle bit :)!
Jinz, I think what Peter was saying is that _deal_next never gets assigned a new value. _deal_next + 13 does this...

0 + 13, there is no assignment. Every time you loop through a suit, you will be indexing into your array with 13. I'm not sure how it works (I guess there is code that is not posted?)
(I guess there is code that is not posted?)


Yes there is code that isn't posted clanmjc here's my whole deck.cpp file :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "deck01.hpp"
#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;



Deck::Deck( int total_cards )
{
    _total_playing_cards = total_cards;

    _playing_cards = new PlayingCard*[total_cards];

    _deal_next = 0;
}

void Deck::build_deck()
{
     
for( int suit = 1; suit <= 4 && _deal_next != 51; ++suit, _deal_next+13)
        {
            for( int fvalue = 1; fvalue <= 13; fvalue++ && _deal_next++)
                {
                    _playing_cards[_deal_next] = new PlayingCard( fvalue, suit );
                    
                    cout << PlayingCard( fvalue, suit ) << endl;
                    
                    
                }
           
        }
/*    for( int suit = 1; suit <= 4 && _deal_next != 51; suit++)
        {
            for( int fvalue = 1; fvalue <= 13; fvalue++ && _deal_next++)
                {
                    _playing_cards[_deal_next] = new PlayingCard( fvalue, suit );
                    cout << PlayingCard( fvalue, suit ) << endl;
                    
                }
           
        }

*/
}

void Deck::shuffle_deck()
{

std::random_shuffle(_playing_cards, _playing_cards + 52);
     
}

PlayingCard Deck::deal_card()
{


 
}

/*void PictureDeck::build_deck()
{

    for( int suit = 1; suit <= 4 && _deal_next != 51; suit++ && _deal_next+13)
        {
            for( int fvalue = 1; fvalue <= 13; fvalue+10 )
                {
                    _playing_cards[_deal_next] = new PlayingCard( fvalue, suit );
                    cout << PlayingCard( fvalue, suit ) << endl;
                }
           
        }

}

*/

/* to shuffle...
pick one random card store it in a tempory spot..
select another random card..
put the second card into the first spot..
put the first card into the second spot..
repeat 60 times.. */


I hope that clears some things up :)
I hope that clears some things up :)
It certainly does! You good to go looks like your shuffling now?
Topic archived. No new replies allowed.