card shuffle program

I am stuck on this incredibly basic function in a c++ program I want to write. can somedody tell me what I am doing wrong with this code that is just supposed to assign values to all 52 cards, or give me a simpler way to do this? (I have never used classes before, so I am not sure how to use them correctly)



#include
#include
#include
#include
#include

class Deck
{
public:
string cardSuit;
string cardFace;
int cardValue;
};


void DeckAssign(Deck& nds,int x,int y)
{

if (x==0)
{cardSuit="Spades";}
else if (x==1)
{cardSuit="Clubs";}
else if (x==2)
{cardSuit="Hearts";}
else if (x==3)
{cardSuit="Diamonds";}

if (y==1)
{cardFace="Ace";}
else if (y==2)
{cardFace="Two";}
else if (y==3)
{cardFace="Three";}
else if (y==4)
{cardFace="Four";}
else if (y==5)
{cardFace="Five";}
else if (y==6)
{cardFace="Six";}
else if (y==7)
{cardFace="Seven";}
else if (y==8)
{cardFace="Eight";}
else if (y==9)
{cardFace="Nine";}
else if (y==10)
{cardFace="Ten";}
else if (y==11)
{cardFace="Jack";}
else if (y==12)
{cardFace="Queen";}
else if (y==13)
{cardFace="King";}

if (y==1) //ace has highest value
{cardValue=14;}
else
{cardValue=y;}

}


int main()
{
Deck nds[51]; //allocate 52 sets for 52 cards (including nds[0])
int n=0;
for (int x=0;x<4;x++) //assigns 13 cards to each suit
{
for (int y=1;y<14;y++)
{
Deck(nds[n],x,y);
n++;
}
}

return(0);}

