0

I have allready shuffled my deck and dealt it out and that works. However, when im about to make the user enter his/her input in my program and get two cards dealt out, i get the same cards, this is not what I want, i want different cards for both players and i dont want them duplicated. I can not see whats wrong with my code, spend some hours looking through it, but in my eyes i feel it should be fine.

Its when i call the function: player1();and from there i call printcards(deck) and here's where i have my problem. Can any of you guys help me with a solution here so when i call my function printcards(deck)i get different kinds of cards without them duplicated.

Heres how my code looks completely: (Its the last function where my problem is).

#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 COLOR_SIZE 13
#define NR_OF_SUITS 4
#define DECK_SIZE 52
#define JACK 11
#define QUEEN 12
#define KING 13
#define ACE 1

struct Card
{
    int suit;
    int value;
    int *sum;
};
int test(struct Card *cardDeck);
int player2(struct Card* deck);
int player1(struct Card* deck);
int printCards(struct Card *cardDeck);
void swapCards(struct Card *cardA, struct Card *cardB);
void shuffleCards(struct Card *cardDeck);

int main()
{
    srand((unsigned)time(NULL));
    //struct Card deck[DECK_SIZE];      //Statiskt allokerad array
    struct Card * deck; //Dynamiskt allokerad array
    int index;
    int suit_index;
    int startAt = 0;

    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;
        }

    shuffleCards(deck);
    printf("\n\n");
    startAt = printCards(deck, startAt); //player 1
    startAt = printCards(deck, startAt); //player 2
    system("pause");
    return 0;
}

