Thread: shuffling cards

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    41

    Exclamation shuffling cards

    what kind of script do i have to put in a black jack program so that after every hand of blackjack it "reshuffles" the cards? im working on the random function but i am just terrible at it so any help will be greatful. i'm also working on an array filled with the cards and i know i need a pointer so that the random factor will can point randomlly at cards stored in the array im just struggling with it. so as you can read i need a lot of help so please.........

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    There are probably a lot of different ways to do this.

    One way (probably non-optimal but it should work) would probably be to generate two random numbers and, since you say you're using an array, swap the elements at those two indexes.

    To get a random number between 0 and an upper limit first get a random number, then you use the modulo operator on it with the right operand as the upper limit.

    Make sure you seed the random number generator first with srand(time(0)).

    Small example:

    Code:
    char array[10];
    int rand1 = rand() % 10;
    int rand2 = rand() % 10;
    
    Swap( array[rand1], array[rand2] );
    Then repeat.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Are you having trouble with the code or with ideas?

    There are plenty of ways to shuffle a card deck. Some more realistic (in that they don't shuffle as fully), and some more random. How do you want to do it?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    41
    i want it pretty much to print out that it is shuffling and just put out 4 more random cards; two on one line and two on the next line. i am so lost on this blackjack program!!!!!!!!!

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You don't need to use pointers for shuffling cards, at all. Just use an index or two, and make it easy on yourself.





    random1 = rand() % 52; //range: 0 to 51, perfect!
    repeat to get random2;

    Now just swap random1 and random2: deck[random1], deck[random2].

    And look ma! No pointers!!

    You need to do this a lot, before the deck will really be shuffled thoroughly.

    Note that although this is "OK random", this is NOT REALLY as random as it needs to be to play blackjack for money. As always with random in C, you need to first (and just once), seed the random number generator, before you do that:

    srand();

    Look that up in your compiler's help, yours will be different than mine. You need to include stdlib.h, also.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by gloworm View Post
    i want it pretty much to print out that it is shuffling and just put out 4 more random cards; two on one line and two on the next line. i am so lost on this blackjack program!!!!!!!!!
    So make an attempt already! We aren't going to do it for you. If all you're going to do is tell us how hard it is, how much help you think you'll need, and how lost you are, then you aren't actually going to get anywhere towards helping yourself.

    Show some code and walk us through where you think your lack of understanding is.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 10-20-2009, 09:39 AM
  2. Help it won't compile!!!!!
    By esbo in forum C Programming
    Replies: 58
    Last Post: 01-04-2009, 03:22 PM
  3. Help!For poker game simulation
    By tx1988 in forum C++ Programming
    Replies: 24
    Last Post: 05-25-2007, 09:59 PM
  4. dealing cards
    By braddy in forum C Programming
    Replies: 15
    Last Post: 03-29-2006, 03:46 AM
  5. Cribbage Game
    By PJYelton in forum Game Programming
    Replies: 14
    Last Post: 04-07-2003, 10:00 AM

Tags for this Thread