3 Replies - 901 Views - Last Post: 25 October 2010 - 01:36 PM Rate Topic: -----

#1 Felipe13576  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-October 10

Showing the location of cards within a deck

Posted 24 October 2010 - 08:29 PM

Hi everyone. For some reason I cannot seem to figure out what it is that I need to do to complete this code. I have to write a program to shuffle a deck of cards and find the location of two of the cards within the deck. I have filled an array with the numbers 1-52, which represent the individual cards and I am attempting to write a function (which is called from main()) that allows the cards to be shuffled and then printed. Once this is done, I have to write two separate functions that are once again called in main() that show the location of two given cards. I understand what the code needs to do, but I can't seem to write it. I was wondering if someone would be willing to point me in the right direction so that I could go from there. My first problem is writing a function that shuffles the deck. I have messed around with it a little, and here is what I have come up with:

 
/* Program that shuffles a deck of cards and determines
* the location of the Ace of Hearts and the Queen of Clubs
*/

#include<stdio.h>       /* printf, scanf definitions           */
#include<time.h>        /* allows for random number generation */
#include<stdlib.h>

#define MAX 52 			/* The maximum integer value allowed in the arrays */

int
main()
{
	char diamonds[] = "diamonds", hearts[] = "hearts", clubs[] = "clubs",
	     spades[] = "spades";
	int decki[52],      /* Deck of 52 cards                    */
        i,				/* Variable used in for loop		   */
        suit,
        card;
 
 	/* Explains the program to the user */
 	puts("This program finds the location of the Ace of Hearts");
 	puts("and the Queen of Clubs.\n\n");

	/* Inputs the numbers 1 - 52 into their corresponding elements in deck[i] */
	for( i = 1 ; i <= MAX ; ++i )
		{
		decki[i] = i;
		printf("decki[%d] = %d\n", i, decki[i]);
		}
	
	
	printf("\n\n\n");
	return(0);
}

int shuffle (int x)

