Search code examples
cvisual-studioplaying-cards

How to have have function give out seperate card hands to seperate people


For a homework assignment i need to create a program that lets you play the Irish card game 25. I can kind of have my code give out a hand to one person but if i try to have multiple people, the code gives out an identical hand to the other people. How do i give different, unique hands to other people?

I've tried using a loop, thinking that the function would simply reset the array but it hasn't

/* Deals a random hand of cards */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define TRUE 1
#define FALSE 0
#define BOOL int
#define NUM_SUITS 4
#define NUM_RANKS 13

int DealCards(int i);

int main()
{
    int i;
    int NumOfPlayers;

    printf("Please Enter the Number Of Players: ");
    scanf("%d", &NumOfPlayers);

    for (i = 1; i <= NumOfPlayers; i++)
    {
        DealCards(i);
    }
}

int DealCards(int i)
{
    BOOL in_hand[NUM_SUITS][NUM_RANKS] = { FALSE };
    int        num_cards   = 5, rank, suit;
    const char rank_code[] = { '2', '3',  '4',  '5',  '6',  '7', '8',
                               '9', '10', '11', '12', '13', 'A' };
    const char suit_code[] = { 'C', 'D', 'H', 'S' };
    srand(time(NULL));
    printf("\n\nPlayer %d's hand :\n", i);
    while (num_cards > 0)
    {
        suit = rand() % NUM_SUITS;
        rank = rand() % NUM_RANKS;
        if (!in_hand[suit][rank])
        {
            in_hand[suit][rank] = TRUE;
            num_cards--;
            printf(" %cof%c ", rank_code[rank], suit_code[suit]);
        }
        printf("\n");
    }
    return 0;
}

Solution

  • The problem is that you call srand() function before giving card to every player. You use time(NULL) as an argument, therefore seed changes only every second, and players get cards with the same seed. You need to set seed only once for each program launch.