# CS 340 - Machine Problem 2

## Card Game: Poker - 5 Card Draw

Due: 11:59 pm on Monday 10/2/2006

For this assignment you must work alone.

For this assignment, you are to implement the card game of Five Card Draw in Poker. The Five Card Draw explains the order in which cards are given to the players and Poker determines the "value" of each hand.

The game is played using a standard deck of 52 cards. Each card has a Rank and Suit value. There are 13 Ranks and 4 Suits making the 52 cards in the deck. The Suits are colored either Black or Red. The cards are to be listed using a two character sequence for each card. The first character is to specify the rank of the card and the second character is to specify the suit. The ranks and suits with the associated characters are given in the table below. Use upper case letters when specifying cards.

Rank Characters Suit Characters
• 2 - Two
• 3 - Three
• 4 - Four
• 5 - Five
• 6 - Six
• 7 - Seven
• 8 - Eight
• 9 - Nine
• T - Ten
• J - Jack
• Q - Queen
• K - King
• A - Ace
• C - Clubs (Black)
• D - Diamonds (Red)
• H - Hearts (Red)
Note that the ranks and suits are listed in order with Two being the low rank, Ace being the high rank, Clubs being the low suit and Spades being the high suit. In the game of Poker, an Ace can being either low or high. It should be treated as a high rank except in the case of a "straight" (to be explained below). The rank of a card has higher priority than the suit of the card. The suit is used when comparing two card only if the ranks are the same. Thus:
```     7C > 5S   and    8H > 8D
```
According to the wikipedia, the suits don't impact the game of poker except when determining a flush. We will use the rules described at its Rank of Hands page as our official rules.

### The Game's Idea

In the game of five card draw the following actions occur.
1. The 52 cards in the deck are shuffled (randomized) and put in the "draw pile".
2. Then each player is given one card in some determined order. This is repeated (using the same order of players) until each player has 5 cards. This is card "dealing" the cards"
3. The first player is allowed to discard up to 3 cards from their hand (the "first" player is player at the beginning of the order from step 2) The player can discard a 4th card only if the remaining card in the player's hand is an Ace. The cards that are discarded are put into a "discard pile" which is separate from the "draw pile". The player then given enough cards from the "draw pile" so that the player will have 5 cards. Following the same order that was used in dealing the cards, this is repeated for the other players.
4. Now the hands are evaluated and the winner is determined.

### The Evaluation of Hands

Refer to wikipedia's page on poker's rank of hands for our official page on this. It has a great way to determine how a determine ties and how to dtermine winners when things appear the same. The evaluation of hands from highest to lowest is:
• Straight Flush - All five cards are in sequence and of the same suit. An Ace can be either the high card (AS KS QS JS TS) or the low card (5H 4H 3H 2H AH). When comparing two Straight Flushes, the two high cards are compared. If these are the same, there is a tie.
• Four of a Kind - Four of the five cards have the same rank. When comparing, the rank of the matched cards is used to determine the winner.
• Full House - Three of the cards have one rank and two of the cards have another rank. When comparing, the rank of the three matched cards is used to determine the winner.
• Flush - All five cards have the same suit. When comparing two flushes, the highest ranking card of each hand is used. If there is a tie, compare the next highest ranking card.
• Straight - All five card are in sequence. An Ace can be either the high card (AS KD QS JH TC) or the low card (5S 4D 3H 2C AS). When comparing the straights the high card from each straight is compared. If these are the same, there is a tie.
• Three of a Kind - Three of the five cards have the same rank. When comparing, the rank of the matched cards is used to determine the winner.
• Two Pair - Two cards in the hand contain one rank and a different two cards in the hand contain another rank. When comparing, the ranks of the higher ranked pair are compared first. If those are the same, then the ranks of the lower ranked pair are compared. If those are also the same, the ranks of the remaining card are compared. If these are the same, there is a tie.
• One Pair or Two of a Kind - Two cards in the hand contain the same rank. When comparing, the rank of the matched cards are compared first. If there is a match, the non-paired cards are compared to determine the winner. If the non-paired cards contain the same ranks, there is a tie.
• High Card - When no cards have the same rank, nor are the five cards in sequence nor are the five cards of the same suit, the rank of the highest card is used to determine the winner. If those are the same the ranks of the next highest cards are compared. If the ranks are all the same, there is a tie.

### The Prompt for the User Player

Your program must give some messages and prompt as the game is played.
1. The first prompt given to the user is the number of computer opponents. Your program must allow answers from 1 to 3. You can allow less computer oppenents (i.e. zero) or more as you wish, but your program must accept the values 1, 2 and 3.

2. The program should then give some messages about the play of the game (i.e. "the desk is being shuffled", "the cards are being delt to 3 players").

3. The game is next in the "discard and draw" phase. When its is a computer opponent's turn to discard and draw, your program must display a message something like:
```     Computer Player 1 has discarded 3 cards.
```
When it is the user player's turn, you must show the five cards in the players hand and allow the user the select which cards will be discarded. Perhaps there is a prompt such as:
```     The cards in your hand are: 1) AS  2) KH  3) TD  4) TC  5) 6S

Since you have an Ace you can keep the Ace and discard the other
four cards.
List the cards numbers you wish to discard. >
```
4. Now show all of the hands, specify what type of hand it is (i.e. One Pair, Three of a Kind, etc) and list the winner.

5. Then thank the player for using the program and list whatever information you wish about the program. Finally end the program.
When showing the cards in a hand, sort the cards from highest to lowest with the highest card being listed first (on the left).

For 5 points extra credit, sort the cards so that any matched cards are list first. When multiple matched cards are in the hand (i.e. two pair or full house), list the more important match first. This would be the match with the higher rank for two pair and the match of three for the full house. Doing this will make breaking ties extremely easy. So you are strongly encouraged to do this. Not only will you get extra credit, but this will simplify a potentially difficult part of the assignment.

### The Computer Player's AI Algorithm

For the computer player's turn, use the following AI algorithm.
1. First check if the computer player already has a hand of One Pair or better. If so, discard all other card.
2. If the hand evaluates to "High Card", determine if the user has 4 cards of the same suit. If so, discard the card of the different suit.
3. Next determine if the user has 4 cards in sequence. of the same suit. If so, discard the card that is out of sequence.
4. Next if the user has an Ace, discard the other four cards.
5. Otherwise, keep the two highest cards and discard the other 3.

### Class Requirements

You must write at least 5 classes for this program. These classes are:
1. A Card Class - This class will keep the information about each card.
2. A Card Pile Class - This class is a collection of cards. While this class cannot just be an STL class. You may use an STL class instance as a data member of the Card Pile Class.
3. A User Player Class - This class will keep track of the user's hand and interact with the user interface.
4. An Opponent Player Class - This class will keep track of the computer player's hand and interact with the Artificial Intelligence.
5. A Game Class - This class will keep track of the particulars of the game, such as the order of actions of the game (shuffling, dealing, discarding and determining the winner) and the evaluation of the hand (does the player have a Straight Flush, Four of a Kind, Full House, etc.). Actually it might be better to divide this into two classes: one to deal with the order of the actions and one to evaluated the hands.

``` turnin -c cs340 -p mp2 <filename(s)>
``` turnin -c cs340 -p mp2 -v