dimanche 27 décembre 2015

Generate a random number doesnt work when calling function twice

Im trying to get 2 different types of numbers/colors when im calling my function twice, but it doesnt work. I've seeded the numbers/function but its still not working.

Here's how my code looks:

    #define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define DIAMONDS 0
#define CLUBS 1
#define HEARTS 2
#define SPADES 3
#define JACK 11
#define QUEEN 12
#define KING 13
#define ACE 1
#define COLOR_SIZE 13
#define NR_OF_SUITS 4
#define DECK_SIZE 52


struct Card
{
    int suit;
    int value;
};

void printCards(struct Card *cardDeck);
void swapCards(struct Card *cardA, struct Card *cardB);
void shuffleCards(struct Card *cardDeck);
void printOneCards(struct Card *cardDeck);

int main()
{
    //struct Card deck[DECK_SIZE];      //Statiskt allokerad array
    struct Card * deck; //Dynamiskt allokerad array
    int index;
    int suit_index;

    deck = (struct Card *)malloc(sizeof(struct Card) * DECK_SIZE);
    for (suit_index = 0; suit_index < NR_OF_SUITS; suit_index++)    /* Initiera kortleken */
        for (index = 0; index < COLOR_SIZE; index++)
        {
            deck[suit_index*COLOR_SIZE + index].suit = suit_index;
            deck[suit_index*COLOR_SIZE + index].value = index;
        }
    printCards(deck);
    shuffleCards(deck);
    printCards(deck);
    printf("\n");
    printOneCards(deck);
    printf("Dealers cards\n");
    srand(time(NULL));
    printOneCards(deck);

    system("pause");
    return 0;
}

void printCards(struct Card *cardDeck)
{
    for (int i = 0; i < DECK_SIZE; i++)
    {
        switch (cardDeck[i].value + 1)
        {
        case ACE: printf("Ace ");
            break;
        case JACK: printf("Jack ");
            break;
        case QUEEN: printf("Queen");
            break;
        case KING: printf("King ");
            break;
        default: printf("%d ", cardDeck[i].value + 1);
            break;
        }
        printf("of ");
        switch (cardDeck[i].suit)
        {
        case DIAMONDS: printf("Diamonds ");
            break;
        case HEARTS: printf("Hearts ");
            break;
        case CLUBS: printf("Clubs ");
            break;
        case SPADES: printf("Spades ");
            break;
        default: printf("Something went wrong!! ");
            break;
        }
        printf("\n");
    }
}

void swapCards(struct Card * cardA, struct Card *cardB)
{
    struct Card temp;
    temp = *cardA;
    *cardA = *cardB;
    *cardB = temp;
}

void shuffleCards(struct Card *cardDeck)
{
    srand(time(NULL));

    for (int i = 0; i < DECK_SIZE; i++)
        swapCards(&cardDeck[i], &cardDeck[rand() % 52]);
}
void printOneCards(struct Card *cardDeck)
{
    srand(time(NULL));
    for (int i = 0; i < 2; i++)
    {
        switch (cardDeck[i].value + 1)
        {
        case ACE: printf("Ace ");
            break;
        case JACK: printf("Jack ");
            break;
        case QUEEN: printf("Queen");
            break;
        case KING: printf("King ");
            break;
        default: printf("%d ", cardDeck[i].value + 1);
            break;
        }
        printf("of ");
        switch (cardDeck[i].suit)
        {
        case DIAMONDS: printf("Diamonds ");
            break;
        case HEARTS: printf("Hearts ");
            break;
        case CLUBS: printf("Clubs ");
            break;
        case SPADES: printf("Spades ");
            break;
        default: printf("Something went wrong!! ");
            break;
        }
        printf("\n");
    }
}

So when im calling the function printonecards(), it prints out 2 randomly cards and colors the first time, but the second time i call it, it doesnt. How do i fix this problem and how do i make it properly work?




Aucun commentaire:

Enregistrer un commentaire