Class Problem

This is a discussion on Class Problem within the C++ Programming forums, part of the General Programming Boards category; Ok, I'm trying to make a card class that can shuffle a deck of cards so I never have to ...

  1. #1
    Seņor Member
    Join Date
    Jan 2002
    Posts
    561

    Angry Class Problem

    Ok, I'm trying to make a card class that can shuffle a deck of cards so I never have to write this shuffle code again. I have the two files here and attached to this post. I'm gettin some error, it's probably has something to do with my class syntax. It was written in dev c++ 4. In these files, I actually documented a lot (something i rarely do).


    heres the header file
    //cardclass.h
    //makes, and suffles a deck of cards
    //including suits, numbers (1-13), and faces

    // IMPLEMENTATION IS EXPLAINED At THE END OF THE CPP FILE

    #ifndef_CARDCLASSMADEBYTIMSEGANPINKBIGFLOATINGELEP HANTFROMCUBA
    #define_CARDCLASSMADEBYTIMSEGANPINKBIGFLOATINGELEP HANTFROMCUBA

    #include <apvector.h> //needed for card aray
    #include <apstring.h> //needed for faces
    #include <stdlib.h> //needed for randomizing
    //#include <windows.h> //not sure if it is needed


    struct cardtype
    { //TYPE OF DATA USED IN ARRAYS
    //USAGE: card[1].suit

    int num; //number (1-13)
    int suit; //Suit (3-6)
    apstring face; //1...9, q, a, (10 = t)
    int val; //card's value (although ace is tricky)
    };


    class cardclass
    {
    public:
    cardclass();
    //there really isnt a need for any public data
    void shuffle();

    private:
    apvector<cardtype>card(53);
    int num (int); //returns # of card (1-13)
    apstring face (int); //returns face ("A", "10"...)
    char csuit (int); //returns char of suit (pic of suit)
    int nsuit (int); //returns # of suit (3-6)

    };

    #include "cardclass.cpp"

    #endif

    // CARDCLASS.CPP

    cardclass::cardclass()
    {
    apvector<cardtype>card; //dont know if this is necessary
    }

    void cardclass::shuffle()
    {
    //ERASING
    for (int i = 1; i<=52; i++)
    {
    card[i].num=0;
    card[i].suit=0; //sets all aspects of card to blank
    card[i].face="";
    card[i].val=0;
    }





    //SHUFFLING
    for (int i = 1; i<=52; i++) //i is simple loop variable
    {
    RERANDOM:
    card[i].num=rand()%13+1; //random card
    card[i].suit=rand()%4+3; //random suit
    bool uni=true; //uni=unique
    if (i!=1) //if its 1, it is unique (first card has to be...)
    {
    for(int t=1; t<i; t++) //check loop
    {
    if (card[i].suit==card[t].suit && card[i].num==card[t].num) //if cards are the same
    bool uni=false;
    }
    }
    if (uni==false) //if card isnt unique
    goto RERANDOM; //rerandomize the card
    }
    }


    int cardclass::num(int i)
    {
    return card[i].num; //RETURNING NUM (1-13)
    }

    apstring cardclass::face(int i)
    {
    return card[i].face; //RETURNING FACE
    }

    char csuit (int i)
    {
    return char(card[i].suit); //returning character value of suit
    }

    int nsuit (int i)
    {
    return card[i].suit; //RETURNING SUIT # (1-6)
    }


    /*GET THE SUIT TO WORK BY

    #include "cardclass.h"

    ...
    cardclass card();
    card.suffle();
    cout<<card.num(5);

    WOULD PRINT OUT # OF CARD ON SCREEN
    */



    I would REALLY appreciate the fix(es) for my problems here because I am the worst debugger ever.
    Last edited by tim545666; 01-14-2002 at 07:26 PM.

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    Rather than spoonfeed you, here's another solution.

    I've not made this the best solution coz i think it's a good idea for you to think about it yourself. It works, you don't get duplicates, and there's no nasty GOTO statements in it (BTW, dont EVER use gotos).

    have a look through this code and try and fix your problems using it:

    Code:
    #include <iostream.h>
    #include <time.h>
    #include <stdlib.h>
    
    enum
    {
    	HEARTS, DIAMONDS, CLUBS, SPADES, NUM_SUITS
    };
    
    typedef struct _Card
    {
    	int suit;
    	int value;
    } Card;
    
    const int cardsPerSuit = 13;
    const int totalCards = NUM_SUITS * cardsPerSuit;
    const char* suits[NUM_SUITS] = { "Hearts", "Diamonds", "Clubs", "Spades" };
    
    int Shuffle(Card* cards);
    int Display(Card* cards);
    
    int main(void)
    {
    	int card = 0;
    	Card cards[totalCards];
    
    	// create a standard deck of cards
    	for(int i = HEARTS; i < NUM_SUITS; i++)
    	{
    		for(int j = 0; j < cardsPerSuit; j++, card++)
    		{
    			cards[card].suit = i;
    			cards[card].value = j + 1;
    		}
    	}
    
    	// display the cards in order
    	Display(cards);
    
    	// shuffle them
    	Shuffle(cards);
    
    	// display the deck again shuffled
    	Display(cards);
    
    	return(0);
    }
    
    int Shuffle(Card* cards)
    {
    	// set the randomiser seed
    	srand(time(NULL));
    
    	for(int i = 0; i < totalCards; i++)
    	{
    		// get a random card from the deck
    		int rnd = rand() % totalCards;
    
    		// swap it with the current card
    
    		// first swap the value
    		int temp = cards[i].value;
    		cards[i].value = cards[rnd].value;
    		cards[rnd].value = temp;
    
    		// then swap the suit
    		temp = cards[i].suit;
    		cards[i].suit = cards[rnd].suit;
    		cards[rnd].suit  = temp;
    	}
    
    	return(1);
    }
    
    int Display(Card* cards)
    {
    	for(int j = 0; j < totalCards; j++)
    	{
    		cout << "Card " << j + 1 << " is the ";
    
    		switch(cards[j].value)
    		{
    		case 1: // this is the ace
    			{
    				cout << "Ace";
    				break;
    			}
    		case 11: // this is the jack
    			{
    				cout << "Jack";
    				break;
    			}
    		case 12: // this is the jack
    			{
    				cout << "Queen";
    				break;
    			}
    		case 13: // this is the jack
    			{
    				cout << "King";
    				break;
    			}
    		default:
    			{
    				 cout << cards[j].value;
    				break;
    			}
    		}
    
    		cout << " of " << suits[cards[j].suit] << endl;
    	}
    
    	cout << endl;
    
    	return(1);
    }
    hope this helps!
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  3. #3
    Seņor Member
    Join Date
    Jan 2002
    Posts
    561
    Thanks, that's a better way to do it, but i already have this EXACT shuffling code in a program that is working, so thats not where the error is coming from... help




    and btw, goto is a habit of mine that i pickeed up from BASIC (stupid language), I figured out a better way but i was just too lazy to change it. I know not to use it but I was in a hurry and didn't feel like thinking of a diff. way to do it.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    card shuffle problem?

    Why do you have the following line in your code?


    #include "cardclass.cpp"

    #endif

    .cpp files must not be included. Only .h files...
    zMan

  5. #5
    Seņor Member
    Join Date
    Jan 2002
    Posts
    561
    No, you can inculde cpp files, it's how we are taught to do it in comp sci ap and I've done it before.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mesh Class Design problem
    By sarah22 in forum Game Programming
    Replies: 2
    Last Post: 05-20-2009, 04:52 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  5. Replies: 3
    Last Post: 12-03-2001, 12:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21