Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
char *cardname(int num)
{
static char buf[10];
char suits[] = "HCDS";
char *cards[] = { " 2", " 3", " 4", " 5", " 6", " 7", " 8",
" 9", "10", " J", " Q", " K", " A"};
sprintf(buf, "%s%c", cards[num%13], suits[num/13]);
return buf;
}
char *cardval(int num)
{
static char *cards[] = { "2", "3", "4", "5", "6", "7", "8",
"9", "10", "J", "Q", "K", "A"};
return cards[num%13];
}
int is_x_of_a_kind(int *vals, int x, int *val, int except)
{
int i, ctr;
for(ctr = 0, *val = vals[0], i = 1;i < 5;++i)
{
if(vals[i] == except)
continue;
if(vals[i] == *val)
ctr++;
else
{
if(ctr == x-1)
return 1;
ctr = 0;
*val = vals[i];
}
}
return ctr == x-1;
}
int is_four_of_a_kind(int *vals, int *val)
{
return is_x_of_a_kind(vals, 4, val, -1);
}
int is_three_of_a_kind(int *vals, int *val)
{
return is_x_of_a_kind(vals, 3, val, -1);
}
int is_pair(int *vals, int *val)
{
return is_x_of_a_kind(vals, 2, val, -1);
}
int is_two_pair(int *vals, int *val1, int *val2)
{
if(!is_x_of_a_kind(vals, 2, val1, -1))
return 0;
if(!is_x_of_a_kind(vals, 2, val2, *val1))
return 0;
return 1;
}
int is_full_house(int *vals, int *val1, int *val2)
{
return is_three_of_a_kind(vals, val1) && is_pair(vals, val2);
}
int is_flush(int *vals, int *suits, int *val)
{
int i;
for(i = 0;i < 4;++i)
if(suits[i] != suits[i+1])
return 0;
*val = vals[4];
return 1;
}
int is_straight(int *vals, int *val)
{
int i;
for(i = 0;i < 4;++i)
if(vals[i]+1 != vals[i+1])
return 0;
*val = vals[4];
return 1;
}
int is_straight_flush(int *vals, int *suits, int *val)
{
return is_flush(vals, suits, val) && is_straight(vals, val);
}
void print_hand_rank(int *hand)
{
int i, j, temp;
int vals[5], suits[5];
int highcard1, highcard2;
for(i = 0;i < 5;++i)
{
vals[i] = hand[i]%13;
suits[i] = hand[i]/13;
}
for(i = 0;i < 5;++i)
for(j = i+1;j < 5;++j)
if(vals[i] > vals[j])
{
temp = vals[i];
vals[i] = vals[j];
vals[j] = temp;
temp = hand[i];
hand[i] = hand[j];
hand[j] = temp;
temp = suits[i];
suits[i] = suits[j];
suits[j] = temp;
}
if(is_straight_flush(vals, suits, &highcard1))
printf("Straight flush - %s high\n", cardval(highcard1));
else if(is_four_of_a_kind(vals, &highcard1))
printf("4 of a kind - %s high\n", cardval(highcard1));
else if(is_full_house(vals, &highcard1, &highcard2))
{
char buf[5];
strcpy(buf, cardval(highcard1));
printf("Full house - %s's over %s's\n", buf, cardval(highcard2));
}
else if(is_flush(vals, suits, &highcard1))
printf("Flush - %s high\n", cardval(highcard1));
else if(is_straight(vals, &highcard1))
printf("Straight - %s high\n", cardval(highcard1));
else if(is_three_of_a_kind(vals, &highcard1))
printf("3 of a kind - %s high\n", cardval(highcard1));
else if(is_two_pair(vals, &highcard1, &highcard2))
{
char buf[5];
strcpy(buf, cardval(highcard1));
printf("2 pair - %s's and %s's\n", buf, cardval(highcard2));
}
else if(is_pair(vals, &highcard1))
printf("Pair of %s's\n", cardval(highcard1));
else
printf("%s high\n", cardval(vals[4]));
}
int main(void)
{
int deck[52], hand[5];
int i, j, card, temp;
srand(time(NULL));
for(i = 0;i < 52;++i)
deck[i] = i;
for(i = 0;i < 10;++i)
for(j = 0;j < 52;++j)
{
card = rand()%52;
temp = deck[j];
deck[j] = deck[card];
deck[card] = temp;
}
for(i = 0;i < 5;++i)
hand[i] = deck[i];
for(i = 0;i < 5;++i)
printf("%s ", cardname(hand[i]));
putchar('\n');
print_hand_rank(hand);
return 0;
Seems to work anyway