i need help with this card game!

K

kingyof2thejring

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");

}

}
 
M

Merrill & Michele

kingyof2thejring said:
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");

}

}

Adding Knuth to this code is like adding Einstein to the World Wrestling
Federation. MPJ
 
O

osmium

kingyof2thejring said:
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;

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>
 
M

Minti

osmium said:
kingyof2thejring said:
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;

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.


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.
 
D

dandelion

Minti said:
kingyof2thejring said:
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;

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.


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.


Specifically since Knuth states that the MSB should be used lest the LSB may
suffer from (unwanted) regularities. Knuth TAoCP, vol II.
 
L

Lawrence Kirby

Minti said:
:

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;

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

Why are structures needed?
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.


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.
Specifically since Knuth states that the MSB should be used lest the LSB
may suffer from (unwanted) regularities. Knuth TAoCP, vol II.

That is true for some implementations of rand(), and in particular true
for the example implementation listed in the standard.

Lawrence
 
R

Raymond Martineau

Minti said:
osmium wrote:
:

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;

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

Why are structures needed?

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).
 
M

Malcolm

kingyof2thejring said:
#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");

}

}

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.
 
M

Merrill & Michele

"Malcolm"
"kingyof2thejring"
#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");

}

}

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.

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top