Shuffling Cards

Jul 30, 2013 at 4:17pm
Hi, I'm new with C++ and trying to get myself familiarize with the syntax as well. However, I can't seem to get my program to work perfectly fine. Can somebody help me with getting my program works by using pointers? I wish to show a pack of 52 cards which has been shuffled (without repeating the same cards which I've already drawn). I can't seem to get this work still. Thanks.

 
Last edited on Aug 1, 2013 at 3:31am
Jul 30, 2013 at 4:26pm
closed account (Dy7SLyTq)
personally i would use vectors, and then do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
template<class Type>
void Shuffle(vector<Type> &Deck)
{
	vector<Type> Copy(Deck);
	int Counter, Max, Current;

	Max = Deck.size();

	srand((unsigned)time(NULL));
	Deck.clear();

	for(int Counter = 0; Counter < Max; Counter++)
	{
		Current = rand()%Copy.size();
		Deck.pushback(Copy[Current]);
		Copy.erase(Current);
	}
}
Jul 30, 2013 at 4:30pm
Nope, it was a requirement to build this using pointers. Oh well. But thanks!
Jul 30, 2013 at 4:31pm
closed account (Dy7SLyTq)
oh sorry i havent had to work with pointer in that way for a while so ill be no help then
Jul 30, 2013 at 5:05pm
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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

enum Suit { Spades, Hearts, Diamonds, Clubs };

struct Card
{
    Suit suit;
    unsigned face;
};

std::ostream& operator<<(std::ostream& os, const Card& card)
{
    const char* suits [] = {"Spades", "Hearts", "Diamonds", "Clubs"};
    const char* faces [] = { "", "Ace", "Deuce", "Three", "Four", "Five", "Six", 
                             "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
    const char* unknown = "Unknown";

    const char* suit = (card.suit >= Spades && card.suit <= Clubs) ? suits[card.suit] : unknown;
    const char* face = (card.face >= 1 && card.face <= 13) ? faces[card.face] : unknown;

    return os << face << " of " << suit;
}

void print(const Card* deck, unsigned cards)
{
    const Card* card = deck;
    for (unsigned i = 0; i < cards; ++i)
        std::cout << *card++ << '\n';
}

int main()
{
    Card cards[52];
    print(cards, 52);

    std::cout << "\nWhat must we do with data before we use it?  Initialize it?  You don't say!\n";
}

Unknown of Unknown
Unknown of Unknown
[...]
Unknown of Unknown
Unknown of Unknown

What must we do with data before we use it?  Initialize it?  You don't say!
Last edited on Jul 30, 2013 at 5:30pm
Jul 30, 2013 at 5:45pm
Do you ever uses vectors?

You can use vectors and call the shuffle algorithm and it will shuffle up the vector array
Jul 30, 2013 at 5:57pm
you will need #include<algorithm> and #include<vector>. What you might wanna have is
vect<int>v ;
// some push back calls
random_shuffle(v.begin(),v.end());
Jul 30, 2013 at 6:15pm
@erock88 and jkevin:
He has a requirement to use pointers for this program vectors are going to irrelevant.
Jul 30, 2013 at 6:22pm
it is not possible to have a pointer to a vector object?
Jul 30, 2013 at 6:31pm
@ahchua -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void shuffle (Card* c, int size)
{
// I've tried running a loop here while calling getACard but it does not seem to work properly
loop until the deck is full
  call getACard()
  check if card is in deck - i.e. call cardInDeck()
    if not in deck add to deck and increment counter
}

bool cardInDeck (Card, Card *, int)
{
// what should I be placing here?
  iterate through deck array
    if card is in deck return true
    else return false
}


Edit: Fumble fingered typing
Last edited on Jul 30, 2013 at 6:41pm
Jul 30, 2013 at 6:31pm
It is possible, but I doubt that was a solution he wanted. It sounded like he wanted pointers to the card structures. So you would have a pointer to a container he may have never used before that contains pointers that point to cards. When tutoring new students at university, I find this topic to be confusing for beginners just learning pointers.
Last edited on Jul 30, 2013 at 6:32pm
Jul 30, 2013 at 6:58pm
Can't you just use the example provided by http://www.cplusplus.com/reference/algorithm/shuffle/ ?

Won't it work if you replace the begin and end iterators by c and c+size?
Jul 30, 2013 at 7:14pm
The following worked satisfactorily with pointers.

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
#include <ctime>
#include <iostream>
#include <algorithm>
#include <random>

const int STUPID_ARRAY_SIZE = 50;

int main()
{
	int someStupidArray[STUPID_ARRAY_SIZE];
	
	for (int i = 0; i < STUPID_ARRAY_SIZE; ++i) {
		someStupidArray[i] = i + 1;
	}
	
	unsigned seed = std::time(0);
	
	std::shuffle(
		&someStupidArray[0],
		&someStupidArray[0] + STUPID_ARRAY_SIZE,
		std::default_random_engine(seed));
	
	for (int i = 0; i < STUPID_ARRAY_SIZE; ++i) {
		std::cout << someStupidArray[i] << '\n';
	}
	
	return 0;
}
Last edited on Jul 30, 2013 at 7:15pm
Jul 31, 2013 at 3:36am
@norm b

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void shuffle (Card* c, int size)
{
// I've tried running a loop here while calling getACard but it does not seem to work properly
loop until the deck is full
  call getACard()
  check if card is in deck - i.e. call cardInDeck()
    if not in deck add to deck and increment counter
}

bool cardInDeck (Card, Card *, int)
{
// what should I be placing here?
  iterate through deck array
    if card is in deck return true
    else return false
}


Hi, thanks for your help. However, am I supposed to create a variable deck in my shuffle() function as well?
Jul 31, 2013 at 4:13am
am I supposed to create a variable deck in my shuffle() function as well?
No. Sorry by deck (of cards) I meant the array which you are passing in. Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void shuffle (Card* c, int size) {
    int count = 0;
    while(count<size) {
            Card card = getACard();
            if(!cardInDeck(card, c, count)){ //if the card is not in the array, add it.	
                c[count] = card;
                ++count;
            }
    }
}

bool cardInDeck (Card card, Card *c, int count) { // changed the signature of this function to only
                                                  // iterate over what's in the array.same signature
    for(int i=0; i<count; ++i){  
        if(card.rank==c[i].rank && card.c == c[i].c) 
            return true;
    }
    return false;
}


HTH

Last edited on Jul 31, 2013 at 4:27am
Jul 31, 2013 at 8:44am
@norm b

Why does my output always print out gibberish characters? Hmmm.

 


The part which I've bold and underline, am I doing that correctly?
Last edited on Aug 1, 2013 at 3:31am
Jul 31, 2013 at 10:53am
You are not returning anything from getACard().
Jul 31, 2013 at 12:21pm
@Zhuge

Hi, alright. I returned C and its working now. Thanks!
Topic archived. No new replies allowed.