Browse Source

Making progress. Have laid out tests, poker hand class, comparators, and poker cards. Finished implementing several outcomes, have a few left to go.

master
Charles Reid 8 years ago
parent
commit
ce9f95df69
  1. 207
      054/Poker.java

207
054/Poker.java

@ -26,15 +26,212 @@ public class Poker { @@ -26,15 +26,212 @@ public class Poker {
}
class PokerHand {
String hand;
public PokerHand(String hand) {
this.hand = hand;
public static final String[] OUTCOMES = {"high","one","two","three","straight","flush","full house","four","straight flush","royal flush"};
public static final char[] CARDS = {'2','3','4','5','6','7','8','9','D','J','Q','K','A'};
public static final char[] SUITS = {'S','C','D','H'};
private class Cards {
char suit;
char value;
public Cards(char suit,char value) { this.suit=suit; this.value=value; }
public char getSuit() { return suit; }
public char getValue() { return value; }
}
///////////////////////////////
// Implement a poker hand
List<Cards> hand;
int nSuits;
int nValues;
int nPairs;
int nThrees;
public PokerHand(String handStr) {
hand = new LinkedList<Cards>();
String[] tokens = handStr.split(" ");
// assume exactly 5 tokens, otherwise you're a masochist
for(int i=0; i<tokens.length; i++) {
char suit = tokens[i].charAt(1);
char value = tokens[i].charAt(0);
hand.add( new Cards(suit, value) );
}
countSuits();
countValues();
}
private class SuitComparator implements Comparator<Cards> {
public int compare(Cards c1, Cards c2) {
int c1outcome = indexOf(SUITS,c1.suit);
int c2outcome = indexOf(SUITS,c1.suit);
return (c1outcome-c2outcome);
}
}
private class ValueComparator implements Comparator<Cards> {
public int compare(Cards c1, Cards c2) {
c1outcome = indexOf(VALUES,c1.value);
c2outcome = indexOf(VALUES,c1.value);
return (c1outcome-c2outcome);
}
}
protected void countSuits() {
this.nSuits = 0;
Set<Character> suits = new HashSet<Character>();
for(Cards c : hand) {
suits.add(c.getValue());
}
}
protected void countValues() {
this.nValues = 0;
Set<Character> values = new HashSet<Character>();
for(Cards c : hand) {
values.add(c.getValue());
}
}
protected void countPairs() {
nPairs = 0;
// Sort by face value
ValueComparator comp = new ValueComparator();
Collections.sort(hand, comp);
// Count pairs, not triples
for(int i=0; i+1<hand.size(); i++) {
if( hand.get(i).getValue()==hand.get(i+1).getValue() ) {
nPairs++;
try {
if(hand.get(i).getValue()==hand.get(i+2).getValue()) {
nPairs--;
}
} catch(IndexOutOfBoundsException e){
// its cool
}
// Skip new pair
i++;
}
}
}
protected void countTriplets() {
nThrees = 0;
// Sort by face value
ValueComparator comp = new ValueComparator();
Collections.sort(hand, comp);
// Count pairs, not triples
for(int i=0; i+2<hand.size(); i++) {
if( hand.get(i).getValue()==hand.get(i+1).getValue()
&& hand.get(i).getValue()==hand.get(i+2).getValue() ) {
nThrees++;
try {
if(hand.get(i).getValue()==hand.get(i+3).getValue()) {
nThrees--;
}
} catch(IndexOutOfBoundsException e){
// its cool
}
// Skip new triplet
i++;
i++;
}
}
}
public boolean hasRoyalFlush() {
// Sort by face value
ValueComparator comp = new ValueComparator();
Collections.sort(hand, comp);
// Each card should have same suit
char whichSuit = hand.get(0).getSuit();
// We need one of each
int na = 0, nk = 0, nq = 0, nj = 0, nd = 0;
for(Cards c : hand) {
if(c.getSuit()!=whichSuit) return false;
if(c.getValue()=='A') na++;
if(c.getValue()=='K') nk++;
if(c.getValue()=='Q') nq++;
if(c.getValue()=='J') nj++;
if(c.getValue()=='D') nd++;
}
if( na!=1 || nk!=1 || nq!=1 || nj!=1 || nd!=1 ) {
return false;
}
return true;
}
public boolean hasFour() { return (nValues==2 && nPairs==0); }
public boolean hasFullHouse() {
return false;
}
public boolean hasFlush() {
}
public boolean hasStraight() {
return false;
}
public boolean hasThree() { return nThrees>0; }
public boolean hasTwo() { return nPairs==2; }
public boolean hasOne() { return nPairs==1; }
public int getOutcome() {
// Check in order of rank:
// royal flush
if(hasRoyalFlush()) {
return indexOf(OUTCOMES,"royal flush");
} else if(hasFour()) {
return indexOf(OUTCOMES,"four");
} else if(hasFullHouse()) {
return indexOf(OUTCOMES,"full house");
} else if(hasFlush()) {
return indexOf(OUTCOMES,"flush");
} else if(hasStraight()) {
return indexOf(OUTCOMES,"straight");
} else if(hasThree()) {
return indexOf(OUTCOMES,"three");
} else if(hasTwo()) {
return indexOf(OUTCOMES,"two");
} else if(hasOne()) {
return indexOf(OUTCOMES,"one");
} else {
return indexOf(OUTCOMES,"high");
}
}
public int indexOf(String[] arr, String target) {
for(int i=0; i<arr.length; i++) {
if(arr[i].equals(target)) {
return i;
}
}
return -1;
}
}
class PokerCompare implements Comparator<PokerHand> {
public PokerCompare() {}
public int compare(PokerHand hand1, PokerHand hand2) {
// Need to implement special cases here
return 99;
int hand1outcome = hand1.getOutcome();
int hand2outcome = hand2.getOutcome();
if(hand1outcome==hand2outcome) {
// resolve by high card
System.out.println("meh");
} else {
return (hand1outcome-hand2outcome);
}
}
}

Loading…
Cancel
Save