#include <stdio.h>
#define NCARDS 52
#define NSUITS 4
char Values[] = "23456789TJQKA";
char Suits[] = "CDHS";
#define HIGH_CARD 1
#define PAIR 2
#define TWO_PAIRS 3
#define THREE_OF_A_KIND 4
#define STRAIGHT 5
#define FLUSH 6
#define FULL_HOUSE 7
#define FOUR_OF_A_KIND 8
#define STRAIGHT_FLUSH 9
int rankCard(char value, char suit)
{
int i,j;
for(i=0; i<(NCARDS/NSUITS); i++)
if(Values[i]==value)
for(j=0; j<NSUITS; j++)
if(Suits[j]==suit)
return i*NSUITS+j;
}
int maxType(int cards[5])
{
int values[13] = {0};
int i,temp;
int temp_count=0;
int straight,flush,three=0,pairs_count=0;
straight = flush = 1;
for(i=0;i<5;i++)
{
if(i==0) temp = cards[i]%NSUITS;
else if(temp != cards[i]%NSUITS) flush = 0;
values[cards[i] / NSUITS] ++;
}
temp = -1;
for(i=0;i<13;i++)
{
if(values[i]==4) return FOUR_OF_A_KIND;
if(values[i]==3) three=1;
if(values[i]==2) pairs_count++;
if(values[i]>0) temp_count++;
if(straight==1)
{
if(values[i]==1)
{
if(temp!=-1 && temp!=i-1) straight = 0;
else temp = i;
}
else if(values[i]>1) straight = 0;
}
}
if(flush&&straight) return STRAIGHT_FLUSH;
if(temp_count==2) return FULL_HOUSE;
if(flush) return FLUSH;
if(straight) return STRAIGHT;
if(three) return THREE_OF_A_KIND;
if(pairs_count==2) return TWO_PAIRS;
if(pairs_count==1) return PAIR;
return HIGH_CARD;
}
int compareValue(int type, int blackCards[5], int whiteCards[5])
{
int blackValues[13] = {0};
int whiteValues[13] = {0};
int i;
for(i=0;i<5;i++)
{
blackValues[blackCards[i] / NSUITS] ++;
whiteValues[whiteCards[i] / NSUITS] ++;
}
switch(type)
{
case STRAIGHT_FLUSH:
case FLUSH:
case STRAIGHT:
case HIGH_CARD:
for(i=12;i>=0;i--)
{
if(blackValues[i]>whiteValues[i]) return 1;
else if(blackValues[i]<whiteValues[i]) return -1;
}
break;
case FOUR_OF_A_KIND:
case FULL_HOUSE:
case THREE_OF_A_KIND:
for(i=12;i>=0;i--)
{
if(blackValues[i]>=3) return 1;
else if(whiteValues[i]>=3) return -1;
}
break;
case TWO_PAIRS:
case PAIR:
for(i=12;i>=0;i--)
{
if(blackValues[i]==2 && whiteValues[i]<2) return 1;
else if(whiteValues[i]==2 && whiteValues[i]<2) return -1;
}
for(i=12;i>=0;i--)
{
if(blackValues[i]==1 && whiteValues[i]==0) return 1;
else if(whiteValues[i]==1 && blackValues[i]==0) return -1;
}
break;
}
return 0;
}
void compareCards(int blackCards[5], int whiteCards[5])
{
int a = maxType(blackCards);
int b = maxType(whiteCards);
int c;
if(a == b)
{
c = compareValue(a,blackCards,whiteCards);
if(c==0)
printf("%s\n","Tie.");
else
printf("%s\n",c==1?"Black wins.":"White wins.");
}
else
printf("%s\n",a>b?"Black wins.":"White wins.");
}
int main()
{
char cards[10][3];
int blackCards[5];
int whiteCards[5];
int i,j;
while(1)
{
if(scanf("%s",cards[0])==EOF) break;
for(i=0; i<10; i++)
{
if(i>0) scanf("%s",cards[i]);
if(i<5) blackCards[i] = rankCard(cards[i][0],cards[i][1]);
else whiteCards[i-5] = rankCard(cards[i][0],cards[i][1]);
}
compareCards(blackCards,whiteCards);
}
return 0;
}
UVa 10315 Poker Hands
最新推荐文章于 2020-10-31 16:43:17 发布