Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

c++ card deck class help

  • 08-03-2009 02:33PM
    #1
    Registered Users, Registered Users 2 Posts: 244 ✭✭


    as part of an assignment, i need a deck of cards class. ive done most of it but im not getting the output i expected, the main is just testing it -
    2 D (2 of diamonds)
    3 D
    4 D
    ...
    K D
    A D
    2 C
    3 C
    4 C
    ...
    K S
    A S (ace of spades)


    heres what ive got
    //deck.h
    class cDeckOfCards
    {
    private:
    int deck[51];
    int cardPointer;
    public:
    cDeckOfCards();
    void shuffle();
    void drawCard();
    };
    //deck.cpp
    #include"deck.h"
    #include<iostream>
    using namespace std;

    cDeckOfCards::cDeckOfCards()
    {
    cardPointer=0;
    int i=0;
    for (i=0; i<52; i++)
    deck=i;
    }

    void cDeckOfCards::shuffle()
    {
    }

    void cDeckOfCards::drawCard()
    {
    int tempCard=deck[cardPointer];
    if ((((tempCard+13)%13)+2)<11)
    cout << (((tempCard+13)%13)+2);
    else if ((((tempCard+13)%13)+2)==11)
    cout << "J";
    else if ((((tempCard+13)%13)+2)==12)
    cout << "Q";
    else if ((((tempCard+13)%13)+2)==13)
    cout << "K";
    else
    cout << "A";

    if ((tempCard/13)==0)
    cout << " D";
    else if ((tempCard/13)==1)
    cout << " C";
    else if ((tempCard/13)==2)
    cout << " H";
    else
    cout << " S";

    cardPointer++;
    if (cardPointer=52)
    cardPointer=0;
    }
    //main.cpp
    #include "deck.h"
    #include <iostream>

    using namespace std;

    int main()
    {
    cDeckOfCards *newDeck = new cDeckOfCards;
    int i = 0;
    for (i=0; i<200; i++)
    {
    newDeck->drawCard();
    cout << endl;
    }
    system("PAUSE");
    return 0;
    }
    the output im getting is
    A S
    2 D
    2 D
    2 D...199 times


Comments

  • Registered Users, Registered Users 2 Posts: 244 ✭✭theliam


    any suggestions?


  • Registered Users, Registered Users 2 Posts: 2,744 ✭✭✭Darwin


    The conditional statement:

    if (cardPointer=52)

    is in fact performing an assignment, i.e. assing 52 to cardPointer, the result of which is always true. Thus, cardPointer is reset to 0 each time drawCard() is invoked. What you want instead is an equality test:

    if (cardPointer==52)


  • Registered Users, Registered Users 2 Posts: 244 ✭✭theliam


    Darwin wrote: »
    The conditional statement:

    if (cardPointer=52)

    is in fact performing an assignment, i.e. assing 52 to cardPointer, the result of which is always true. Thus, cardPointer is reset to 0 each time drawCard() is invoked. What you want instead is an equality test:

    if (cardPointer==52)

    thanks:o


  • Registered Users, Registered Users 2 Posts: 1,451 ✭✭✭Onikage


    You do realise that int deck[51]; will give you an array with 51 elements, not 52?


  • Registered Users, Registered Users 2 Posts: 26,449 ✭✭✭✭Creamy Goodness


    also the if statements are very difficult to follow.

    store this in a variable i.e.

    int cardNum = ((tempCard+13)%13)+2;

    then do your conditional statements based on the value inside the variable.
    also i'd tend to use a switch when there's more than one else if.

    also posting up code in [.code][./code] (removing the dots.) tags will preserve indentation.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 244 ✭✭theliam


    Onikage wrote: »
    You do realise that int deck[51]; will give you an array with 51 elements, not 52?

    damn, always forget that with the 0th term thing...


  • Registered Users, Registered Users 2, Paid Member Posts: 11,210 ✭✭✭✭Crash


    Also, just as a logic thing:

    int cardNum = ((tempCard+13)%13)+2;

    is the same as

    int cardNum = (tempCard%13)+2;

    as an example, say tempcard =6;

    (6+13) % 13 is 6. as is 6%13.


  • Registered Users, Registered Users 2 Posts: 244 ✭✭theliam


    doing the shuffle function now and it works... but i forget how to get a different set of random numbers everytime its run...
    void cDeckOfCards::shuffle()
    {
    	int randomNum;
    	for (int i=0; i<52; i++)
    	{
    		randomNum = (rand()%52);
    		swap(deck[i], deck[randomNum]);
    	}
    }
    


  • Registered Users, Registered Users 2 Posts: 3,945 ✭✭✭Anima


    That was used in another recent thread actually. You need to set the seed. A lot of people use the time because its always changing.


Advertisement