i need help with this card game!

Discussion in 'C Programming' started by kingyof2thejring, Nov 18, 2004.

  1. Hi there ,
    I am facing problem i need to represent an array of cards, to add Knuth’s
    shuffle routine to it.
    Thanks in advance if someone could help.

    #include <stdio.h>

    int main()
    {
    int cards[52];
    int i;
    int suit;
    int number;


    for (i = 0; i < 52; i++){
    suit=i/13; // prev i had suit=cards/13;
    number=(i%13)+2; // and number=(cards%13)+2;
    printf("Card for %d is ", i); // but that dosent do the trick

    switch (suit)
    {
    case(0): printf("C"); break;
    case(1): printf("D"); break;
    case(2): printf("H"); break;
    case(3): printf("S"); break;

    }

    if (number < 11)
    {
    printf("%d", number);
    }
    switch (number)
    {
    case(11): printf("J"); break;
    case(12): printf("Q"); break;
    case(13): printf("K"); break;
    case(14): printf("A"); break;
    }

    printf("\n");

    }

    }
     
    kingyof2thejring, Nov 18, 2004
    #1
    1. Advertisements


  2. Adding Knuth to this code is like adding Einstein to the World Wrestling
    Federation. MPJ
     
    Merrill & Michele, Nov 19, 2004
    #2
    1. Advertisements

  3. kingyof2thejring

    osmium Guest

    Your array is empty. You want an array of struct. Fix that problem first.

    I don't know what Knuth says but something like this:

    int i;
    for(i=0; i<51; i++)
    /* swap cards with cards[random number in range 0..51] */

    is fine. Pendants like to criticize that. Ignore them.
    <snip>
     
    osmium, Nov 19, 2004
    #3
  4. kingyof2thejring

    Minti Guest



    Well, it's not completly unreasonable to believe that

    rand() % 51 Might not give you the randomness that is generally
    desired. I am not sure about pedants. But pragmatically you need to
    think about these issues.
     
    Minti, Nov 19, 2004
    #4
  5. kingyof2thejring

    dandelion Guest



    Specifically since Knuth states that the MSB should be used lest the LSB may
    suffer from (unwanted) regularities. Knuth TAoCP, vol II.
     
    dandelion, Nov 19, 2004
    #5
  6. Why are structures needed?


    It makes more sense to have the test as i<52, or the loop misses the last
    card. You could argue that it ccan still get swapped by other iterations
    of the loop, but then why not make the test i<50, i<49 and so on? Also

    /* swap cards with cards[random number in range 0..51] */

    doesn't produce an even distribution of orderings whereas

    /* swap cards with cards[random number in range 0..i] */

    does. Why suggest an algorithm when a better one is available that is just
    as simple?

    To produce a value in the range 0..51 you need rand() % 52. However I'm
    not sure that is what osmium is talking about since he never mentioned
    the method of generating random numbers, his comments appear to relate to
    the shuffling algorithm itself.
    That is true for some implementations of rand(), and in particular true
    for the example implementation listed in the standard.

    Lawrence
     
    Lawrence Kirby, Nov 19, 2004
    #6
  7. They aren't, but help make interpretation of a card's suit/number much
    easier to manage (e.g. you can check it directly rather than calculate it.)

    If desired, a struct in a bitfield format can be used to pack the card data
    into one byte (4 bits for the number, 2 for the suit, and 2 padding or
    spare bits).
     
    Raymond Martineau, Nov 19, 2004
    #7
  8. kingyof2thejring

    Malcolm Guest


    You badly need a bit of organisation. It is unlikely that shuffling will be
    performance critical, so why not make the code modular?

    void shuffle(void *list, size_t width, int N)
    {
    int i;
    unsigned char *ptr = list;

    for(i=0;i<N;i++)
    swap(ptr + width * i, ptr + width * (rand() % N), width);
    }

    void swap(void *xptr, void *yptr, size_t len)
    {
    unsigned char *ptr1 = xptr;
    unsigned char *ptr2 = yptr;
    unsigned char temp;
    size_t i;

    for(i=0;i<len;i++)
    {
    temp = ptr1;
    ptr1 = ptr2;
    ptr2 = temp;
    }
    }

    Now this function isn't perfect. For instance use of modulus to calculate a
    random number is non-ideal, and purists will also criticise the way the swap
    target is selected from the whole array. It is probably good enough, but you
    can slot it in and then make it perfect, when you are thinking about
    shuffling and not about cards.
    You also have a handy "swap" function, for free. This could come in useful
    in lots of places.
     
    Malcolm, Nov 19, 2004
    #8
  9. "Malcolm"

    A swap function is of great utility in creating a derangement. I happened
    to be talking to a wonderful lady who was a Harvard-trained statistician,
    and she described to me in great detail how they programmed such
    derangements in machine (not even assembly) code on the vacuum-tube
    machines. And most people think of C as a low-level language! MPJ
     
    Merrill & Michele, Nov 21, 2004
    #9
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.