int printCards(struct Card *cardDeck, int start_index)
{
    int sum = 0;
    int i = 0;
    for (int i = start_index; i < start_index + 2; i++)
    {
        switch (cardDeck[i].value + 1)
        {
        case ACE: printf("Ace ");
            cardDeck[i].value = 11;
            break;
        case JACK: printf("Jack ");
            cardDeck[i].value = 10;
            break;
        case QUEEN: printf("Queen");
            cardDeck[i].value = 10;
            break;
        case KING: printf("King ");
            cardDeck[i].value = 10;
            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");
    }
    return i;
}


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

void shuffleCards(struct Card *cardDeck)
{

    for (int i = 0; i < DECK_SIZE; i++)
        swapCards(&cardDeck[i], &cardDeck[rand() % 52]);
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
William hart
  • 123
  • 5
  • Since I was dealt a "1 of Hearts" I suggest you have an array of the rank names, such as `char *rank[]= {"Ace", "Two", "Three", ..., "King"};` and have ranks in the range `0..12`, rather than adjusting the ranks' value. If you need to refer to a specific rank, you can use `enum { ACE, TWO, THREE, ..., KING};` instead of defining each separately. Then you'll avoid any confusion between numeric value and rank. – Weather Vane Dec 29 '15 at 13:03
  • Why all the pointers? – Lightness Races in Orbit Dec 31 '15 at 16:29
  • William, don't alter the question to include code provided by the answers. That was really confusing until I found out what you'd done. – Lightness Races in Orbit Dec 31 '15 at 16:30

2 Answers2

3

It seems you need a new structure

struct CardDeck  
{
   struct Card cards[52];
   int last_Index;
};

This can hold where in the deck you are, and will not use a global variable.

so :-

Updated

Re-wrote the questions code so it compiles.

#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 COLOR_SIZE 13
#define NR_OF_SUITS 4
#define DECK_SIZE 52
#define JACK 11
#define QUEEN 12
#define KING 13
#define ACE 1
struct Card    /* <<<< Card needs to be before CardDeck */
{
    int suit;
    int value;
    int *sum;
};
struct CardDeck
{
    struct Card cards[52];
    int last_Index;
};




int test(struct Card *cardDeck);
int player2(struct Card* deck);
int player1(struct Card* deck);
int printCards(struct CardDeck *cardDeck);
void swapCards(struct Card *cardA, struct Card *cardB);
void shuffleCards(struct Card *cardDeck);

int main()
{
    srand((unsigned)time(NULL));
    //struct Card deck[DECK_SIZE];      //Statiskt allokerad array
    struct CardDeck * deck; //Dynamiskt allokerad array
    int index;
    int suit_index;
    int startAt = 0;
      /* Changed deck to a CardDeck */
    deck = (struct CardDeck *)malloc(sizeof(struct CardDeck) );
    deck->last_Index = 0; /* << ensure the deck is full of cards */
    for (suit_index = 0; suit_index < NR_OF_SUITS; suit_index++)    /* Initiera kortleken */
        for (index = 0; index < COLOR_SIZE; index++)
        {
            deck->cards[suit_index*COLOR_SIZE + index].suit = suit_index;
            deck->cards[suit_index*COLOR_SIZE + index].value = index;
        }

    shuffleCards(deck->cards); /* shuffleCards can still shuffle the cards array */
    printf("\n\n");
    system("pause");
    return 0;
}

int printCards(struct CardDeck *cardDeck) /* changed to CardDeck */
{
    int sum = 0;
    int i = 0;
    for (int i = cardDeck->last_Index; i < cardDeck->last_Index + 2; i++)
    {
        switch (cardDeck->cards[i].value + 1)
        {
        case ACE: printf("Ace ");
            cardDeck->cards[i].value = 11;
            break;
        case JACK: printf("Jack ");
            cardDeck->cards[i].value = 10;
            break;
        case QUEEN: printf("Queen");
            cardDeck->cards[i].value = 10;
            break;
        case KING: printf("King ");
            cardDeck->cards[i].value = 10;
            break;
        default: printf("%d ", cardDeck->cards[i].value + 1);
            break;
        }

        printf("of ");
        switch (cardDeck->cards[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");
    }
    cardDeck->last_Index += 2; /* original post had this increment - ensures each time this is called, 2 cards get used */
    return i;
}


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

void shuffleCards(struct Card *cardDeck)
{

    for (int i = 0; i < DECK_SIZE; i++)
        swapCards(&cardDeck[i], &cardDeck[rand() % 52]);
}
mksteve
  • 12,614
  • 3
  • 28
  • 50
  • I've tried it your way, but i dont get it. When i Use your code and use my switch statement it doesnt really work. Can you edit your code and try to use mine to sum it up in a solution because im confused when im entering my own code in your ".....". Thank you! – William hart Dec 30 '15 at 22:21
  • Yeah, look at my code above, i've tried to edit it like you did, but still. It wont work at all. Could you please look it over again? – William hart Dec 30 '15 at 23:12
  • Great! So before i accept your answer, do you just mind explaining the line of code: deck->last_Index = 0; And also shuffleCards(deck->cards);, why dont you just need to use shuffleCards(deck)? I so appreciate your help! – William hart Dec 31 '15 at 12:37
  • One more question, the function printcards() Why do you use cardDeck->last_Index += 2? I mean, why the += 2? – William hart Dec 31 '15 at 13:03
0

First of all don't cast the result of malloc

The reason why your code returns always the same cards is cause in both cases (Player1 and Player2) you're accesing the same indices in the deck:

for (int i = 0; i < 2; i++){
    switch(cardDeck[i].value){
    ...

You should store the last index you took from the deck:

int last_index = 0; //Global variable    


void printCards(struct Card *cardDeck)
{
    int sum = 0;
    for (int i = last_index; i < last_index+2; i++)
    {
        switch (cardDeck[i].value + 1)
        ...
    }
    last_index += 2;
}

Without global variable:

void printCards(struct Card *cardDeck, int start_index)
{
    int sum = 0;
    for (int i = start_index; i < last_index+2; i++)
    {
        switch (cardDeck[i].value + 1)
        ....
    }
    return i;
}

And call it:

int startAt = 0;
startAt = printCards(deck, startAt); //player 1
startAt = printCards(deck, startAt); //player 2
Community
  • 1
  • 1
Mr. E
  • 2,070
  • 11
  • 23
  • Thanks for your answer. Well i dont want to use any global variables, so how should i do that then? – William hart Dec 29 '15 at 12:39
  • I really think the best is to use the global... But if you don't want, other options could be to supply `printCards` with the starting index (It could be stored in a player struct). Anyway, I think you're trying to do a blackjack game right? If that's so the global is the only solution (Think the case when player 1 takes 2 extra cards and the player 2 only one) – Mr. E Dec 29 '15 at 12:45
  • @Williamhart Edited my answer without the global, but just saw mksteve answers and I think it's more consistent than mine in case you don't want globals – Mr. E Dec 29 '15 at 12:49
  • I have edited my post like you told me to, but the code is still not working properly? Can you look it through again and tell me whats the matter? I appreciate it! – William hart Dec 30 '15 at 23:04