Code:
Points of interest for applying suitsInHand and facesInHand as counters for suits and faces of a hand
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SUITS 4 // suits of cards
#define FACES 13 // two - ace
#define AVAILABLE 0 // card not drawn from deck
#define TAKEN 1 // card has been drawn from deck
#define SIZE 5
#define TRUE 1
#define FALSE 0
void dealACard(char *suits[], char *faces[], int deck[][FACES]);
void dealAHand(char *suits[], char *faces[], int deck[][FACES]);
void analyzeHand(int suitsInHand[], int facesInHand[]); //igrone this for now
typedef int bool;
bool straight, flush, four, three;
int pairs; /* can be 0, 1, or 2 */
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 }; // using array to make sure the same card is drawn from a single deck
int i;
int suitsInHand[SUITS], facesInHand[FACES];
srand(time(NULL));
for(i = 0; i < 10; i++) {
dealAHand(suits, faces, deck);
analyzeHand(suitsInHand, facesInHand); // igrone for now
}
system("pause");
}
void dealAHand(char *suits[], char *faces[], int deck[][FACES]){
int i;
for(i = 0; i < 5; i++)
dealACard(suits, faces, deck);
printf("\n");
}
void dealACard(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;
printf("%s of %s \n", faces[faceIndex], suits[suitIndex]);
}
/*
analyzeHand: Determines whether the hand contains a
straight, a flush, four-of-a-kind,
and/or a three-of-a-kind; determines the
number of pairs; stores the results into
the external variables straight, flush,
four, three, and pairs.
*/
void analyzeHand(int suitsInHand[], int facesInHand[]){
int num_consec = 0;
int rank, suit;
straight = FALSE;
flush = FALSE;
four = FALSE;
three = FALSE;
pairs = 0;
/* check for flush */
for (suit = 0; suit < SUITS; suit++)
if (suitsInHand[suit] == 5)
flush = TRUE;
/* check for straight */
rank = 0;
while (facesInHand[rank] == 0)
rank++;
for (; rank < FACES && facesInHand[rank]; rank++)
num_consec++;
if (num_consec == 5) {
straight = TRUE;
return;
}
/* check for 4-of-a-kind, 3-of-a-kind, and pairs */
for (rank = 0; rank < FACES; rank++) {
if (facesInHand[rank] == 4)
four = TRUE;
if (facesInHand[rank] == 3)
three = TRUE;
if (facesInHand[rank] == 2)
pairs++;
}
if (straight && flush)
printf("Straight flush\n\n");
else if (four)
printf("Four of a kind\n\n");
else if (three && pairs == 1)
printf("Full house\n\n");
else if (flush)
printf("Flush\n\n");
else if (straight)
printf("Straight\n\n");
else if (three)
printf("Three of a kind\n\n");
else if (pairs == 2)
printf("Two pairs\n\n");
else if (pairs == 1)
printf("Pair\n\n");
else
printf("High card\n\n");
}