You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
169 lines
3.6 KiB
169 lines
3.6 KiB
/*
|
|
* pr_04.c
|
|
*
|
|
* Created on: Jun 9, 2013
|
|
* Author: delmadord
|
|
*/
|
|
|
|
/* Classifies a poker hand, poker.c modified */
|
|
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#define NUM_RANKS 13
|
|
#define NUM_SUITS 4
|
|
#define NUM_CARDS 5
|
|
|
|
// Prototypes
|
|
void read_cards(int num_in_rank[NUM_RANKS], int num_in_suit[NUM_SUITS]);
|
|
void analyze_hand(int num_in_rank[NUM_RANKS], int num_in_suit[NUM_SUITS],
|
|
int hand[5]);
|
|
void print_result(int hand[5]);
|
|
|
|
int main (void) {
|
|
int num_in_rank[NUM_RANKS];
|
|
int num_in_suit[NUM_SUITS];
|
|
// Represents straight, flush, 4-of-a-kind, 3-of-a-kind
|
|
// and number of pairs
|
|
int hand[5] = {0, 0, 0, 0, 0};
|
|
|
|
while (1) {
|
|
read_cards(num_in_rank, num_in_suit);
|
|
analyze_hand(num_in_rank, num_in_suit, hand);
|
|
print_result(hand);
|
|
}
|
|
}
|
|
|
|
// Reads the card into external variables
|
|
void read_cards(int num_in_rank[NUM_RANKS], int num_in_suit[NUM_SUITS]) {
|
|
bool card_exists[NUM_RANKS][NUM_SUITS];
|
|
char ch, rank_ch, suit_ch;
|
|
int rank, suit;
|
|
bool bad_card;
|
|
int cards_read = 0;
|
|
|
|
for (rank = 0; rank < NUM_RANKS; rank++) {
|
|
num_in_rank[rank] = 0;
|
|
for (suit = 0; suit < NUM_SUITS; suit++)
|
|
card_exists[rank][suit] = false;
|
|
}
|
|
|
|
for (suit = 0; suit < NUM_SUITS; suit++)
|
|
num_in_suit[suit] = 0;
|
|
|
|
while (cards_read < NUM_CARDS) {
|
|
bad_card = false;
|
|
|
|
printf("Enter a card: ");
|
|
|
|
rank_ch = getchar();
|
|
switch (rank_ch) {
|
|
case '0': exit(EXIT_SUCCESS);
|
|
case '2': rank = 0; break;
|
|
case '3': rank = 1; break;
|
|
case '4': rank = 2; break;
|
|
case '5': rank = 3; break;
|
|
case '6': rank = 4; break;
|
|
case '7': rank = 5; break;
|
|
case '8': rank = 6; break;
|
|
case '9': rank = 7; break;
|
|
case 't': case 'T': rank = 8; break;
|
|
case 'j': case 'J': rank = 9; break;
|
|
case 'q': case 'Q': rank = 10; break;
|
|
case 'k': case 'K': rank = 11; break;
|
|
case 'a': case 'A': rank = 12; break;
|
|
default: bad_card = true;
|
|
}
|
|
|
|
suit_ch = getchar();
|
|
switch (suit_ch) {
|
|
case 'c': case 'C': suit = 0; break;
|
|
case 'd': case 'D': suit = 1; break;
|
|
case 'h': case 'H': suit = 2; break;
|
|
case 's': case 'S': suit = 3; break;
|
|
default: bad_card = true;
|
|
}
|
|
|
|
while ((ch = getchar()) != '\n')
|
|
if (ch != ' ')
|
|
bad_card = true;
|
|
|
|
if (bad_card)
|
|
printf("Bad card; ignored.\n");
|
|
else if (card_exists[rank][suit])
|
|
printf("Duplicate card; ignored.\n");
|
|
else {
|
|
num_in_rank[rank]++;
|
|
num_in_suit[suit]++;
|
|
card_exists[rank][suit] = true;
|
|
cards_read++;
|
|
}
|
|
}
|
|
}
|
|
|
|
void analyze_hand(int num_in_rank[NUM_RANKS], int num_in_suit[NUM_SUITS],
|
|
int hand[5]) {
|
|
int rank, suit, i, num_consec = 0;
|
|
for (i = 0; i < 5; i++)
|
|
hand[i] = 0;
|
|
|
|
|
|
// Check for flush
|
|
for (suit = 0; suit < NUM_SUITS; suit++)
|
|
if (num_in_suit[suit] == NUM_CARDS)
|
|
hand[1] = 1;
|
|
|
|
// Check for straight
|
|
rank = 0;
|
|
while (num_in_rank[rank] == 0)
|
|
rank++;
|
|
for (; rank < NUM_RANKS && num_in_rank[rank] > 0; rank++)
|
|
num_consec++;
|
|
if (num_consec == NUM_CARDS) {
|
|
hand[0] = true;
|
|
return;
|
|
}
|
|
|
|
// Check for 4-of-a-kind, 3-of-a-kind and pairs
|
|
for (rank = 0; rank < NUM_RANKS; rank++) {
|
|
if (num_in_rank[rank] == 4)
|
|
hand[2] = true;
|
|
if (num_in_rank[rank] == 3)
|
|
hand[3] = true;
|
|
if (num_in_rank[rank] == 2)
|
|
hand[4]++;
|
|
|
|
}
|
|
}
|
|
|
|
void print_result(int hand[5]) {
|
|
if (hand[0] && hand[1])
|
|
printf("Straight flush");
|
|
else if (hand[2])
|
|
printf("Four of a kind");
|
|
else if (hand[3] && hand[4] == 1)
|
|
printf("Full house");
|
|
else if (hand[1])
|
|
printf("Flush");
|
|
else if (hand[0])
|
|
printf("Straight");
|
|
else if (hand[3])
|
|
printf("Three of a kind");
|
|
else if (hand[4] == 2)
|
|
printf("Two pairs");
|
|
else if (hand[4] == 1)
|
|
printf("Pair");
|
|
else
|
|
printf("High card");
|
|
|
|
printf("\n\n");
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|