Random card dealer function

This is a discussion on Random card dealer function within the C++ Programming forums, part of the General Programming Boards category; I just made a function that will return a random card from a sorted deck, but I cant get it ...

  1. #1
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115

    Random card dealer function

    I just made a function that will return a random card from a sorted deck, but I cant get it to stop shuffling the cards every time I goto retrieve one,
    Code:
    char get_card(short int c)
    {
           vector<char> cards;
                cards.push_back('A');
                cards.push_back('2');
                cards.push_back('3');
                cards.push_back('4');
                cards.push_back('5');
                cards.push_back('6');
                cards.push_back('7');
                cards.push_back('8');
                cards.push_back('9');
                cards.push_back('10');
                cards.push_back('J');
                cards.push_back('Q');
                cards.push_back('K');
                
           for (int i=0; i<=randomNumber(0); i++) 
           {
               random_shuffle(cards.begin(), cards.end());
           }
           return cards[c];
    }
    Anyone have an idea how to make it so that, that loop will only run ounce? I was thinking of using a “if” statement, but cant get it to work..

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    - create a deck (once)
    - shuffle the deck (once)
    - call get_card() a number of times, removing one card from the shuffled deck.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,604
    Wouldn't it be simpler to just generate a random integer in the range [0,13) and return the card corresponding to that?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Quote Originally Posted by laserlight View Post
    Wouldn't it be simpler to just generate a random integer in the range [0,13) and return the card corresponding to that?
    Well for the game I am writing to work, I need the cards to shuffle at the start of each game and I need to retrieve the next card at different points in the game… And obviously I cant get the same card twice, which is why I used a function…

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,604
    Yeah, I see what you mean. Follow Salem's advice.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    Create a class called "deck"
    Create a method called "shuffle"
    Create a method called "deal"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Quote Originally Posted by Salem View Post
    Create a class called "deck"
    Create a method called "shuffle"
    Create a method called "deal"
    Sorry im not familiar with class's, and methods... i think ill have to look into it..

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    I thought that was the whole point of using C++

    > cards.push_back('10');
    BTW, '10' is not a single character.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Quote Originally Posted by Salem View Post
    I thought that was the whole point of using C++

    > cards.push_back('10');
    BTW, '10' is not a single character.
    Still in my first year of uni, my class hasn't covered that yet. only just learned functions this week.

    But thats for your help, ill research class's and methods on google...

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    In any case, you should probably fill the deck only once. You also shouldn't shuffle it in the function that deals a card: otherwise it will be just the same as returning a random value between 0 and 13 (the cards will repeat).

    With classes you can naturally have a variable in the Deck class that represents the index of the next card to be dealt, so you'll never need to worry about getting a wrong card. How this value is managed would be completely transparent in the outside world (similarly how the vector automatically "knows" its size). Without classes you'll need to manage this value yourself (in your code you'll need to ensure that you pass the right argument to the function).

  11. #11
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Can someone point me in the right direction for some info on class's? I'm having a bit of trouble finding something that appears to be relevant to a deck of cards? :S

  12. #12
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    I have made a few changes to the funtion, as i cant work out how to use class's...

    Code:
    char get_card(int c, int b)
    {
           vector<char> cards;
                cards.push_back('A');
                cards.push_back('2');
                cards.push_back('3');
                cards.push_back('4');
                cards.push_back('5');
                cards.push_back('6');
                cards.push_back('7');
                cards.push_back('8');
                cards.push_back('9');
                cards.push_back('10');
                cards.push_back('J');
                cards.push_back('Q');
                cards.push_back('K');
                
           if (b == '1')
           {
            for (int i=0; i<=randomNumber(0); i++) 
           {
               random_shuffle(cards.begin(), cards.end());
           }
           }
           return cards[c];
    }
    What i am trying to do is, if i give it a value of 1 for b shuffle the cards, other wise leave that part of code out... But when i try to compile it. i get this error...
    too few arguments to function `char get_card(int, int)'

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Are you sending two arguments to the function?

    It won't work anyway, though. The vector of cards is created every time you call the function, so if you send b = '1', then you will get a card from a shuffled deck, but if you do not send b = '1', then you will get a card from the unshuffled deck.

    What you want is to create the vector<char> outside that function. Then call random_shuffle in the same place that you create the vector cards or pass it by reference to a new function that initializes and shuffles it. You can also create a function that returns a vector<char> and just save the return value.

    Whatever you do, your get_card code should not be creating and initializing al local vector, you need to save the deck in the calling function.

  14. #14
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    ah righto, makes sense... cheers mate...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Function
    By T1m in forum C Programming
    Replies: 5
    Last Post: 01-06-2009, 07:41 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Segmentation Fault - aaaaaaaah!
    By yogibear in forum C Programming
    Replies: 6
    Last Post: 10-01-2007, 03:21 AM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM

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