Code:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#define FALSE 0
#define TRUE 1
#define SUITS 4
#define FACES 13
#define AVAILABLE 0
#define TAKEN 1
void dealACard(char *suits[], char *faces[], int deck[][FACES], int suitsInHand[], int facesInHand[]);
void dealAHand(char *suits[], char *faces[], int deck[][FACES], int suitsInHand[], int facesInHand []);
void dealAHand2(char *suits[], char *faces[], int deck[][FACES], int suitsInHand2[], int facesInHand2[]);
void analyzeHand(char *suits[], char *faces[], int deck[][FACES], int suitsInHand[], int facesInHand[], int *hand1);
void analyzeHand2(char *suits[], char *faces[], int deck[][FACES], int suitsInHand[], int facesInHand[], int *hand2);
main () {
char *suits[4] = {"Hearts", "Diamonds", "Spades", "Clubs"};
char *faces[13] = {"Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"};
char *win[9] = {"High Card", "Pair", "Two Pairs", "Three of a Kind", "Straight", "Flush", "Full House", "Four of a Kind", "Straight Flush"};
int deck[4][13] = {AVAILABLE};
int suitsInHand[4] = {AVAILABLE};
int facesInHand[13] = {AVAILABLE};
int suitsInHand2[4] = {AVAILABLE};
int facesInHand2[13] = {AVAILABLE};
int hand1 = 0, hand2 = 0;
srand(time(NULL));
dealAHand(suits, faces, deck, suitsInHand, facesInHand);
analyzeHand(suits, faces, deck, suitsInHand, facesInHand, &hand1);
dealAHand2(suits, faces, deck, suitsInHand2, facesInHand2);
analyzeHand2(suits, faces, deck, suitsInHand2, facesInHand2, &hand2);
if (hand1 > hand2)
printf("Player one wins with a %s! \n", win[hand1]);
else if (hand1 < hand2)
printf("Player two wins with a %s! \n", win[hand2]);
system("pause");
}
void dealAHand(char *suits[], char *faces[], int deck[][FACES], int suitsInHand[], int facesInHand[]){
int i;
for (i = 0; i < 5; i++)
dealACard(suits, faces, deck, suitsInHand, facesInHand);
printf("\n");
}
void dealAHand2(char *suits[], char *faces[], int deck[][FACES], int suitsInHand2[], int facesInHand2[]){
int i;
for (i = 0; i < 5; i++)
dealACard(suits, faces, deck, suitsInHand2, facesInHand2);
printf("\n");
}
void dealACard(char *suits[], char *faces[], int deck[][FACES], int suitsInHand[], int facesInHand[]){
int suitsIndex, facesIndex;
suitsIndex = rand() % 4;
facesIndex = rand() % 13;
while (deck[suitsIndex][facesIndex] == TAKEN){
suitsIndex = rand() % 4;
facesIndex = rand() % 13;
}
suitsInHand[suitsIndex] += 1;
facesInHand[facesIndex] += 1;
deck[suitsIndex][facesIndex] = TAKEN;
printf("%s of %s \n", faces[facesIndex], suits[suitsIndex]);
}
void analyzeHand(char *suits[], char *faces[], int deck[][FACES], int suitsInHand[], int facesInHand[], int *hand1)
{
int num_consec = 0;
int rank, suit; // the rank of a card is 0 – 12 (representing cards 2 – Ace)
int straight = FALSE;
int flush = FALSE;
int four = FALSE;
int three = FALSE;
int pairs = 0;
// check for flush – 5 cards of the same suit
for (suit = 0; suit < SUITS; suit++)
if (suitsInHand[suit] == 5)
flush = TRUE;
// check for straight – eg. One each of 5,6,7,8,9
// locate the first card
rank = 0;
while (facesInHand[rank] == 0)
rank++;
// count the consecutive non-zero faces
// the loop above initializes the rank variable as the first non-zero counter
for (; rank < FACES && facesInHand[rank]; rank++)
num_consec++;
if (num_consec == 5) {
straight = TRUE;
return; // don’t bother to check anything else if there’s a straight
}
/* 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");
*hand1 = 8;}
else if (four) {
printf("Four of a kind\n\n");
*hand1 = 7;}
else if (three && pairs == 1) {
printf("Full house\n\n");
*hand1 = 6;}
else if (flush){
printf("Flush\n\n");
*hand1 = 5;}
else if (straight) {
printf("Straight\n\n");
*hand1 = 4;}
else if (three){
printf("Three of a kind\n\n");
*hand1 = 3;}
else if (pairs == 2) {
printf("Two pairs\n\n");
*hand1 = 2;}
else if (pairs == 1){
printf("Pair\n\n");
*hand1 = 1;}
else {
printf("High card\n\n");
*hand1 = 0;}
}
void analyzeHand2(char *suits[], char *faces[], int deck[][FACES], int suitsInHand[], int facesInHand[], int *hand2)
{
int num_consec = 0;
int rank, suit; // the rank of a card is 0 – 12 (representing cards 2 – Ace)
int straight = FALSE;
int flush = FALSE;
int four = FALSE;
int three = FALSE;
int pairs = 0;
// check for flush – 5 cards of the same suit
for (suit = 0; suit < SUITS; suit++)
if (suitsInHand[suit] == 5)
flush = TRUE;
// check for straight – eg. One each of 5,6,7,8,9
// locate the first card
rank = 0;
while (facesInHand[rank] == 0)
rank++;
// count the consecutive non-zero faces
// the loop above initializes the rank variable as the first non-zero counter
for (; rank < FACES && facesInHand[rank]; rank++)
num_consec++;
if (num_consec == 5) {
straight = TRUE;
return; // don’t bother to check anything else if there’s a straight
}
/* 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");
*hand2 = 8;}
else if (four) {
printf("Four of a kind\n\n");
*hand2 = 7;}
else if (three && pairs == 1) {
printf("Full house\n\n");
*hand2 = 6;}
else if (flush){
printf("Flush\n\n");
*hand2 = 5;}
else if (straight) {
printf("Straight\n\n");
*hand2 = 4;}
else if (three){
printf("Three of a kind\n\n");
*hand2 = 3;}
else if (pairs == 2) {
printf("Two pairs\n\n");
*hand2 = 2;}
else if (pairs == 1){
printf("Pair\n\n");
*hand2 = 1;}
else {
printf("High card\n\n");
*hand2 = 0;}
}