Skip to content

Commit

Permalink
Finished hand detection code. We can now determine the winner so long…
Browse files Browse the repository at this point in the history
… as two players do not have the same type of hand. Next step is to score hands so that we can resolve ties
  • Loading branch information
MusikPolice committed Oct 4, 2012
1 parent 340be74 commit 0d60388
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 32 deletions.
52 changes: 29 additions & 23 deletions src/pokerhands/Hand.java
Expand Up @@ -169,7 +169,7 @@ public boolean hasOnePair()
//if any of the ranks have two or more cards, then there is one pair.
for (int r = 1; r < 14; r++)
{
if (rankCount.get(r) >= 2) return true;
if (rankCount.get(r) != null && rankCount.get(r) >= 2) return true;
}

return false;
Expand All @@ -195,19 +195,24 @@ public boolean hasTwoPair()

int pairs = 0;

//if any of the ranks have four or more cards, then there is a four of
//a kind in the hand.
//if any of the ranks have two or more cards, then there is a pair in the hand.
for (int r = 1; r < 14; r++)
{
if (rankCount.get(r) >= 2)
if (rankCount.get(r) != null && rankCount.get(r) >= 2)
{
pairs++;
}
}

return pairs > 0;
return pairs >= 2;
}

/**
* Returns true if the hand contains three of a kind.
* Three of a kind is defined as three cards with the same rank. Suit is not
* considered for this comparison.
* @return true if the hand contains three of a kind.
*/
public boolean hasThreeOfAKind()
{
if (cards.size() < 5) return false;
Expand All @@ -226,7 +231,7 @@ public boolean hasThreeOfAKind()
//of a kind in the hand.
for (int r = 1; r < 14; r++)
{
if (rankCount.get(r) >= 3) return true;
if (rankCount.get(r) != null && rankCount.get(r) >= 3) return true;
}

return false;
Expand Down Expand Up @@ -263,7 +268,7 @@ public boolean hasFlush()
suitCount.put(c.getSuit(), count + 1);
}

//if any of the suits have more than four cards, then there is a flush
//if any of the suits have more than five cards, then there is a flush
//of that suit in the hand.
for (Card.SUIT s : suitCount.keySet())
{
Expand Down Expand Up @@ -300,11 +305,11 @@ public boolean hasFullHouse()
//a kind in the hand.
for (int r = 1; r < 14; r++)
{
if (rankCount.get(r) == 2)
if (rankCount.get(r) != null && rankCount.get(r) == 2)
{
pairRank = r;
}
else if (rankCount.get(r) == 3)
else if (rankCount.get(r) != null && rankCount.get(r) == 3)
{
tripsRank = r;
}
Expand Down Expand Up @@ -337,7 +342,7 @@ public boolean hasFourOfAKind()
//a kind in the hand.
for (int r = 1; r < 14; r++)
{
if (rankCount.get(r) >= 4) return true;
if (rankCount.get(r) != null && rankCount.get(r) >= 4) return true;
}

return false;
Expand Down Expand Up @@ -390,7 +395,7 @@ public boolean hasRoyalFlush()
* Assumes that h has been sorted from lowest rank to highest, regardless of suit
* @param h the hand to search for a straight
* @return the highest straight in h, or null if no straight could be found
*/
*
private Hand getHighestStraight()
{
List<Hand> straights = getStraights();
Expand All @@ -404,6 +409,7 @@ private Hand getHighestStraight()
return null;
}
}
*/

/**
* Returns a list of all sub-hands of this hand that constitute a five
Expand Down Expand Up @@ -431,7 +437,18 @@ private List<Hand> getStraights()
//the previous card, it is part of the straight
straight.add(c);
}
else if (c == cards.get(cards.size() - 1))
else
{
//the straight was broken. split it into five card runs
//and add each of them to our collection of straights.
straights.addAll(getSubRuns(straight, 5));

//start fresh with just the current card
straight = new ArrayList<>();
straight.add(c);
}

if (c == cards.get(cards.size() - 1))
{
//if the highest card in the hand is a king
if (c.getRank() == 13)
Expand All @@ -441,21 +458,10 @@ else if (c == cards.get(cards.size() - 1))
{
//then the both the king and the ace count
//towards the straight
straight.add(c);
straight.add(cards.get(0));
}
}
}
else
{
//the straight was broken. split it into five card runs
//and add each of them to our collection of straights.
straights.addAll(getSubRuns(straight, 5));

//start fresh with just the current card
straight = new ArrayList<>();
straight.add(c);
}
}
else
{
Expand Down
89 changes: 80 additions & 9 deletions src/pokerhands/Pokerhands.java
@@ -1,8 +1,5 @@
package pokerhands;

import java.util.ArrayList;
import java.util.List;

/**
*
* @author jfritz
Expand Down Expand Up @@ -46,15 +43,89 @@ public static void main(String[] args)
System.out.println(community.toString());

Hand p1AllCards = new Hand(player1, community);
if (p1AllCards.hasOnePair())
{
System.out.println("Player1 has a pair");
}
System.out.print("Player one has a ");
int p1Score = scoreHand(p1AllCards);
System.out.println();

Hand p2AllCards = new Hand(player2, community);
if (p2AllCards.hasOnePair())
System.out.print("Player two has a ");
int p2Score = scoreHand(p2AllCards);
System.out.println();

if (p1Score > p2Score)
{
System.out.println("Player 1 Wins!");
}
else if (p1Score < p2Score)
{
System.out.println("Player 2 Wins!");
}
else
{
System.out.println("Temporary tie");
}
}

/**
* Checks the contents of the specified hand for winning combinations.
* Returns an integer between 0 and 9 (inclusive) that indicates the score
* of the hand, where 0 is high card and 9 is royal flush.
* This is the first step of determining the winning hand. If two hands have
* the same score, then they need to be compared in more detail.
* @param h the hand to score
* @return the score of the hand, an integer from 0 to 9
*/
private static int scoreHand(Hand h)
{
if (h.hasRoyalFlush())
{
System.out.print("Royal Flush");
return 9;
}
else if (h.hasStraightFlush())
{
System.out.print("Straight Flush");
return 8;
}
else if (h.hasFourOfAKind())
{
System.out.print("Four of a Kind");
return 7;
}
else if (h.hasFullHouse())
{
System.out.print("Full House");
return 6;
}
else if (h.hasFlush())
{
System.out.print("Flush");
return 5;
}
else if (h.hasStraight())
{
System.out.print("Straight");
return 4;
}
else if (h.hasThreeOfAKind())
{
System.out.print("Three of a Kind");
return 3;
}
else if (h.hasTwoPair())
{
System.out.print("Two Pair");
return 2;
}
else if (h.hasOnePair())
{
System.out.print("One Pair");
return 1;
}
else
{
System.out.println("Player2 has a pair");
System.out.print("High Card");
return 0;
}
}
}

0 comments on commit 0d60388

Please sign in to comment.