I really need some help implementing counters. I have to count the suits and faces drawn from the deck but for some reason all outputs is junk numbers. Am I missing something? I what it to come out something like this:

Say a player is dealt:

Ace of Hearts
Ten of Clubs
Ace of Clubs
Eight of Hearts
Seven of Diamonds

2 2 1 0 // see this one counts the 2 hearts drawn, the 2 clubs, etc.
0 0 0 0 0 1 1 0 1 0 0 0 2

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

#define SUITS 4
#define FACES 13
#define AVAILABLE 0
#define TAKEN 1

void dealACard(int suitsInHand[], int facesInHand[],char *suits[], char *faces[], int deck[][FACES]);
void dealAHand(int suitsInHand[], int facesInHand[], char *suits[], char *faces[], int deck[][FACES]);
//void analyzeHand();

main(){
	char *suits[4] = {"Hearts", "Diamonds", "Spades", "Clubs"};
	char *faces[13] = {"Two", "Three", "Four", "Five", "Six", "Seven",
		"Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"};
	int deck[4][13] = { AVAILABLE };
	int i;

	int suitsInHand[4];
	int facesInHand[13];

	int suitsInHand2[4];
	int facesInHand2[13];

	srand(time(NULL));

	dealAHand(suitsInHand, facesInHand, suits, faces, deck);  
	dealAHand(suitsInHand2, facesInHand2, suits, faces, deck);

	system("pause");

}
void dealAHand(int suitsInHand[], int facesInHand[], char *suits[], char *faces[], int deck[][FACES]){
	int i;
	for(i = 0; i < 5; i++)
		dealACard(suitsInHand, facesInHand, suits, faces, deck);

	printf("\n");
}
void dealACard(int suitsInHand[], int facesInHand[], char *suits[], char *faces[], int deck[][FACES]){
	int suitIndex, faceIndex;

	suitIndex = rand() % 4;
	faceIndex = rand() %13;
	
	while( deck[suitIndex][faceIndex] == TAKEN ){
		suitIndex = rand() % 4;
		faceIndex = rand() %13;
	}
	deck[suitIndex][faceIndex] = TAKEN;
       /* Here are the counters but all it outputs is junk. Do I need a for loop?*/
	facesInHand[faceIndex]++;
	suitsInHand[suitIndex]++;

	printf("%s of %s \n", faces[faceIndex], suits[suitIndex]);
	
}

Any suggestions will be much appreciated. If my question still seems a bit hazy, I will be happy to clarify some more. Thanks for any help :)

Take all the way too many cooks, out of the kitchen -- they're spoiling the meal.

Make a struct for each card:

struct card {
  int value;
  char name[20];
  char suit[10];
  char icon; //3,4,5,or 6 in ASCII chart
  int dealt;
  //etc.
}card1;

int main() {
  struct card cards[52];

There's a lot of info to keep track of in a card game. If you gather most of it up into a few structs, it really helps minimize the complexity, and make the whole thing much more reasonable to understand.

Icon's are char's because ASCII table has char's for suits of cards:

for(i = 3; i <7 ; i++)
  printf("%c", card_icon);

For a simple console poker program, they look OK. Better than anything I could draw that small, for sure. I was going to post a pic of the output, but both paint and the image software program I have (Irfanview), think that these suit icons should be replaced by question marks.

They work fine on screen, however. I'm dismayed to see that they don't work in these programs.

Edited 6 Years Ago by Adak: n/a

@adak Thanks for the reply. I use the functions to minimize the complexity of the game though. I'm not really familiar with structs. I'm using suitsInHand and facesInHand as my counter variables.

I'm not much of a card player, but I think it's helpful if you arrange your suits according to their rank, if the cards face values, ever tie:

Diamonds, then Hearts, then Clubs, then Spades.

I believe that's right.

I'm going to butt out here, I guess. When I see stuff like this, I know you're either quite the genius, or quite mad:

suitIndex = rand() % 4;
	faceIndex = rand() %13;
	
	while( deck[suitIndex][faceIndex] == TAKEN ){
		suitIndex = rand() % 4;
		faceIndex = rand() %13;
       }

Good luck!

This article has been dead for over six months. Start a new discussion instead.