The Psychic Poker Player
The Psychic Poker Player |
In 5-card draw poker, a player is dealt a hand of five cards (which may be looked at). The player may then discard between zero and five of his or her cards and have them replaced by the same number of cards from the top of the deck (which is face down). The object is to maximize the value of the final hand. The different values of hands in poker are given at the end of this problem.
Normally the player cannot see the cards in the deck and so must use probability to decide which cards to discard. In this problem, we imagine that the poker player is psychic and knows which cards are on top of the deck. Write a program which advises the player which cards to discard so as to maximize the value of the resulting hand.
Input and Output
Input will consist of a series of lines, each containing the initialfive cards in the hand then the first five cards on top of the deck.Each card is represented as a two-character code. The first characteris the face-value (A=Ace, 2-9, T=10, J=Jack, Q=Queen, K=King) and thesecond character is the suit (C=Clubs, D=Diamonds, H=Hearts,S=Spades). Cards will be separated by single spaces. Each input linewill be from a single valid deck, that is there will be no duplicatecards in each hand and deck.
Each line of input should produce one line of output, consisting ofthe initial hand, the top five cards on the deck, and the best valueof hand that is possible. Input is terminated by end of file.
Use the sample input and output as a guide. Note that the order of the cards in the player's hand is irrelevant, but the order of the cards in the deck is important because the discarded cards must be replaced from the top of the deck. Also note that examples of all types of hands appear in the sample output, with the hands shown in decreasing order of value.
Sample Input
TH JH QC QD QS QH KH AH 2S 6S 2H 2S 3H 3S 3C 2D 3D 6C 9C TH 2H 2S 3H 3S 3C 2D 9C 3D 6C TH 2H AD 5H AC 7H AH 6H 9H 4H 3C AC 2D 9C 3S KD 5S 4D KS AS 4C KS AH 2H 3C 4H KC 2C TC 2D AS AH 2C 9S AD 3C QH KS JS JD KD 6C 9C 8C 2D 7C 2H TC 4C 9S AH 3D 5S 2H QD TD 6S KH 9H AD QH
Sample Output
Hand: TH JH QC QD QS Deck: QH KH AH 2S 6S Best hand: straight-flush Hand: 2H 2S 3H 3S 3C Deck: 2D 3D 6C 9C TH Best hand: four-of-a-kind Hand: 2H 2S 3H 3S 3C Deck: 2D 9C 3D 6C TH Best hand: full-house Hand: 2H AD 5H AC 7H Deck: AH 6H 9H 4H 3C Best hand: flush Hand: AC 2D 9C 3S KD Deck: 5S 4D KS AS 4C Best hand: straight Hand: KS AH 2H 3C 4H Deck: KC 2C TC 2D AS Best hand: three-of-a-kind Hand: AH 2C 9S AD 3C Deck: QH KS JS JD KD Best hand: two-pairs Hand: 6C 9C 8C 2D 7C Deck: 2H TC 4C 9S AH Best hand: one-pair Hand: 3D 5S 2H QD TD Deck: 6S KH 9H AD QH Best hand: highest-card
题目大意:手里有五张牌,桌上有五张牌,手里的牌可以拿出来,然后从桌面的牌中从上到下取牌来补回牌数,问能得到最大的牌值是多少。
牌的组合值:
花色相同,是个顺子:straight-flush
四个相同:four-of-a-kind
三个相同,还有一对:full-house
花色相同:flush
是个顺子:straight
三个相同:three-of-a-kind
有两个对:two-pairs
有一个对:one-pairs
不是以上:hightest-card
另外:TJQKA也是一个顺子
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char BestHand[9][20]={"straight-flush","four-of-a-kind","full-house","flush",
"straight","three-of-a-kind","two-pairs","one-pair","highest-card"};//每种牌的各个值
int Number[10],Color[10],k;
char card[10][5];
int ba[5]={16,8,4,2,1};
int num(char s)
{
if(s>='2'&&s<='9') return int(s-'0');
else if(s=='A') return 1;
else if(s=='T') return 10;
else if(s=='J') return 11;
else if(s=='Q') return 12;
return 13;
}
int color(char s)
{
if(s=='C') return 1;
else if(s=='D') return 2;
else if(s=='H') return 3;
return 4;
}
void ChangeNum(char *str)
{
Number[k]=num(str[0]);//把牌点数换成数值
Color[k]=color(str[1]);//花色也换成数值
k++;
}
bool link(int A[])
{
for(int i=1;i<10;i++)
{
if(A[i]>1) {return false;}
else if(A[i]==1){
int j;
for(j=i;j<=i+4;j++)
{
if(A[j]!=1) break;
}
if(j>i+4) return true;
}
}
if(A[1]==1&&A[10]==1&&A[11]==1&&A[12]==1&&A[13]==1)
return true;
return false;
}//判断是否为顺子
int judge(int s)
{
int A[20],B[20],C[20];
memset(A,0,sizeof(A));
memset(B,0,sizeof(B));
memset(C,0,sizeof(C));
for(int i=0;i<5;i++)
{
if(s/ba[i])
B[i]=0;
else B[i]=1;
s=s%ba[i];
}//根据枚举的值,去或留卡牌,为B[i]为0表示留,为1表示去。
int cnt=0;
for(int i=0;cnt<5;i++)
if(!B[i]){
A[Number[i]]++;
C[Color[i]]++;
cnt++;
}//换牌后的牌
int flagnum=0,colornum=0;
for(int i=1;i<=13;i++)
if(A[i]) flagnum++;//计算不同点数的牌的个数
for(int i=1;i<=4;i++)
if(C[i]) colornum++;//计算不同颜色的牌的个数
bool lin=link(A);//判断是否为顺子
int maxcnt=0;
for(int i=1;i<=13;i++)
if(A[i]>maxcnt) maxcnt=A[i];//计算最大的相同牌的相同的个数
if(colornum==1&&lin) return 8;
else if(maxcnt==4) return 7;
else if(maxcnt==3&&flagnum==2) return 6;
else if(colornum==1) return 5;
else if(lin) return 4;
else if(maxcnt==3) return 3;
else if(maxcnt==2&&flagnum==3) return 2;
else if(maxcnt==2) return 1;
else return 0;//从高值到低值逐一判断,一个符合就返回,得出该情况所能表示的最大牌值
}
int main()
{
while(1)
{
char str[4];
bool flag=true;
k=0;
int sum=0;
for(int i=0;i<10;i++)
{
if(scanf("%s",str)!=1)
{flag=false;break;}
ChangeNum(str);
strcpy(card[i],str);
}
if(!flag) break;
int Max=0;
for(int i=0;i<32;i++)//枚举全部情况,从00000到11111;
{
int temp=judge(i);
if(temp>Max) Max=temp;//得到最大的那个值
}
printf("Hand:");
for(int i=0;i<5;i++)
printf(" %s",card[i]);
// cout<<hand[i];
printf(" Deck:");
for(int i=5;i<10;i++)
printf(" %s",card[i]);
printf(" Best hand: %s\n",BestHand[8-Max]);
}
return 0;
}