Member Avatar for karoma

Hi guys,

I'm trying to develop a card program, which seems to be working fine, with the exception of the dealing part.
The deck is initialised, shuffled, then is supposed to deal out 5 cards without duplicates. However I keep seeing duplicates when I deal. I've ran though it countless times but I can't see how the duplicates are getting through. Can anyone shed any light?

void deal()
{

char suits[4] = {0,1,2,3};
char faces[13] = {'A','2','3','4','5','6','7','8','9','X','J','Q','K'};
int deck[52];

int i, j, k, card, suit, face;
int hand[5];

	srand(time(0));

	for (i=0;i<5;i++)
	{
		j = rand()%52;
		card = deck[j];
		for (k=0;k<5;k++)
		{
			if (card == hand[k])
			{
				i--;
				break;
			}
			else
			{
				hand[i] = card;
				suit = card/13;
				face = card%13;
				printf ("%c of %c \n", faces[face], suits[suit]+3);

				break;
			}
		}
	}
}

Hmmm... isn't it that the elements of deck have no value?

so whats the use of line 16?

Member Avatar for karoma

Oops, sorry. The deck is initialised in another function. Lines 4-6 above are global variables; I just copied them into the function above so you could see them.

void setupDeck()
{
	int i;
	for(i=0;i<52;i++)
	{
		deck[i] = i;
	}
}

Shouldn't the variables be initialized in the main function then passed on to to other functions or use global variables to change the value?

like in your setupDeck() function deck is not initialized

Member Avatar for karoma

Most of the variables are declared before main, as global variables. setupDeck() is working fine. The program goes on to compare hands and other things, which all work fine.
The problem with the duplicates is somewhere within deal() I think.

I see... could you post the whole code so I can see everything instead of asking questions

debugging is hard when you can't see the whole program especially when helping someone across the web

Member Avatar for karoma

Here you go. Sorry, I should have done this in the OP. I was trying to keep it simple. I haven't included all the functions after printDeck() since they don't really matter.

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

void setupDeck();
void shuffle();
void printDeck();

char suits[4] = {0,1,2,3};
char faces[13] = {'A','2','3','4','5','6','7','8','9','X','J','Q','K'};
int deck[52];

void main()
{
setupDeck();
printDeck(); 
}

void setupDeck()
{
	int i;
	for(i=0;i<52;i++)
	{
		deck[i] = i;
	}
}
void printDeck()
{
	int i, j, k, card, suit, face;
	int hand[5];

	srand(time(0));

	for (i=0;i<5;i++)
	{
		j = rand()%52;
		card = deck[j];
		for (k=0;k<5;k++)
		{
			if (card == hand[k])
			{
				i--;
				break;
			}
			else
			{
				hand[i] = card;
				suit = card/13;
				face = card%13;
				printf ("%c of %c \n", faces[face], suits[suit]+3);

				break;
			}
		}
	}


}

Is it something like the loop doesn't pass the if (card == hand[k]) statement?

I compiled the code a lot of times and though rarely I get results where this event occurs

well I'm not exactly sure how to solve this but just a suggestion maybe you shouldn't use an else statement for assigning the variable to the array cause when the first element is not equals to it the else statement occurs which does this

maybe you could try using a boolean like statement like this

int found = 0;
	for (i=0;i<5;i++)
	{
		j = rand()%52;
		card = deck[j];
		for (int k=0;k<5;k++)
		{
			if (card == hand[k])
			{
				i--;
				found=1;
                                break;
			}

		}
			if (found == 0)
			{
				hand[i] = card;
				suit = card/13;
				face = card%13;
				printf ("%c of %c \n", faces[face], suits[suit]+3);
			}
		found = 0;
	}

after compiling this a lot of times I haven't received a duplicate output yet

Member Avatar for karoma

Thanks very much, that works perfectly. I'm still a bit confused over what was wrong with that else statement, but a solution is a solution so I'll move on.
Thanks again.

Member Avatar for karoma

It turns out I'm still getting duplicates, even with the above corrections to my code.
Can anyone see where it might be going wrong?

Is it something like the loop doesn't pass the if (card == hand[k]) statement?

How absent minded of me... I didn't notice that the problem in the first code is not in the if statement but how the for loop works

for (k=0;k<5;k++)
		{
			if (card == hand[k])
			{
				...
				break;
			}
			else
			{
                                ...
				break;
			}
		}

only run once when it passes either the if or else conditionals (meaning it only checks hand[0]), it exits the loops because of the break

It turns out I'm still getting duplicates

what exactly is the output that has the duplicates?

The main problem is you aren't 'dealing' the card. It's still in the deck.

Your first card is j = rand()%52; Then remove that card from the deck (shift all values from j thru 51 down 1 entry.
Now you have 51 cards left, just like a real deal.
Now you deal the next card with j = rand()%51; Or, if you have the deck shuffled, just deal from the top - 0-9 for 2 hands of 5 cards.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.