1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Deck of Cards in C++ Help!

Discussion in 'C++' started by Rangerbob86, Mar 31, 2010.

  1. Rangerbob86

    Rangerbob86 New Member

    Joined:
    Mar 31, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I am trying to make a deck of cards using a vector that shuffles, and then prints out the deck using a toString function. I am close, but some weird things are happening...

    Card.cpp
    Code:
    #include <iostream>
    #include "Card.h"
    #include "DeckOfCards.h"
    using namespace std;
    
    int face, suit;
    static string faces[14] = {"One", "Two", "Three", "Four", "Five", "Six", "Seven",
    "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"};
    static string suits[4] = {"Clubs", "Spades", "Diamonds", "Hearts"};
    
    Card::Card(int aFace, int aSuit)
    {
        face = aFace;
        suit = aSuit;
    }
    
    string Card::toString()
    {
        return(faces[face] + " of " + suits[suit]);
    }
    
    DeckOfCards.cpp
    Code:
    #include <iostream>
    #include <vector>
    #include "Card.h"
    #include "DeckOfCards.h"
    using namespace std;
    
    vector<Card> deck;
    int currentCard = -1;
    
    DeckOfCards::DeckOfCards()
    {
        for(int i = 0; i < 13; i++)
        {
            for(int j = 0; j < 4; j++)
            {
                deck.push_back(Card(i, j));
            }
        }
    }
    
    void DeckOfCards::shuffle()
    {
        srand (time(NULL));
        random_shuffle (deck.begin(), deck.end());
    }
    
    Card DeckOfCards::dealCard()
    {
        currentCard++;
        return deck.at(currentCard);
    }
    
    int DeckOfCards::getCurrentCard()
    {
        return currentCard;
    }
    
    bool DeckOfCards::moreCards()
    {
        if(currentCard < 52)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    DeckOfCardsDriver.cpp
    Code:
    #include <iostream>
    #include "Card.h"
    #include "DeckOfCards.h"
    using namespace std;
    
    int main()
    {
        DeckOfCards deck;
    
        //deck.shuffle();
        for(int i = 0; i < 52; i++)
        {
            cout << deck.dealCard().toString() << endl;
            cout << deck.getCurrentCard() << endl;
        }
    
        return(0);
    }
    
    Theres also 2 header files. Any help of why the cards arent actually changing would be great...I've been looking at it for a while now...thanks!
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Could it be because deck.shuffle() is commented out?
     

Share This Page