Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Deck of Cards in C++ Help! (http://www.go4expert.com/forums/deck-cards-cpp-help-t21594/)

Rangerbob86 31Mar2010 07:11

Deck of Cards in C++ Help!
 
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!

xpi0t0s 31Mar2010 12:32

Re: Deck of Cards in C++ Help!
 
Could it be because deck.shuffle() is commented out?


All times are GMT +5.5. The time now is 21:50.