Advertisement
Guest User

Poker hand class implementing Comparable

a guest
Sep 16th, 2012
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.72 KB | None | 0 0
  1.     static class Hand implements Comparable{
  2.         Card[] c;
  3.         byte hand, handCompA, handCompB;
  4.         Hand(String a,String b,String c,String d,String e){
  5.             this.c=new Card[5];
  6.             this.c[0]=new Card(a);
  7.             this.c[1]=new Card(b);
  8.             this.c[2]=new Card(c);
  9.             this.c[3]=new Card(d);
  10.             this.c[4]=new Card(e);
  11.             java.util.Arrays.sort(this.c);
  12.             if((this.c[1].s==this.c[0].s)&&(this.c[2].s==this.c[0].s)&&(this.c[3].s==this.c[0].s)&&(this.c[4].s==this.c[0].s)){
  13.                 if((this.c[1].v==this.c[0].v-1)&&(this.c[2].v==this.c[0].v-2)&&(this.c[3].v==this.c[0].v-3)&&(this.c[4].v==this.c[0].v-4)){
  14.                     handCompA=this.c[4].v;
  15.                     hand=(byte)((handCompA==10)?10:9);
  16.                 }else if((this.c[0].v==14)&&(this.c[1].v==5)&&(this.c[2].v==4)&&(this.c[3].v==3)&&(this.c[4].v==2)){
  17.                     hand=9;
  18.                     handCompA=1;
  19.                 }
  20.             }else if(((this.c[1].v==this.c[0].v)&&(this.c[2].v==this.c[0].v)&&(this.c[3].v==this.c[0].v))||((this.c[2].v==this.c[1].v)&&(this.c[3].v==this.c[1].v)&&(this.c[4].v==this.c[1].v))){
  21.                 hand=8;
  22.                 handCompA=this.c[1].v;
  23.             }else if((this.c[1].v==this.c[0].v)&&(this.c[2].v==this.c[0].v)&&(this.c[3].v==this.c[4].v)){
  24.                 hand=7;
  25.                 handCompA=this.c[0].v;
  26.                 handCompB=this.c[3].v;
  27.             }else if((this.c[3].v==this.c[2].v)&&(this.c[4].v==this.c[2].v)&&(this.c[0].v==this.c[1].v)){
  28.                 hand=7;
  29.                 handCompB=this.c[0].v;
  30.                 handCompA=this.c[2].v;
  31.             }else if((this.c[1].s==this.c[0].s)&&(this.c[2].s==this.c[0].s)&&(this.c[3].s==this.c[0].s)&&(this.c[4].s==this.c[0].s)){
  32.                 hand=6;
  33.                 handCompA=0;
  34.             }else if((this.c[1].v==this.c[0].v-1)&&(this.c[2].v==this.c[0].v-2)&&(this.c[3].v==this.c[0].v-3)&&(this.c[4].v==this.c[0].v-4)){
  35.                 hand=5;
  36.                 handCompA=this.c[4].v;
  37.             }else if((this.c[0].v==14)&&(this.c[1].v==5)&&(this.c[2].v==4)&&(this.c[3].v==3)&&(this.c[4].v==2)){
  38.                 hand=9;
  39.                 handCompA=1;
  40.             }else if(((this.c[0].v==this.c[2].v)&&(this.c[1].v==this.c[2].v))||((this.c[1].v==this.c[2].v)&&(this.c[3].v==this.c[2].v))||((this.c[3].v==this.c[2].v)&&(this.c[4].v==this.c[2].v))){
  41.                 hand=4;
  42.                 handCompA=this.c[2].v;
  43.             }else if((this.c[0].v==this.c[1].v&&((this.c[2].v==this.c[3].v)||(this.c[3].v==this.c[4].v)))||((this.c[1].v==this.c[2].v)&&(this.c[3].v==this.c[4].v))){
  44.                 hand=3;
  45.                 handCompA=this.c[1].v;
  46.                 handCompB=this.c[3].v;
  47.                 if(handCompA<handCompB){
  48.                     byte temp=handCompA;
  49.                     handCompA=handCompB;
  50.                     handCompB=temp;
  51.                 }
  52.             }else if((this.c[0].v==this.c[1].v)||(this.c[2].v==this.c[1].v)){
  53.                 hand=1;
  54.                 handCompA=this.c[1].v;
  55.             }else if((this.c[4].v==this.c[3].v)||(this.c[2].v==this.c[3].v)){
  56.                 hand=1;
  57.                 handCompA=this.c[3].v;
  58.             }else{
  59.                 hand=1;
  60.                 handCompA=0;
  61.             }
  62.         }
  63.         public int compareTo(Object o){
  64.             Hand h=(Hand)o;
  65.             if(hand>h.hand){
  66.                 return 1;
  67.             }else if(hand<h.hand){
  68.                 return -1;
  69.             }else{
  70.                 int comp=0;
  71.                 if(handCompA<h.handCompA){
  72.                     comp=-1;
  73.                 }else if(handCompA>h.handCompA){
  74.                     comp=1;
  75.                 }
  76.                 if((comp==0)&&((hand==3)||(hand==7))){
  77.                     if(handCompB<h.handCompB){
  78.                         comp=-1;
  79.                     }else if(handCompB>h.handCompB){
  80.                         comp=1;
  81.                     }
  82.                 }
  83.                 if(comp!=0){
  84.                     return comp;
  85.                 }else{
  86.                     for(int i=0;i<5;i++){
  87.                         if(c[i].v>h.c[i].v){
  88.                             return 1;
  89.                         }else if(c[i].v<h.c[i].v){
  90.                             return -1;
  91.                         }
  92.                     }
  93.                     return 0;
  94.                 }
  95.             }
  96.         }
  97.         private static class Card implements Comparable{
  98.             byte v, s;
  99.             Card(String s){
  100.                 char vc=s.charAt(0);
  101.                 v=(byte)(vc>='2'&&vc<='9'?s.charAt(0)-'0':vc=='T'?10:vc=='J'?11:vc=='Q'?12:vc=='K'?13:14);
  102.                 this.s=(byte)(s.charAt(1)=='D'?0:s.charAt(1)=='H'?1:s.charAt(1)=='S'?2:3);
  103.             }
  104.             public int compareTo(Object o){
  105.                 Card c=(Card)o;
  106.                 if(v<c.v){
  107.                     return 1;
  108.                 }else if(v==c.v){
  109.                     return 0;
  110.                 }else{
  111.                     return -1;
  112.                 }
  113.             }
  114.         }
  115.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement