Beloved minions,
I was thoroughly bored, so I wrote a function that determines the name of your poker hand (5 cards). I couldn’t help myself and started to optimize for code size. :clue:
int h(char[] g){
int n=99,i=-2,j,p=n,q=-p,r=n,s=q;
int[][]y=new int[5][n];
int[]a=y[0],b=y[1],c=y[2],d=y[3],e=y[4];
for(;i<13;a[" A23456789TJQK".indexOf(g[i+=2])]++,b[g[++i]]++);
for(i=n;--i>=0;c[a[i]]=i)if(a[i]>0){p=p<i?p:i;q=q>i?q:i;j=i==1?14:i;r=r<j?r:j;s=s>j?s:j;}
for(i=n;--i>=0;d[b[i]]=i,e[a[i]]++);boolean u,v,w=q-p==4|s-r==4;
for(int f:a)w&=f<2;v=d[5]>0;u=w&v;
for(boolean h:new boolean[]{u&s-r<5,u,c[4]>0,e[3]==1&e[2]==1,v,w,c[3]>0,e[2]==2,e[2]==1}){if(h)break;n--;}
return n;
}
Code size: 477 chars (after trimming lines and removing newlines)
Can you find a bug, or can you come up with a smaller code size? Scraping off some bytes, to prove how I wasn’t paying attention is appreciated too!
Usage:
String[] hands = new String[9];
hands[0] = "3H 5D JS 3C 7C"; // one pair
hands[1] = "JH 4C 2C JD 2H"; // two pair
hands[2] = "7H 3S 7S 7D 7C"; // four of a kind
hands[3] = "8C 3H 8S 8H 3S"; // full house
hands[4] = "8C 3C 4S 8C 3C"; // two pair
hands[5] = "4C 6C 8S 5C 7C"; // straight
hands[6] = "AC 2C 3C 4C 5C"; // straight flush: A,2,3,4,5 (ace low)
hands[7] = "AC KC QC JC TC"; // royal flush: T,J,Q,K,A (ace high)
hands[8] = "AC 8C QA JH TC"; // high card
String[] name = new String[10];
name[0] = "royal flush";
name[1] = "straight flush";
name[2] = "four of a kind";
name[3] = "full house";
name[4] = "flush";
name[5] = "straight";
name[6] = "three of a kind";
name[7] = "two pair";
name[8] = "one pair";
name[9] = "high card";
for(String hand : hands)
System.out.println(hand+": "+name[99-h(hand.toCharArray())]);