university of nebraska at omaha

CIST 1400 (Section 401)

Introduction to Computer Programming

Summer 2001 (Second Day Session)

 

Programming Assignment 5

 

Due: Wednesday, August 8, 2001

Introduction

The player with the highest-ranking hand wins the card game of poker.  Each hand has five cards.  Each card has a suit (one of clubs, diamonds, hearts and spades) and a rank (one of 2, 3, 4, … , 10, jack, queen, king, ace).

In this problem, the input data will contain specification of the five cards in a poker hand, and your program is to determine the rank associated with the hand.

The specification of each card is given by two characters entered without any separation between them.  The first character of a pair indicates the rank of the card.  For the ranks of 2 through 9, the characters ‘2’ through ‘9’ are used.  For 10, jack, queen, king, and ace, the characters ‘T’, ‘J’, ‘Q’, ‘K’, and ‘A’ are used.  The second character of the pair indicates the suit of the card.  ‘C’ is used for clubs, ‘D’ for diamonds, ‘H’ for hearts, and ‘S’ for spades.  Thus the pair AH specifies the ace of hearts, the pair 2C specifies the two of clubs, and the pair TD specifies the ten of diamonds.

Each poker hand has one of the ranks described below.  These are given in order from the lowest-ranking hand to the highest-ranking hand.

High card.  If none of the other cases apply, the hand is ranked by the highest ranked card in the hand.  Example: 6S, 8D, 9D, JH, KS.

One pair.  A hand with two cards of the same rank.  Example: 6S, 6D, 3C, TC, AS.

Two pair.  A hand with two pairs of cards of the same rank.  Example: 2C, 2D, 6H, 6S, AD.

Three of a kind.  A hand containing three cards with the same rank.  Example: 2S, 2A, 2D, 4C, 4H.

Straight.  A hand containing five cards in sequence.  Example:  8S, 9C, TH, JC, QD.

Flush.  A hand in which all five cards have the same suit.  Example:  2C, 5C, 7C, 8C, AC.

Full house.  A hand containing three cards of the same rank, and the remaining two cards with the same rank.  Example: 2C, 2H, 2D, 7H, 7S.

Four of a kind.  A hand containing four cards of the same rank.  Example: 7D, 7H, 7C, 7S. AD.

Straight flush.  A hand containing a straight in which each of the cards is of the same suit.  Example: 6D, 7D, 8D, 9D, TD.

The Problem

Write a program that obtains from the user the specification of the five cards in a poker hand, then determines and displays the rank associated with that poker hand.  An acceptable format for the input and output is shown in the sample execution (below).

Solution Notes

  1. You may think it possible to rank one hand in several ways.  For example, the hand containing 2H 2C 2S 3D 3C could conceivably be ranked as one pair (since it does, indeed, contain at least one pair), or two pair, or three of a kind, or a full house.  In poker, a hand always has the highest possible rank, regardless of any other ranks it might conceivably exhibit.  What does this tell you about the order in which your program should check the hand for the various ranks of hands?
  2. As usual, you are urged to solve the problem by developing multiple refinements of the pseudocode for the problem, each refinement consisting of more steps, each of which is a smaller problem than those in the previous refinement.  For example, the problem statement above gives a reasonable statement of the first-level pseudocode, and includes three separate and distinct subproblems: (1) obtain specification of the five cards, (2) determine the rank associated with that set of five cards, and (3) display the result.  The second subproblem could be refined by considering pseudocode statements like this: determine if all the cards in the hand have the same suit, or like this: determine if the cards in the hand, suitably ordered, form a sequence (e.g. 2, 3, 4, 5, 6).  Eventually, this pseudocode would result in a main function that invokes three functions: obtain input, determine rank, display result.  The function to determine rank would use multiple functions, each used to determine if the hand is of a particular rank.
  3. Before writing the first line of your program, make certain you have precisely defined how you will represent the cards.  You could obviously use two separate variables to represent each card, one containing an indication of the rank, and another containing an indication of the suit.  Or you might choose to use a single variable that combines the rank and suit information.  For example, a three-digit decimal number could be used to represent a card, with the first digit representing the suit, and the remaining two digits representing the suit.  Then you could use simple functions to extract the rank or suit information from a single number.
  4. For clarity, the executable form of the instructor’s solution to this problem will be available in the file /u1/stanw/prog5 no later than Friday, August 3.

Sample Execution

The input and output from several typical executions of the program is shown below.  Make certain you test your program with various input data to ensure you have correctly solved the problem.

Enter the specification of the five cards in the hand: AS 2D AC 2H 2C

Hand rank: full house

Enter the specification of the five cards in the hand: 2H 3H 7H 9H AH

Hand rank: flush

Enter the specification of the five cards in the hand: 4D 7D 9H 4S 7C

Hand rank: two pair