// =============================================================================
// PokerHand.java
// 2016-Oct-02
// Prof. Scott F. H. Kaplan -- sfkaplan@cs.amherst.edu
/**
* A PokerHand
can hold any collection of Card
s.
*/
public class PokerHand {
// =============================================================================
// =========================================================================
// DATA MEMBERS.
/** The internal PokerHand
storage. */
private Card[] _cards;
// =========================================================================
// =========================================================================
/**
* Default constructor. Creates an empty PokerHand
.
*/
public PokerHand () {
_cards = new Card[0];
}
// =========================================================================
// =========================================================================
/**
* Copy constructor.
*
* @param other The PokerHand
of which to make a copy.
*/
public PokerHand (PokerHand other) {
// Duplicate the array.
_cards = Arrays.copyOf(other._cards, other._cards.length);
}
// =========================================================================
// =========================================================================
/**
* Specialized constructor.
*
* @param deck The Deck
from which to draw the cards
* and fill the hand.
* @param count The number of Card
s to draw into the hand.
*/
public PokerHand (Deck deck, int count) {
_cards = new Card[count];
for (int i = 0; i < count; i += 1) {
_cards[i] = deck.draw();
}
}
// =========================================================================
// =========================================================================
/**
* Does this hand contain a pair?
*/
public boolean hasPairs () {
}
// =========================================================================
// =========================================================================
/**
* Does this hand contain three of a kind?
*/
public boolean hasThreeOfAKind () {
}
// =========================================================================
// =========================================================================
/**
* Does this hand contain four of a kind?
*/
public boolean hasFourOfAKind () {
}
// =========================================================================
// =========================================================================
/**
* Does this hand contain a full house?
*/
public boolean hasFullHouse () {
}
// =========================================================================
// =========================================================================
/**
* Does this hand contain a straight?
*/
public boolean hasStraight () {
}
// =========================================================================
// =========================================================================
/**
* Does this hand contain a flush?
*/
public boolean hasFlush () {
}
// =========================================================================
// =========================================================================
/**
* Does this hand contain a straight flush?
*/
public boolean hasStraightFlush () {
}
// =========================================================================
// =========================================================================
/**
* Does this hand contain a royal flush?
*/
public boolean hasRoyalFlush () {
}
// =========================================================================
// =========================================================================
/**
* Running this class will yield a test of the ability to create
* poker hands and determine their content.
*/
public static void main (String[] args) {
// Check and extract the command line argument.
if (args.length != 1) {
showUsageAndExit();
}
int numHands;
try {
numHands = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
showUsageAndExit();
}
if (numHands < 0) {
showUsageAndExit();
}
// Create the requested number of hands and check them.
int numPairs = 0;
int numThreeOfAKind = 0;
int numFourOfAKind = 0;
int numFullHouse = 0;
int numStraight = 0;
int numFlush = 0;
int numStraightFlush = 0;
int numRoyalFlush = 0;
int cardsPerHand = 5;
Deck deck = new Deck();
deck.shuffle();
for (int handNum = 0; handNum < numHands; handNum += 1) {
if (deck.getCardsRemaining() < cardsPerHand) {
deck.refill();
deck.shuffle();
}
PokerHand hand = new PokerHand(deck, cardsPerHand);
if (hand.hasPair()) {
numPairs += 1;
}
if (hand.hasThreeOfAKind()) {
numThreeOfAKind += 1;
}
if (hand.hasFourOfAKind()) {
numFourOfAKind += 1;
}
if (hand.hasFullHouse()) {
numFullHouse += 1;
}
if (hand.hasStraight()) {
numStraight += 1;
}
if (hand.hasFlush()) {
numFlush += 1;
}
if (hand.hasStraightFlush()) {
numStraightFlush += 1;
}
if (hand.hasRoyalFlush()) {
numRoyalFlush += 1;
}
}
// Show the results.
System.out.println("pairs:\t\t" + numPairs);
System.out.println("3 of a kind:\t" + numThreeOfAKind);
System.out.println("4 of a kind:\t" + numFourOfAKind);
System.out.println("full house:\t" + numFullHouse);
System.out.println("straight:\t" + numStraight);
System.out.println("flush:\t\t" + numFlush);
System.out.println("straight flush:\t" + numStraightFlush);
System.out.println("royal flush:\t" + numRoyalFlush);
}
// =========================================================================
// =========================================================================
private static void showUsageAndExit () {
System.err.println("USAGE: java PokerHand ");
System.exit(1);
}
// =========================================================================
// =============================================================================
} // class PokerHand
// =============================================================================