Comments

  • Ok, Here is a function that will shuffle them. This is not the perfect way to do it, there are many many ways, but this was just one that I cam up with off of the top of my head. It is more randomized, but it is less actual human shuffle, but anyways....
    [code]
    void swap(Deck & card_one, Deck & card_two)
    {
    Deck temp;
    temp = card_one;
    card_one = card_two;
    card_two = temp;
    return;
    }

    void main()
    {
    int x = 0, shuf, fle;
    Deck deck[52];
    //It goes 0 to n-1
    //Not 0 to n

    time_t seconds;
    time(&seconds);
    srand((unsigned int) seconds);
    while (x < 100)
    {
    randomize();
    shuf = rand() % 51 + 1;
    fle = rand() % 51 + 1;
    swap(deck[shuf], deck[fle]);
    x++;
    }
    getch();
    return;
    }[/code]

    Please ask if you have any questions about what I did...
  • : I am stuck on this incredibly basic function in a c++ program I want to write. can somedody tell me what I am doing wrong with this code that is just supposed to assign values to all 52 cards, or give me a simpler way to do this? (I have never used classes before, so I am not sure how to use them correctly)
    :
    :
    :
    : #include
    : #include
    : #include
    : #include
    : #include
    :
    : class Deck
    : {
    : public:
    : string cardSuit;
    : string cardFace;
    : int cardValue;
    : };
    :
    :
    : void DeckAssign(Deck& nds,int x,int y)
    : {
    :
    : if (x==0)
    : {cardSuit="Spades";}
    : else if (x==1)
    : {cardSuit="Clubs";}
    : else if (x==2)
    : {cardSuit="Hearts";}
    : else if (x==3)
    : {cardSuit="Diamonds";}
    :
    : if (y==1)
    : {cardFace="Ace";}
    : else if (y==2)
    : {cardFace="Two";}
    : else if (y==3)
    : {cardFace="Three";}
    : else if (y==4)
    : {cardFace="Four";}
    : else if (y==5)
    : {cardFace="Five";}
    : else if (y==6)
    : {cardFace="Six";}
    : else if (y==7)
    : {cardFace="Seven";}
    : else if (y==8)
    : {cardFace="Eight";}
    : else if (y==9)
    : {cardFace="Nine";}
    : else if (y==10)
    : {cardFace="Ten";}
    : else if (y==11)
    : {cardFace="Jack";}
    : else if (y==12)
    : {cardFace="Queen";}
    : else if (y==13)
    : {cardFace="King";}
    :
    : if (y==1) //ace has highest value
    : {cardValue=14;}
    : else
    : {cardValue=y;}
    :
    : }
    :
    :
    : int main()
    : {
    : [red]Deck nds[51]; //allocate 52 sets for 52 cards (including nds[0])[/red]
    [blue]I should tell you that Deck nds[51] will generate an array of 51 decks starting with an index of 0 to an index of 50![/blue]
    : int n=0;
    : for (int x=0;x<4;x++) //assigns 13 cards to each suit
    : {
    : for (int y=1;y<14;y++)
    : {
    : Deck(nds[n],x,y);
    : n++;
    : }
    : }
    :
    : return(0);}
    :

    Greets...
    Richard

  • Richard has it right, you are not creating a large enough array for the entire deck.

    May I offer another suggestion? In your function declaration for DeckAssign, use more descriptive names for your input parameters. Some thing like:
    void DeckAssign(Deck& cpDeck, int iSuit, int iValue)

    It will help you know what each parameter is for later.
    When you call the function in main it is OK to use x, and y since they are loop counters at that point.

    (Aside: consider using hungarian notation. Though it is falling out of fashion with the Java and .NET crowds, I think it is an easy and helpful tool - just don't get too carried away about it.)

  • : I am stuck on this incredibly basic function in a c++ program I want to write. can somedody tell me what I am doing wrong with this code that is just supposed to assign values to all 52 cards, or give me a simpler way to do this? (I have never used classes before, so I am not sure how to use them correctly)
    :
    :
    :
    : #include
    : #include
    : #include
    : #include
    : #include
    :
    : class Deck
    : {
    : public:
    : string cardSuit;
    : string cardFace;
    : int cardValue;
    : };
    :
    :
    : void DeckAssign(Deck& nds,int x,int y)
    : {
    :
    : if (x==0)
    : {cardSuit="Spades";}
    : else if (x==1)
    : {cardSuit="Clubs";}
    : else if (x==2)
    : {cardSuit="Hearts";}
    : else if (x==3)
    : {cardSuit="Diamonds";}
    :
    : if (y==1)
    : {cardFace="Ace";}
    : else if (y==2)
    : {cardFace="Two";}
    : else if (y==3)
    : {cardFace="Three";}
    : else if (y==4)
    : {cardFace="Four";}
    : else if (y==5)
    : {cardFace="Five";}
    : else if (y==6)
    : {cardFace="Six";}
    : else if (y==7)
    : {cardFace="Seven";}
    : else if (y==8)
    : {cardFace="Eight";}
    : else if (y==9)
    : {cardFace="Nine";}
    : else if (y==10)
    : {cardFace="Ten";}
    : else if (y==11)
    : {cardFace="Jack";}
    : else if (y==12)
    : {cardFace="Queen";}
    : else if (y==13)
    : {cardFace="King";}
    :
    : if (y==1) //ace has highest value
    : {cardValue=14;}
    : else
    : {cardValue=y;}
    :
    : }
    :
    :
    : int main()
    : {
    : Deck nds[51]; //allocate 52 sets for 52 cards (including nds[0])
    : int n=0;
    : for (int x=0;x<4;x++) //assigns 13 cards to each suit
    : {
    : for (int y=1;y<14;y++)
    : {
    : Deck(nds[n],x,y);
    : n++;
    : }
    : }
    :
    : return(0);}
    :
    Here is a way to implement what you need with classes and some STL..

    [code]
    #include <iostream>
    #include

    #include
    #include
    #include
    #include

    using namespace std;

    class Card
    {
    public:
    Card(const string &, const string &, int);
    private:
    string cardSuit;
    string cardFace;
    int cardValue;
    };

    Card::Card(const string &suit, const string &face, int value)
    :cardSuit(suit),
    cardFace(face),
    cardValue(value){}

    class Deck
    {
    public:
    void buildLookupTable();
    void buildDeck();
    void shuffleDeck();
    private:
    vector vDeck;
    map cardLookup;
    vector suit;
    };

    void Deck::buildLookupTable()
    {
    suit.push_back("Spades");
    suit.push_back("Clubs");
    suit.push_back("Hearts");
    suit.push_back("Diamonds");
    cardLookup.insert(make_pair(string("Ace"), 11));
    cardLookup.insert(make_pair(string("Two"), 2));
    cardLookup.insert(make_pair(string("Three"), 3));
    cardLookup.insert(make_pair(string("Four"), 4));
    cardLookup.insert(make_pair(string("Five"), 5));
    cardLookup.insert(make_pair(string("Six"), 6));
    cardLookup.insert(make_pair(string("Seven"), 7));
    cardLookup.insert(make_pair(string("Eight"), 8));
    cardLookup.insert(make_pair(string("Nine"), 9));
    cardLookup.insert(make_pair(string("Ten"), 10));
    cardLookup.insert(make_pair(string("Jack"), 10));
    cardLookup.insert(make_pair(string("Queen"), 10));
    cardLookup.insert(make_pair(string("King"), 10));
    }

    void Deck::buildDeck()
    {
    map::iterator map_iter;
    vector::iterator suit_iter = suit.begin();
    while(suit_iter != suit.end())
    {
    for(map_iter = cardLookup.begin(); map_iter != cardLookup.end(); ++map_iter)
    vDeck.push_back(Card(*suit_iter, map_iter->first, map_iter->second));
    ++suit_iter;
    }
    }

    void Deck::shuffleDeck()
    {
    random_shuffle(vDeck.begin(), vDeck.end());
    }

    int main(int argc, char* argv[])
    {
    srand(time(NULL));
    Deck d;
    d.buildLookupTable();
    d.buildDeck();
    d.shuffleDeck();
    return 0;
    }
    [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories