Programming Contest: Poker
Description
Situation: You are a poker player that has been dealt 4 of the 5 cards of your hand. You would like to know what is the best possible hand you could make when you are given your fifth card.
Solution Strategy: Make functions to determine which hands are possible given the 4 cards, rather than functions that check if a hand of 5 cards is of a specific type of hand. This will be quicker to code, as well as speed runtime.
[ complete description ] [ poker.java ]
Sample Input and Output
TS QS JS 8S Best possible hand: STRAIGHT FLUSH 2S 4H QD 6C Best possible hand: ONE PAIR 2H 4H JH AH Best possible hand: FLUSH AS AH AD AC Best possible hand: FOUR OF A KIND 2S 3S 4S 5S Best possible hand: STRAIGHT FLUSH 7H 6H 4H 3H Best possible hand: STRAIGHT FLUSH AD KH QS JC Best possible hand: STRAIGHT TD 7S JS 8H Best possible hand: STRAIGHT AD 8H 9D AC Best possible hand: THREE OF A KIND 2S 2C 2H 7S Best possible hand: FOUR OF A KIND 7H 7S TD TC Best possible hand: FULL HOUSE 2C 5H 6D 7H Best possible hand: ONE PAIR AD AC 9H 9D Best possible hand: FULL HOUSE AH 9D KH QH Best possible hand: ONE PAIR KD 7D 2D AD Best possible hand: FLUSH TH QD KS AS Best possible hand: STRAIGHT
Solution
class Card
{
char suite;
int number;
public Card(String crd)
{
suite = crd.charAt(1);
switch(crd.charAt(0))
{
case 'T': number = 10; break;
case 'J': number = 11; break;
case 'Q': number = 12; break;
case 'K': number = 13; break;
case 'A': number = 14; break;
default: number = crd.charAt(0) - '0';
}
}
public String toString()
{
switch(number)
{
case 10: return ""+'T'+suite;
case 11: return ""+'J'+suite;
case 12: return ""+'Q'+suite;
case 13: return ""+'K'+suite;
case 14: return ""+'A'+suite;
default: return ""+number+suite;
}
}
}
Card cards[][];
public void go() throws Exception
{
Scanner in = new Scanner(new File("poker.in"));
int n = in.nextInt();
in.nextLine();
cards = new Card[n][4];
for(int i=0; i<n; i++)
{
String cline[] = in.nextLine().split(" ");
for(int j=0; j<4; j++)
{
cards[i][j] = new Card(cline[j]);
prn(""+cards[i][j]+" ");
}
prnln("");
prnln("Best possible hand: "+solve(cards[i]));
prnln("");
}
}
public String solve(Card hand[])
{
if(straight_flush(hand)) return "STRAIGHT FLUSH";
if(four_of_a_kind(hand)) return "FOUR OF A KIND";
if(full_house(hand)) return "FULL HOUSE";
if(flush(hand)) return "FLUSH";
if(straight(hand)) return "STRAIGHT";
if(three_of_a_kind(hand)) return "THREE OF A KIND";
if(two_pair(hand)) return "TWO PAIR";
return "ONE PAIR";
}
public boolean straight_flush(Card hand[])
{
return straight(hand) && flush(hand);
}
public boolean four_of_a_kind(Card hand[])
{
int nums[] = new int[13];
for(int i=0; i<4; i++)
{
nums[hand[i].number-2]++;
}
for(int i=0; i<13; i++)
{
if(nums[i]>=3)
{
return true;
}
}
return false;
}
public boolean full_house(Card hand[])
{
int two_count = 0;
int nums[] = new int[13];
for(int i=0; i<4; i++)
{
nums[hand[i].number-2]++;
}
for(int i=0; i<13; i++)
{
if(nums[i]>=2)
{
two_count++;
}
}
return (two_count>=2);
}
public boolean flush(Card hand[])
{
char suite = hand[0].suite;
for(int i=0; i<4; i++)
{
if(hand[i].suite!=suite)
{
return false;
}
}
return true;
}
public boolean straight(Card hand[])
{
int count = 0;
int nums[] = new int[13], sums[] = new int[13];
for(int i=0; i<4; i++)
{
nums[hand[i].number-2]++;
if(nums[hand[i].number-2]>1)
return false;
}
for(int i=0; i<13; i++)
{
int forward=0, backward=0, j;
if(nums[i]==1)
{
j=i;
while(forward<3 && j>=0 && j<13)
{
forward++;
sums[j++]++;
}
j=0;
while(forward<3 && j>=0 && j<13)
{
forward++;
sums[j++]++;
}
j=i-1;
while(backward<2 && j>=0 && j<13)
{
backward++;
sums[j--]++;
}
j=12;
while(backward<2 && j>=0 && j<13)
{
backward++;
sums[j--]++;
}
}
}
for(int i=0; i<13; i++)
{
if(sums[i]>=4)
{
return true;
}
}
return false;
}
public boolean three_of_a_kind(Card hand[])
{
int nums[] = new int[13];
for(int i=0; i<4; i++)
{
nums[hand[i].number-2]++;
}
for(int i=0; i<13; i++)
{
if(nums[i]>=2)
{
return true;
}
}
return false;
}
public boolean two_pair(Card hand[])
{
// if a three of a kind is not possible, two pairs won't be
return false;
}