Thread: Not generating the same card twice?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by jamort View Post
    So after about and hour or two of a headache with errors I finally got this things figured a little better (I hope)...

    Code:
    void CardHandler::shuffle(){
        for(int x; x < 1000; x++)
            std::swap(deck[rand() % 52], deck[rand() % 52]);
    }
    Believe it or not, shuffle code is usually handled sequentially. This reduces the number of swaps you have to do while ensuring that every card has been touched at least once. Something like:
    Code:
    for(int i = 0; i < 51, ++i)
       std::swap(deck[i], deck[i + 1 + rand() % (51 - i));
    Quote Originally Posted by jamort View Post
    Code:
    for(int face = 0; face < 13; face++){
            for(int i = 0; i < 4; i++){
                deck[face].myFace = face;
                deck[face + 13].myFace = face;
                deck[face + 26].myFace = face;
                deck[face + 39].myFace = face;
    
            }
        }
    Note that the inner for loop here does nothing. The value of i is never used. You're just assigning four values four times to the same four places.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    106
    Quote Originally Posted by pianorain View Post
    Believe it or not, shuffle code is usually handled sequentially. This reduces the number of swaps you have to do while ensuring that every card has been touched at least once. Something like:
    Code:
    for(int i = 0; i < 51, ++i)
       std::swap(deck[i], deck[i + 1 + rand() % (51 - i));
    Note that the inner for loop here does nothing. The value of i is never used. You're just assigning four values four times to the same four places.
    okay so... I get what you're saying about the swaps, good point. The for loop I eventually took out reading back through the code I found it. and well here's my code so far that actually allows you to play, only lacking the full code for if the two cards thrown are the same, which I'm probably going to implement later on today.

    Code:
    #include <vector>
    #include <iostream>
    #include <time.h>
    
    #define NUMBER_OF_CARDS 52
    
    enum CardFace
    {
    	TWO = 0, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE
    };
    
    enum CardSuit
    {
        HEARTS = 0, DIAMONDS, SPADES, CLUBS
    };
    
    typedef struct
    {
    	int myFace;
    	int mySuit;
    
    } Card;
    
    class CardHandler{
    	Card deck[NUMBER_OF_CARDS];
    	std::vector <Card> playersHand;
    	std::vector <Card> computersHand;
    	public:
    		CardHandler();
    		void shuffle();
    		void deal();
    		void play();
    		void throwCards();
    		int playerReturn();
    		int computerReturn();
    		void checkWin(int, int);
    		void declareWar();
    };
    
    void CardHandler::shuffle()
    {
        for(int i = 0; i < 51; i++ )
            std::swap(deck[i], deck[i + 1 + rand() % (51 - i)]);
    }
    
    CardHandler::CardHandler(){
        for(int i = 0; i < 13; i++)
        {
            deck[i].mySuit = HEARTS;
        }
        for(int i = 13; i < 26; i++)
        {
            deck[i].mySuit = DIAMONDS;
        }
        for(int i = 26; i < 39; i++)
        {
            deck[i].mySuit = SPADES;
        }
        for(int i = 39; i < 52; i++)
        {
            deck[i].mySuit = CLUBS;
        }
        for(int face = 0; face < 13; face++)
        {
            deck[face].myFace = face;
            deck[face + 13].myFace = face;
            deck[face + 26].myFace = face;
            deck[face + 39].myFace = face;
        }
    }
    void CardHandler::deal()
    {
        playersHand.clear();
        computersHand.clear();
        for(int i = 0; i < NUMBER_OF_CARDS; i += 2){
            computersHand.push_back(deck[i]);
            playersHand.push_back(deck[i + 1]);
        }
    }
    
    void CardHandler::play()
    {
        std::cout<< "\nNumber of player cards: " << playersHand.size() << "\n";
        std::cout<< "\nNumber of computer cards: " << computersHand.size() << "\n";
        throwCards();
        std::cin.get();
    }
    
    void CardHandler::throwCards(){
       int playersValue = playerReturn();
       int computersValue = computerReturn();
       checkWin(playersValue, computersValue);
    
    }
    
    int CardHandler::playerReturn(){
    	std::cout<< "\n\nYour card: ";
    	switch(playersHand[0].myFace)
            {
                case TWO:
                std::cout<< "\nTwo";
                break;
    
                case THREE:
                std::cout<< "\nThree";
                break;
    
                case FOUR:
                std::cout<< "\nFour";
                break;
    
                case FIVE:
                std::cout<< "\nFive";
                break;
    
                case SIX:
                std::cout<< "\nSix";
                break;
    
                case SEVEN:
                std::cout<< "\nSeven";
                break;
    
                case EIGHT:
                std::cout<< "\nEight";
                break;
    
                case NINE:
                std::cout<< "\nNine";
                break;
    
                case TEN:
                std::cout<< "\nTen";
                break;
    
                case JACK:
                std::cout<< "\nJack";
                break;
    
                case QUEEN:
                std::cout<< "\nQueen";
                break;
    
                case KING:
                std::cout<< "\nKing";
                break;
    
                case ACE:
                std::cout<< "\nAce";
                break;
            }
            std::cout<< " of ";
            switch(playersHand[0].mySuit)
            {
                case HEARTS:
                std::cout<< "Hearts";
                break;
    
                case DIAMONDS:
                std::cout<< "Diamonds";
                break;
    
                case SPADES:
                std::cout<< "Spades";
                break;
    
                case CLUBS:
                std::cout<< "Clubs";
                break;
            }
    		return playersHand[0].myFace;
    }
    
    int CardHandler::computerReturn(){
    	std::cout<<"\n\nComputer card: ";
    	switch(computersHand[0].myFace)
            {
                case TWO:
                std::cout<< "\nTwo";
                break;
    
                case THREE:
                std::cout<< "\nThree";
                break;
    
                case FOUR:
                std::cout<< "\nFour";
                break;
    
                case FIVE:
                std::cout<< "\nFive";
                break;
    
                case SIX:
                std::cout<< "\nSix";
                break;
    
                case SEVEN:
                std::cout<< "\nSeven";
                break;
    
                case EIGHT:
                std::cout<< "\nEight";
                break;
    
                case NINE:
                std::cout<< "\nNine";
                break;
    
                case TEN:
                std::cout<< "\nTen";
                break;
    
                case JACK:
                std::cout<< "\nJack";
                break;
    
                case QUEEN:
                std::cout<< "\nQueen";
                break;
    
                case KING:
                std::cout<< "\nKing";
                break;
    
                case ACE:
                std::cout<< "\nAce";
                break;
            }
            std::cout<< " of ";
            switch(computersHand[0].mySuit)
            {
                case HEARTS:
                std::cout<< "Hearts";
                break;
    
                case DIAMONDS:
                std::cout<< "Diamonds";
                break;
    
                case SPADES:
                std::cout<< "Spades";
                break;
    
                case CLUBS:
                std::cout<< "Clubs";
                break;
            }
    		return computersHand[0].myFace;
    }
    void CardHandler::checkWin(int playersValue, int computersValue){
        if(playersValue == computersValue){
            declareWar();
        } else if(playersValue < computersValue){
            std::cout<< "\n\nComputer wins\n";
            computersHand.push_back(computersHand[0]);
            computersHand.push_back(playersHand[0]);
        } else if(playersValue > computersValue){
            std::cout<< "\n\nPlayer wins\n";
            playersHand.push_back(computersHand[0]);
            playersHand.push_back(playersHand[0]);
        }
        playersHand.erase(playersHand.begin());
        computersHand.erase(computersHand.begin());
    }
    
    void CardHandler::declareWar(){
        std::cout<< "\n----I----DECLARE----WAR----\n";
    
    
    }
    
    int main()
    {
    	srand(time(NULL));
    	CardHandler one;
    	one.shuffle();
    	one.deal();
    	bool playing = true;
    	do{
    	    one.play();
    	} while(playing);
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help With a BlackJack Program in C
    By Jp2009 in forum C Programming
    Replies: 15
    Last Post: 03-30-2009, 10:06 AM
  2. Bitwise Unwanted Output
    By pobri19 in forum C++ Programming
    Replies: 4
    Last Post: 09-15-2008, 04:07 AM
  3. Vector out of range program crash.
    By Shamino in forum C++ Programming
    Replies: 11
    Last Post: 01-18-2008, 05:37 PM
  4. Segmentation Fault - aaaaaaaah!
    By yogibear in forum C Programming
    Replies: 6
    Last Post: 10-01-2007, 03:21 AM
  5. Blackjack
    By Tommo in forum C Programming
    Replies: 10
    Last Post: 06-20-2007, 08:07 PM