Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAXC 5 /* define number of cards on your hand here */
#define PLAYER 2 /* define number of players */
/* prototype */
void shuffle (int a[][13]);
void deal (const int a[][13], const char *b[], const char *c[], const int player);
void record (const int dDeck[][13], int hSuit[], int hFace[], const int player);
int pair ( const int hFace[] );
int flush ( const int hSuit[] );
int straight ( const int hFace[] );
int main (void)
{
// Initialize face, suit and hand arrays
const char *suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"};
const char *face[13] = {"Ace", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King"};
int deck [4][13] = {0};
int handSuit1[MAXC] = {0};
int handFace1[MAXC] = {0};
int handSuit2[MAXC] = {0};
int handFace2[MAXC] = {0};
int *playerSuit [2] = {handSuit1, handSuit2};
int *playerFace [2] = {handFace1, handFace2};
// Shuffle and deal
shuffle (deck);
for (int i=0; i<=PLAYER-1; i++)
{
printf ("%s%d%s", "Player ", i+1, ":\n");
deal (deck, suit, face, i);
record (deck, playerSuit[i], playerFace[i], i);
switch (pair (playerFace[i]))
{
case 1:
puts("You have a pair");
break;
case 2:
puts("You have two pair");
break;
case 3:
puts("You have three of a kind");
break;
case 0:
break;
default:
puts("You have four of a kind!");
}
if (flush(playerSuit[i]))
puts("You have a flush!");
if (straight(playerFace[i]))
puts("You have a straight!");
puts("");
}
return 0;
}
/* shuffle the deck */
void shuffle (int sDeck[][13])
{
srand(time(0));
int row, col;
for (int cards = 1; cards <= 52; cards++)
{
do
{
row = rand() % 4;
col = rand() % 13;
}
while (sDeck[row][col] != 0);
sDeck[row][col] = cards;
}
}
/* deal the cards */
void deal (const int dDeck[][13], const char *dSuit[], const char *dFace[], const int player)
{
for (int cards = player * MAXC + 1; cards <= (player+1)*MAXC; cards++)
for (int row = 0; row <= 3; row++)
for (int col = 0; col <= 12; col++)
if (dDeck[row][col] == cards)
printf ("%5s%s%-8s\n", dFace[col], " of ", dSuit[row]);
}
/* record hands of players into arrays */
void record (const int dDeck[][13], int hSuit[], int hFace[], const int player)
{
for (int cards = player * MAXC + 1; cards <= (player+1)*MAXC; cards++)
for (int row = 0; row <= 3; row++)
for (int col = 0; col <= 12; col++)
if (dDeck[row][col] == cards)
{
hSuit[cards - 1] = row;
hFace[cards - 1] = col;
}
}
/* determine if pairs exist in hand */
int pair ( const int hFace[] )
{
int counter = 0;
for ( int i = 0; i <= (MAXC - 2); i++)
for ( int j=i+1; j <= (MAXC - 1); j++ )
if (hFace[i] == hFace[j])
counter++;
return counter;
}
/* determine if flush exist in hand */
int flush ( const int hSuit[] )
{
for ( int i = 0; i <= (MAXC - 2); i++)
for ( int j=i+1; j <= (MAXC - 1); j++ )
if (hSuit[i] != hSuit[j])
return 0;
return 1;
}
/* determine if straight exist in hand */
int straight ( const int hFace[] )
{
for ( int i = 0; i <= (MAXC - 2); i++)
for ( int j=i+1; j <= (MAXC - 1); j++ )
if ((hFace[i] == hFace[j]) || (abs(hFace[i] - hFace[j]) >= MAXC))
return 0;
return 1;
}