{
	int deckx[52],
		j,
		*a,
		*b,
		temp;

    /* Initializes the random number generator */
    srand(time(NULL));
	    
    for( j = 1 ; j <= MAX ; ++j)
    	{
    	 deckx[j] = j;
	    }
    
    for( j = 1 ; j <= MAX ; ++j)
    	{
    	*a = &deckx[j];
    	*b = &deckx[ ( rand() % 52 ) + 1 ];
    	
    	temp = *a;
    	*a = *b;
    	*b = temp;
	    }
    
    for( j = 1 ; j <= MAX ; ++j)
    	{
    	printf("deckx[%d] = %d\n\n\n", j, deckx[j]);
	    }
    

	printf("\n\n");
    return(0);



Any advice would be greatly appreciated.

This post has been edited by Felipe13576: 24 October 2010 - 08:30 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Showing the location of cards within a deck

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Showing the location of cards within a deck

Posted 24 October 2010 - 11:55 PM

First thing we need to look at is what the compiler says about your code.
dic.c: In function ‘main’:
dic.c:20: warning: unused variable ‘card’
dic.c:19: warning: unused variable ‘suit’
dic.c:16: warning: unused variable ‘spades’
dic.c:15: warning: unused variable ‘clubs’
dic.c:15: warning: unused variable ‘hearts’
dic.c:15: warning: unused variable ‘diamonds’
dic.c: In function ‘shuffle’:
dic.c:57: warning: assignment makes integer from pointer without a cast
dic.c:58: warning: assignment makes integer from pointer without a cast



The warnings about lines 57 and 58 are the most concerning.
In this section of code
    	*a = &deckx[j];
    	*b = &deckx[ ( rand() % 52 ) + 1 ];


what were you trying to do?
Explain your intent there so we can help you better.
Was This Post Helpful? 0
  • +
  • -

#3 oscode  Icon User is offline

  • D.I.C Regular

Reputation: 109
  • View blog
  • Posts: 257
  • Joined: 24-October 10

Re: Showing the location of cards within a deck

Posted 25 October 2010 - 05:41 AM

Here is a clean-up of part of your shuffle code. Notice it uses your MAX constant; < rather than <=; and rand() without + 1 due to 0 based indexing. Let us know how you get on from here.

for(int i = 0; i < MAX; i++)
	deckx[i] = i;

for(int i = 0; i < MAX; i++)
{
	int j = rand() % MAX;
	std::swap(deckx[i], deckx[j]);
	std::cout << "deckx[" << i << "] = " << j << "\n";
}



I haven't tested this code, but hopefully you understand it enough to use it.

This post has been edited by oscode: 25 October 2010 - 05:48 AM

Was This Post Helpful? 0
  • +
  • -

#4 Felipe13576  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-October 10

Re: Showing the location of cards within a deck

Posted 25 October 2010 - 01:36 PM

ok, I have finished with the first part of the code and now I need to update the program to that the user can enter a card (face value and suit) and it outputs the cards position within the shuffled deck.

Here is what I have so far:

/* Program that shuffles a deck of cards and determines
* the location of the Ace of Hearts and the Queen of Clubs
*/

#include<stdio.h>       /* printf, scanf definitions           */
#include<time.h>        /* allows for random number generation */
#include<stdlib.h>

#define MAX 52 			/* the maximum integer value allowed in the arrays */
#define NOT_FOUND -1    /* value returned by search function if target n_f */

/* Function prototypes */
void shuffle(int deck[]);
int search(const int deck[], int target);
void find_card (int c);

int
main()
{
 	char face,			/* Face value of the card		       */
 		 suit;			/* Suit of the card					   */
	int deck[52],       /* Deck of 52 cards                    */
        i,				/* Variable used in for loop		   */
        j,
  		index,			/* Location of required cards		   */
  		c;				/* Card choice						   */	
  		
	srand(time(NULL));
	
 	/* Explains the program to the user */
 	puts("This program shuffles a deck of cards and finds the location");
 	puts("of inputed cards.\n\n");
 	
 	/* Shows the values of each of the cards */
 	puts("        Card suits and values:");
 	
 	puts("     |   D   |   H   |   C   |   S   |");
 	puts("---------------------------------------");
 	puts("  2  |   1   |   14  |   27  |   40  |");
 	puts("  3  |   2   |   15  |   28  |   41  |");
 	puts("  4  |   3   |   16  |   29  |   42  |");
 	puts("  5  |   4   |   17  |   30  |   43  |");
 	puts("  6  |   5   |   18  |   31  |   44  |");
 	puts("  7  |   6   |   19  |   32  |   45  |");
 	puts("  8  |   7   |   20  |   33  |   46  |");
	puts("  9  |   8   |   21  |   34  |   47  |");
 	puts("  10 |   9   |   22  |   35  |   48  |");
 	puts("  J  |   10  |   23  |   36  |   49  |");
 	puts("  Q  |   11  |   24  |   37  |   50  |");
 	puts("  K  |   12  |   25  |   38  |   51  |");
 	puts("  A  |   13  |   26  |   39  |   52  |\n\n\n");

	for( j = 0 ; j < 3 ; ++j)
	{
	/* Inputs the numbers 1 - 52 into their corresponding elements in deck[i] */
	for( i = 1 ; i <= MAX ; ++i )
		{
		deck[i] = i;
		}
		
	printf("\n\n");
	
	/* Calls deck[] from shuffle function */
	shuffle(deck);
	
	/* Prompts the user for a card position */
	printf("Enter a position in the deck: ");
	scanf("%d", &c);
	
	printf("\ndeck[%i]: %i  find_card[%i]: ", c, deck[c], deck[c]);
	find_card(deck[c]);	
	
	/* Prompts the user for a card (suit and face value) */
	printf("Enter a suit and face value (i.e. 2D - two of diamonds): ");
	scanf("%c%c", &face, &suit);
	
	printf("\%c %c is card # %d", face, suit, i);
	find_card(deck[c]); 

	printf("\n\n\n");
	}
	return(0);
}

/* Function to shuffle the deck of cards */
int get_min_range (int deck[], int min, int max);

void shuffle (int deck[])	/* array being sorted			     */

{
	int i,				/* first element in unsorted array   */
		temp,			/* temporary storage			     */
		x;				/* subscript of next smallest element */
 
	for ( i = 1 ; i < MAX + 1 ; ++i)
		{
		/* random number generator */
		x = rand() % MAX ;
		
		
		/* Exchange elements */
		if (i != x)
			{
			temp = deck[x];
			deck[i] = deck[x];
			deck[i] = temp;
			}
		printf("temp[%d] = %d\n", i, temp);
		}
	printf("\n\n");
		
}

/* Function to find the required cards */
int search (const int deck[],			/* input - array to search    */
			int target)					/* input - value searched for */

{
	int i,
		found = 0,		/* whether or not target has been found  */
		where;			/* index where target found or NOT_FOUND */
	
	/* Compares each element to target */
	i = 0;
	while( !found  &&  i < MAX)
		{
		if(deck[i] == target)
			found = 1;
		else
			++i;
		}
	 /* Returns index of element matching target or NOT_FOUND */
	 if(found)
	 	where = i;
 	else
 		where = NOT_FOUND;
	

	return(where);
}

void find_card (char face, char suit)
{
	
	/* Finds the card values */
	if( c % 13 == 10)
		printf("J");
	else if( c % 13 == 11)
		printf("Q");
	else if( c % 13 == 12)
		printf("K");
	else if( c % 13 == 0)
		printf("A");
	else
		printf("%i", c % 13 + 1);
	
	/* Finds the suit */
	if( c >= 1  &&  c <= 13 )
		printf("D");
	if( c >= 14  &&  c <= 26 )
		printf("H");
	if( c >= 27  &&  c <= 39 )
		printf("C");
	if( c >= 40  &&  c <= 52 )
		printf("S");
	putchar('\n');			
}


The find_card function was used to output the face value and suit when the user enters a card position within the deck. I now need to change that function so that it is essentially backwards. The user enters a card (face value and suit) and the program outputs the cards position within the deck, which is shuffled using the shuffle() function. I know that the program is right so far, but now I need to "enhance" it a little and change the user input and program output. If anyone would be willing to give me some advice on how to do this, that would be great.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1