473,379 Members | 1,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

i need help with this card game!

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[i]/13;
number=(i%13)+2; // and number=(cards[i]%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");

}

}


Nov 14 '05 #1
8 1557

"kingyof2thejring" <y2*@kingofthering.wanadoo.co.uk> wrote in message
news:73******************************@localhost.ta lkaboutprogramming.com...
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[i]/13;
number=(i%13)+2; // and number=(cards[i]%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
Nov 14 '05 #2
"kingyof2thejring" writes:
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[i] with cards[random number in range 0..51] */

is fine. Pendants like to criticize that. Ignore them.
<snip>
Nov 14 '05 #3

osmium wrote:
"kingyof2thejring" writes:
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[i] 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.
--

Imanpreet Singh Arora
Mailto: imanpreet""!""gmail""!""com

Nov 14 '05 #4

"Minti" <mi************@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...

osmium wrote:
"kingyof2thejring" writes:
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[i] 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.
Nov 14 '05 #5
On Fri, 19 Nov 2004 10:08:28 +0100, dandelion wrote:
"Minti" <mi************@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...

osmium wrote:
> "kingyof2thejring" writes:
>
> > 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[i] 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[i] with cards[random number in range 0..51] */

doesn't produce an even distribution of orderings whereas

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

does. Why suggest an algorithm when a better one is available that is just
as simple?
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.


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
Nov 14 '05 #6
On Fri, 19 Nov 2004 10:23:55 +0000, Lawrence Kirby <lk****@netactive.co.uk>
wrote:
On Fri, 19 Nov 2004 10:08:28 +0100, dandelion wrote:
"Minti" <mi************@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...

osmium wrote:
> "kingyof2thejring" writes:
>
> > 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).
Nov 14 '05 #7

"kingyof2thejring" <y2*@kingofthering.wanadoo.co.uk> wrote
#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[i]/13;
number=(i%13)+2; // and number=(cards[i]%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[i];
ptr1[i] = ptr2[i];
ptr2[i] = 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.
Nov 14 '05 #8

"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[i]/13;
number=(i%13)+2; // and number=(cards[i]%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[i];
ptr1[i] = ptr2[i];
ptr2[i] = 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
Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Gary Camblin | last post by:
Has anyone got a script to run a higher or lower card game on a web page. Like the game show play you cards right.
7
by: Ken Smith | last post by:
I have a little video poker game I created in Javascript. It uses Tables and inner html stuff - for example: (psudo-code) imagePicked=random card image (2h.gif);...
0
by: Limpor | last post by:
Hello, I am new to learning java, and i am trying to build the class for a calculation card game, unfortunately i can't get the public Card top() and Card takeTop() method in the Stock class. Can...
0
by: Limpor | last post by:
Hi, I’m working on a solitaire game as my course assignment, and I am having trouble to dealing with Stock class, which consists of an upturned top card plus deck. The code for Stock class: import...
27
true911m
by: true911m | last post by:
I would like to solicit your thoughts and ideas (code can come later) about how you would approach a project idea I just tossed together. I chose this as a testbed for me to play with objects,...
13
by: lane straatman | last post by:
I'm trying to figure out what data type is appropriate to represent a card in a game. The idea that I thought was going to work was a struct, foo, with two integer fields and two fields of char...
7
by: Gladen Blackshield | last post by:
Hello All! Still very new to PHP and I was wondering about the easiest and simplest way to go about doing something for a project I am working on. I would simply like advice on what I'm asking...
1
by: Olmar | last post by:
Hi, I am trying to write a version of the Hears card game. I can't figue out what is the best way to detect clicks on the cards. Using the cards.dll interface as described in tutorials like...
3
by: noob2008 | last post by:
this code works but theres a problem wif it. wen it generates for 4 players, it has repeated values i.e 2 of diamond appear twice. how do i avoid this? and i hav no idea how to program the...
32
by: falconsx23 | last post by:
I am making a game called Set, it is a card game: here is a brief description of the rules: The game of Set is played with 81 cards, each of which has 4 properties (number, pattern, color and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.