I tried to print out a hand of 5 random cards in C, but it doesn't work after I filled in the deck of 52 cards. How can you fix the area where I could print out a hand of 5 random cards?
My code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* handy typedefs */
typedef unsigned char card;
typedef unsigned char pairs;
static char *suits[] = {"Hearts","Diamonds","Clubs","Spades"};
static char *values[]= {"Ace","Two","Three","Four","Five","Six",/
"Seven","Eight","Nine","Ten","Jack","Queen","King"};
static char *colour[]= {"Black","Red"};
/* function prototypes */
void filldeck(card deck[52]); /* Populates a deck of cards */
void shuffle(card deck[52]); /* Randomizes the order of cards */
int main()
{
card deck[52],*deckp;
card hands[5][5],handssorted[5][5];
pairs numpairs[5],highest;
int hand,cd,winner,i,irand;
srand(time(NULL)); /* seed the random number generator */
/*populate and shuffle the deck */
filldeck(deck);
shuffle(deck);
for(cd=0;cd<5;cd++)
{
for(hand=0;hand<5;hand++)
{
/* deal the hands here */
}
}
for (hand=0;hand<5;hand++)
{
/* sort the hands here */
printf("Hand %i:\n",hand+1 );
/* print the hands here */
/* Get a random value between/including MIN_RAND_VAL and MAX_RAND_VAL
* A Better mathematical description would be:
* "Generate a uniformly distributed random number on the half-open interval
* [MIN_RAND_VAL, MAX_RAND_VAL).
*/
for ( i = 0; i < 5; i++) {
irand = (rand() % 52);
printf(" %s \n ", deck[irand]);
}
}
return 0;
}
void filldeck(card deck[52])
{
/* populate the deck here */
int i=0, S, V;
for ( S= 0; S <13; S++)
{
for (V =0; V< 4; V++){
memset(deck,0,sizeof(deck));/* Clear the buffer before writing new value*/
strcpy( deck, values[S]);
strcat(deck, " of ");
strcat(deck,suits[V]);
strcat(deck,", is ");
if ( V == 0 || V == 1 )
{
strcat(deck, colour[1]);
}
else
if ( V == 2 || V == 3 )
{
strcat(deck, colour[0]);
}
printf("%s\n", deck);//prints out all the 52 playing cards
i++;
}
}
printf("Number of playing cards: %d\n",i);
return;
}
void shuffle(card deck[52])
{
int i,rnd;
card c;
for(i=0;i<52;i++)
{
/* generate a random number between 0 & 51 */
rnd=rand() * 52.0 / RAND_MAX;
c = deck[i];
deck[i] = deck[rnd];
deck[rnd] = c;
}
}
Aucun commentaire:
Enregistrer un commentaire