playing ... cards??

This is a discussion on playing ... cards?? within the C Programming forums, part of the General Programming Boards category; hey People... I was trying to make a program that would play cards, BUT (there is always a butt - ...

  1. #1
    Registered User GiraffeMan's Avatar
    Join Date
    Feb 2002
    Posts
    20

    Unhappy playing ... cards??

    hey People...
    I was trying to make a program that would play cards, BUT (there is always a butt - the random function (to shuffle the cards) could work forever if it wont pick all the cards...
    you see I used random(52); and then switch case....
    now if random doesn't pick them all the loop continues...

    Well I know it sounds stupid.. but thats me, and I'm looking for a code that picks cards better then random...

    Thanks in advance...
    THE GIRAFFE MAN
    "our heads in the skies, but our feet on the ground"....

    http://dagan.150m.com

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    i know this would be maybe too much work, but here's an idea:
    make a list of all numbers, 1-52 in order
    randomly pick a number(really random)
    cut the number from the list, choose again from the remaining 51 numbers in the list

    with an array you could make a function which would cut a number and move every number after that down to fill the gap. or, with a linked list, you could just attach the previous pNext pointer to one further down the list. but a linked list needs structs, and that seems like too much for a simple card choosing program.

  3. #3
    Registered User GiraffeMan's Avatar
    Join Date
    Feb 2002
    Posts
    20

    Question try this...

    Thanks BUT.. I've done a list and just scrambeld it a little using random... can you tell me if that's ok???

    r=random(20);
    for(i=0;i<52;i++)
    {
    while((i+r)>52) r=random(20); // makes sure the random
    temp=cards[i]; // has limits!!!
    cards[i]=cards[i+r];
    cards[i+r]=temp; // Shuffels the cards...
    }
    THE GIRAFFE MAN
    "our heads in the skies, but our feet on the ground"....

    http://dagan.150m.com

  4. #4
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    from what i see it looks ok. here's what i think's happening(tell me if i'm wrong):
    Code:
    r=random(20); 
    for(i=0;i<52;i++) 
    { 
    while((i+r)>52) r=random(20); // makes sure the random 
    temp=cards[i]; // has limits!!! 
    cards[i]=cards[i+r]; 
    cards[i+r]=temp; // Shuffels the cards... 
    }
    each card (i) is swapped with (i+r), which is basically another card sometime after (i). it's not really random that way, but i'm assuming you're looking for an imperfect way to do this. i'll compile this (with declarations, etc...) and try it out.

  5. #5
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    i've compiled this program(basically your code sample with extras to make it compile):
    Code:
    #include <stdlib.h>
    #include <time.h>
    #define random(x) (rand()%x)
    int main()
    {
    
    int i=0;
    int r=0;
    int temp=0;
    int cards[52];
    srand(time(NULL));
    for (i=0;i<53;i++) { cards[i]=i; }
    
    
    
    
    r=random(20); 
    for(i=0;i<52;i++) 
    { 
    while((i+r)>52) r=random(20); // makes sure the random 
    temp=cards[i]; // has limits!!! 
    cards[i]=cards[i+r]; 
    cards[i+r]=temp; // Shuffels the cards... 
    }
    for (i=0;i<52;i++) {
    printf("%i      %i\n",cards[i],i); }
    
          return 0;
    }
    the results are very sequential and not random. i think you need a better algorithm. and it looks like you may be swapping an extra card(cards 0-52 instead of 0-51). try making the while((i+r)>52 statement into while((i+r)>=52.
    Last edited by ygfperson; 02-04-2002 at 03:08 PM.

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

    shuffle cards

    This sample seems to get the job done... Deitel & Deitel series

    Code:
    for( int i = 0; i < MAX_CARDS; i++)
    {
         randIndex = rand() % MAX_CARDS;
    
        temp                        = cards[ i ];
        cards[ i ]                  = cards[ randIndex ];
        cards[ randIndex ]   = temp; 
    }
    zMan

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    223
    correction....




    for( i = 0; i < MAX_CARDS; i++)
    {
    randIndex = rand() % MAX_CARDS;

    temp = cards[ i ];
    cards[ i ] = cards[ randIndex ];
    cards[ randIndex ] = temp;
    }
    zMan

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #include <stdio.h>
    #include <time.h>
    
    
    int getCurrent( int c[], int num )
    {
        int x,y,z;
        for( y = -1, x = 0; x < 52; x++ )
        {
            if( c[x] > -1 ) y++;
            if( y == num )
            {
                y = c[x];
                c[x] = -1;
                return y;
            }
        }
        return -1;
    }
    
    int main( void )
    {
        int cards[52], deck[52], count, current;
        srand( time(0) );
    
        for( count = 0; count < 52; count++ )
        {
            cards[count] = count;
        }
    
        for( count = 0, current=52; count++ )
        {
            deck[count] = getCard( card, rand() % current-- );
            if( deck[count] == -1 )
            {
                error...exit or do whatever, shouldn't ever happen
            }
        }
    }
    I'm sure this is your homework assignment, but I'm tired, and don't care. You luck out today. Anyway, this should work. I haven't tried it, and keep in mind I've only had 2 hours sleep in the past day, so use at your own risk.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    Watch the array going OOB as declared as 52 ie 0-51 but array loops, random numbers generated to 52 (should be 51).
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > Watch the array going OOB as declared as 52 ie 0-51 but array
    > loops, random numbers generated to 52 (should be 51).

    Oh really? How do you figure:

    > for( count = 0, current=52; count++ )
    > {
    > deck[count] = getCard( card, rand() % current-- );

    rand % 52 is zero to 51. You are incorrect.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > for( count = 0, current=52; count++ )

    I wonder what this does?

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > for( count = 0, current=52; count++ )

    Ah, well, there you have it. You're an idiot. You obviously cannot tell what a typo is. If so, you're realize that the above line was supposed to be (OBVIOUSLY):

    for( count = 0, current=52; count < 52; count++ )

    Hell, that (the first) won't even compile.

    You obviously missed the part where I said:
    Anyway, this should work. I haven't tried it, and keep in mind I've only had 2 hours sleep in the past day, so use at your own risk.
    It's not my fault you can't read, or use common sense.

    In any case, they're STILL wrong, because they stated that the random number goes to 52. No, it doesn't. It starts at MOD 52 and goes downward. You still aren't going over 52. If the looping is off, the most likely thing to happen is a divide by zero error. In which case, they're still wrong, because the random number does NOT go above 51.

    Quzah.
    Last edited by quzah; 02-05-2002 at 05:34 PM.
    Hope is the first step on the road to disappointment.

  13. #13
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    Sorry.

    How dare I correct your mistake and try to help the person who came here to ask a question.

    Will know better in the future.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > Sorry.
    >
    > How dare I correct your mistake and try to help the person who
    > came here to ask a question.

    The problem isn't that you tried to point out a mistake. The problem is that you acted like you knew what you were talking about, when clearly you didn't.

    You didn't "point out" anything. You didn't quote code. You didn't show a fix. You didn't even have the right "solution". Simply put: You said the problem was with the random number. This is wrong. There was nothing at all wrong with the random number aspect of the program.

    If you're going to try and point out errors, then by all means, POINT THEM OUT. Even better, make sure you're right when you're doing it. If you say something is wrong, point it out (quoting usually is a GoodThing(TM)).

    The fact remains that you were wrong. Nothing was wrong with the "random" part of the program, which is what you said was wrong.

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Originally posted by quzah

    Ah, well, there you have it. You're an idiot. You obviously cannot tell what a typo is. If so, you're realize that the above line was supposed to be (OBVIOUSLY):
    You're scaring me again.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help it won't compile!!!!!
    By esbo in forum C Programming
    Replies: 58
    Last Post: 01-04-2009, 02:22 PM
  2. Help!For poker game simulation
    By tx1988 in forum C++ Programming
    Replies: 24
    Last Post: 05-25-2007, 09:59 PM
  3. dealing cards
    By braddy in forum C Programming
    Replies: 15
    Last Post: 03-29-2006, 02:46 AM
  4. RANT: What the hell is the point of gift cards?
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 42
    Last Post: 12-03-2004, 06:43 AM
  5. Cribbage Game
    By PJYelton in forum Game Programming
    Replies: 14
    Last Post: 04-07-2003, 10:00 AM

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