4 Replies - 918 Views - Last Post: 13 April 2009 - 05:47 AM Rate Topic: -----

#1 deborah_morales1990  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 08-April 09

BlackJack - having to deal cards with more than one deck

Post icon  Posted 08 April 2009 - 08:43 PM

HEllo! I am writing a program that plays blackjack! But we give the user a choice of how many decks are in a shoe. I got it to shuffle and display one set. but now my next step is to figure out how to make it if the user entered a deck larger than 1 but less then 6.
So i made an array of [312] because that is the max of cards a player can have (52 cards * 6 decks)
but when i run it it, it says it succeeds but then it corrupts !... what am i doing wrong? :blink:

Thanks!


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//struct datatype
struct card{
	char *face;
	char *suit;
};
typedef struct card Card; //typedef keyword allows us to create a synonym for a previously defined data type

struct shoe{
	struct card Cards[312];
	int totalCards;
};

typedef struct{
	Card cards[312];
	int is_shuffled;
}Deck;

void shuffle(Card d[312], int numdecks){
	int totalCards = numdecks * 52;
	
	int i, num;
	struct card temp;
	srand(time(NULL));
	for(i = 0; i<totalCards; i++){
		num = rand() % totalCards;
		temp = d[i];
		d[i] = d[num];
		d[num] = temp;
	}
}
void print_deck(Card d[312], int numdecks){	
	//printf("%d\n", numdecks);
int i, j;
int totalCards = numdecks * 52;
	
	for(j = 0; j < numdecks; j++){
		for(i = 0; i < 52; i++){
			printf("%s of %s\n", d[i].face, d[i].suit);
		}		
	}
	printf("\n\n");	

	//printf("Dealer up-card\n");
	//printf("  %s of %s\n", d[0].face, d[0].suit);  // dealer 1 card
	//printf("%d\n", numplayers);
	printf("\n\n");
}

void main(){
	Deck my_deck;
	int i,j;
	char *suits[4] = {"Spades", "Clubs", "Hearts", "Diamonds"};
	char *faces[13] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"}; 
	
	my_deck.is_shuffled = 0;

	for(i=0; i<4; i++){
		for(j=0; j<13; j++){
			my_deck.cards[(i*13) + j].face = faces[j];
			my_deck.cards[(i*13) + j].suit = suits[i];
		}
	}
	
	//print_deck(my_deck.cards);
	
	int numplayers;
	int numdecks;
	char outputfile[30];
	
	printf("What is the name of the output file: ");
	gets(outputfile);
	
	printf("How many players are at the table (between 1 and 6): ");
	scanf("%d", &numplayers);
		while(numplayers > 6 || numplayers < 0){
			printf("How many players are at the table (between 1 and 6): ");
			scanf("%d", &numplayers);
		}
	printf("How many decks are in the shoe (between 1 and 6): ");
	scanf("%d", &numdecks);
		while(numdecks > 6 || numdecks < 0){
			printf("How many decks are in the shoe (between 1 and 6): ");
			scanf("%d", &numdecks);
		}
		//print_deck(my_deck.cards, numdecks);
		shuffle(my_deck.cards, numdecks);
		print_deck(my_deck.cards, numdecks);

}


Is This A Good Question/Topic? 0
  • +

Replies To: BlackJack - having to deal cards with more than one deck

#2 janotte  Icon User is offline

  • code > sword
  • member icon

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

Re: BlackJack - having to deal cards with more than one deck

Posted 09 April 2009 - 04:39 AM

1 - main() always returns int not void.

2 - Where do make use of the variable "totalCards"?

3 - You should never make use of gets(). It is highly unsafe. Consider getchar() or getline() instead.

4 - Don't give the array dimension here
void print_deck(Card d[312], int numdecks){
just Card d[] is fine

Probably more but that's enough for now.
Was This Post Helpful? 0
  • +
  • -

#3 deborah_morales1990  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 08-April 09

Re: BlackJack - having to deal cards with more than one deck

Posted 12 April 2009 - 09:54 PM

hello,
in my blackjack i have this:
char *faces[13] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"}; 


does anyone know how i can assign values to those?
make Ace equal to one
Jack = 10
Queen = 10
King = 10;

Im trying to use pointers but its not exactly doing what i want it to do.
this is what i have:
	char *suits[4] = {"Spades", "Clubs", "Hearts", "Diamonds"};
	char *faces[13]= {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
	char values[13] = {1,2,3,4,5,6,7,8,9,10,10,10,10};

	*faces = values;
	printf("%d\n", *faces[0]); //1
	printf("%d\n", *faces[1]); //50
	printf("%d\n", *faces[2]); //51
	printf("%d\n", *faces[3]); //52
	printf("%d\n", *faces[4]); //53 


why is it that *faces[0] = 1 and then it jumps to 50 when *faces[1] ?

This post has been edited by deborah_morales1990: 12 April 2009 - 09:55 PM

Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

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

Re: BlackJack - having to deal cards with more than one deck

Posted 12 April 2009 - 11:24 PM

View Postdeborah_morales1990, on 12 Apr, 2009 - 08:54 PM, said:

know how i can assign values to those?
make Ace equal to one
Jack = 10
Queen = 10
King = 10;


One of these things is not like the others.
One of these things is not the same.

	char *suits[4] = {"Spades", "Clubs", "Hearts", "Diamonds"};
	char *faces[13]= {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
	char values[13] = {1,2,3,4,5,6,7,8,9,10,10,10,10};



Don't you want the items in values[] to be integers rather than characters?

Alternatively, if you do want the items in values[] to be char then talk me through what this:
char *suits[4]
is doing and why the same logic doesn't apply here:
char values[13]

This post has been edited by janotte: 12 April 2009 - 11:58 PM

Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: BlackJack - having to deal cards with more than one deck

Posted 13 April 2009 - 05:47 AM

Faces, suits, and all the card stuff is really the least of your problems.

You have an array of 312. You initialize it here:
for(i=0; i<4; i++){
	for(j=0; j<13; j++){



So, you've initialized the first 52 slots in the array. Period. If you're lucky the rest of those slots are null, but they may not be.

Now you sort and every card slot after 51 that has junk data get's combined into the mess.

Make some more functions, they don't cost anything. Here's a couple I'd recommend:
// add the standard 52 cards to a card array
void addDeck(Card *d) { /* your code here */ }


// calls addDeck numdecks times
// moving the pointer up by 52 each time
void loadShoe(Card *d, int numdecks) { /* your code here */ }




Also, you're not using shoe. That's ok, you don't have to. I'd modify deck, though:
typedef struct{
	Card cards[312];
	int numberOfDecks;
	int numberOfCards; // this will go down as you deal
	// int is_shuffled; who cares about this one?
} Deck;



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1