random shuffle deck

I am working on a card game. I have the program able to shuffle the deck 52 cards in an array, but the way that the random is making the cards is with the card number and not the card, I cannot think of a way to make the output the type of card it is instead.

what I have: 1, 2, 3, 4, 5, 6, 7, 8,etc...
what i want: a1, 21, 31, 41, etc...

where the first character is the card type and the second is the suit represented by a number. 10's will be three characters total 101, 102, 103, 104.

here is the code i am working on. Any help would be great :)
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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main() {
    int x;
    int card[52];
    int n;
    srand(time(0));
    
    for (int i=0; i<52; i++) {
        card[i] = i;
    }
    
    while (cin >> n) {
          for (int i=0; i<(52-1); i++) {
              int r = i + (rand() % (52-i));
              int temp = card[i]; card[i] = card[r]; card[r] = temp;
          }
          
          for (int c=0; c<n; c++) {
              cout << card[c] << " ";
              }
          cout << endl;
    }
    cin >> x;
    
    return 0;
    
}
Check this out -> http://cplusplus.com/reference/algorithm/random_shuffle/

You can use it like this -> random_shuffle(array, array + array_size);

EDIT: Looks like I misunderstood the question...
Last edited on
you could approach it that way, but have you considered just keeping the result as a number and determining from that number what suit and card it represents?

If you had two arrays:

1
2
std::string suits[4] = { "hearts", "clubs", "spades", "diamonds" };
std::string cardsTypes[13] = { "Ace", "2", ... "King" };


you could determine the suit from the number divided by 13 (as there are thirteen cards in each suit), and the type of card by the number modulus 13.

1
2
3
int cardNumber = someFunction();
std::string suit = suits[cardNumber/13];
std::string type = cardTypes[cardNumber%13];


this seems simpler imo, but it's up to you of course.
Last edited on
Thank you guys soo much for the help, let the creation of my card game continue! :)
Not sure how much c++ you know, but the natural approach seems to be the one by quirkyusername, but in a stream inserter.

1
2
3
4
5
6
7
8
9
10
typedef int Card;

std::ostream &operator<<(std::ostream &os, const Card c) {
std::string suits[4] = { "hearts", "clubs", "spades", "diamonds" };
std::string cardsTypes[13] = { "Ace", "2", ... "King" };

assert(c >= 0 && c < 52);
os << type[int(c)%13] << " of " << suits[int(c)/13];
return os;
}


With this you can now do stream << card.
I would define a card as:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
enum Suits
{
    Hearts,
    Clubs,
    Spades,
    Diamonds
};

struct Card
{
    Suits suit;
    unsigned int value;

    Card(unsigned int val, Suits suit) : value(val), suit(suit)
    { }
}


Then I would code a functor that can generate an ordered deck:

1
2
3
4
class NewDeckGen
{
...
};


And I would use it in a call to std::generate() to create 1, 2 or whatever number of decks.

Finally, I would have used std::random_shuffle(). See the STL algorithm random_shuffle() explained @: http://www.cplusplus.com/reference/algorithm/random_shuffle/.

So basically I aggregate the two distinct properties of a card together inside a struct, and then create an array of such a struct to represent a deck.
Topic archived. No new replies allowed.