I've been working on a problem that simulates a poker hand. The program shuffles a standard deck of 52 cards and then deals a 5-card poker hand. Then it evaluates what kind of hand you have. Here's what I have so far:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUM_FACES 13
#define NUM_SUITS 4
#define NUM_CARDS 5
#define TRUE 1
#define FALSE 0
typedef int bool;
bool straight, flush, four, three;
int pairs; /* can be 0, 1, or 2 */
void shuffle( int wDeck[][ 13 ] );
void deal( int wDeck[][ 13 ], char *wFace[], char *wSuit[] );
void analyze_hand(int [], int []);
void print_result(void);
int getSuit( int Card );
int getFace( int Card );
int main()
{
char *Suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };
char *Face[ 13 ] = { "Ace", "Deuce", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
int deck[ 4 ][ 13 ] = { 0 };
int num_in_Face[NUM_FACES];
int num_in_Suit[NUM_SUITS];
srand( time( 0 ) );
shuffle( deck );
deal( deck, Face, Suit );
analyze_hand(num_in_Face, num_in_Suit);
return 0;
}
void shuffle( int wDeck[][ 13 ] )
{
int Suit;
int Face;
int card;
for ( card = 1; card <= 5; card++ )
{
do
{
Suit = rand() % 4; Face = rand() % 13;
} while( wDeck[ Suit ][ Face ] != 0 );
wDeck[ Suit ][ Face ] = card;
}
}
void deal( int wDeck[][ 13 ], char *wFace[], char *wSuit[] )
{
int card;
int Suit;
int Face;
for ( card = 1; card <= 5; card++ )
{
for ( Suit = 0; Suit <= 3; Suit++ )
{
for ( Face = 0; Face <= 12; Face++ )
{
if ( wDeck[ Suit ][ Face ] == card )
{
printf( "%5s of %-8s%c", wFace[ Face ], wSuit[ Suit ], card % 2 == 0 ? '\n' : '\t' );
}
}
}
}
}
void analyze_hand(int num_in_Face[], int num_in_Suit[])
{
int num_consec = 0;
int Face, Suit;
straight = FALSE;
flush = FALSE;
four = FALSE;
three = FALSE;
pairs = 0;
/* check for flush */
for (Suit = 0; Suit < NUM_SUITS; Suit++)
{
if (num_in_Suit[Suit] == NUM_CARDS) {
flush = TRUE;
}
}
/* check for straight */
Face = 0;
while (num_in_Face[Face] == 0) Face++;
{
for (; Face < NUM_FACES && num_in_Face[Face]; Face++)
num_consec++;
{
if (num_consec == NUM_CARDS) {
straight = TRUE;
return;
}
}
}
/* check for 4-of-a-kind, 3-of-a-kind, and pairs */
for (Face = 0; Face < NUM_FACES; Face++) {
if (num_in_Face[Face] == 4) {
four = TRUE;
}
if (num_in_Face[Face] == 3) {
three = TRUE;
}
if (num_in_Face[Face] == 2) {
pairs++;
}
}
if (straight && flush) {
printf("You have a Straight Flush.\n\n");
}
else if (four) {
printf("You have Four of a Kind.\n\n");
}
else if (three && pairs == 1) {
printf("You have a Full House.\n\n");
}
else if (flush) {
printf("You have a Flush.\n\n");
}
else if (straight) {
printf("You have a Straight.\n\n");
}
else if (three) {
printf("You have Three of a Kind.\n\n");
}
else if (pairs == 2) {
printf("You have two pairs.\n\n");
}
else if (pairs == 1) {
printf("You have one pair.\n\n");
}
else {
printf("You have a runt.\n\n");
}
}
When I ran the program, it compiled and executed perfectly except for one detail. Every single hand I play, it says "You have a runt" even when I do indeed have a poker hand. Here's one example:
Seven of Hearts Jack of Spades
Three of Clubs Seven of Clubs
Five of Diamonds You have a runt.
Press any key to continue
It should say "You have one pair" instead. Any suggestions?