243717594 poker casestudy-1-doc
-
Upload
homeworkping6 -
Category
Education
-
view
136 -
download
0
Transcript of 243717594 poker casestudy-1-doc
Get Homework/Assignment Done Homeworkping.comHomework Help https://www.homeworkping.com/
Research Paper helphttps://www.homeworkping.com/
Online Tutoringhttps://www.homeworkping.com/
click here for freelancing tutoring sitesCSC 120, CASE STUDY using enumerated data types:
Randomly generate 2 hands of poker (5 card draw) and determine which is the better hand.
Types of "5 card draw" poker hands : (Listed from best to worst)
Where A=ace, K=king, Q=queen, J=jack, H=hearts, D=diamonds, S=spades, and C=clubs. Ex: A H means Ace of Hearts.
Royal flush A,K,Q,J,10 of the same suit Ex: A,K,Q,J,10 of spades
Straight flush In sequence, same suit Ex: A H, K H, Q H, J H, 10 H
4 of a kind 4 with same face value Ex: 8 H, 8 S, 8 D, 8 C
Full house 3 with same face & 2 with other face value Ex: 3 10s, 2 Js
Flush Any 5 of same suit Ex: 3, 7, 9, J, and A of clubs
Straight 5 in a row, any suit Ex: 5 H, 6 D, 7 C, 8 H, 9 D
3 of a kind 3 with same face value Ex: 3 Qs
2 pair 2 with one face value; 2 with other face value Ex: 2 7s, 2 Js
1 pair 2 with same face value Ex: 2 Qs
High card (If none of the above, hand with highest card wins)
The High Level Design:
Where…GenHand (...) randomly generates a 5-card hand DisplayHands (...) prints out the 5 cards RankHand (...) determines the type of hand (eg 4-of-a-kind, full house, ...) (The hardest!) TellWinner (...) compares the two hand ranks, determines which is better, and prints an appropriate message
Evaluate Poker Hands
Shuffle Deck(deck)
GenerateHands DisplayHands Determine Hand Type Display The Better Hand
GenHand(deck, hand1)
GenHand(deck, hand2)
DisplayHand(hand1)
DisplayHand(hand2)
RankHand(hand1,h1Rank)
RankHand(hand2,h2Rank)
Tell Winner(h1Rank,h2Rank)
Pseudocode for generating a hand of cards:
counter <-- 0 Loop til you have 5 cards: Randomly gen a location in the deck of 52 sorted cards If not already taken Put it in the hand Increment counterExample of using the C++ random(n) function which returns a random number between from 0 to (n-1), inclusive. location <-- rand (CARDSINDECK) Reminder: After the program has been thoroughly tested, seed the random number generator to ensure that you will get different cards every time.
A "hand" will be an array of 5 records. Each record has 2 fields: a face value (faceValue) and a suit (suit). For readability, enumerated types will be used for the face values and suits. This will avoid having to "code" the cards.
enum SuitType { hearts=1, diamonds, spades, clubs }; enum FaceValType { two=2, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace }; enum Ranking { highcard=1, one_pair, two_pair, threekind, straight, flush, fullhouse, fourkind, stflush, royalflush};
Reminder: You cannot read/write enumerated values in C++. You must use selection (typically a switch statement). The default ordering for an enumerated type is 0, 1, 2, etc. but in our case study, we'll override the defaults. Notice for the FaceValType how the "two" gets the order 2. This forces the other orders (called "ordinals") to fall in place according to the face value of the cards.
Here is the C++ code for writing out the suit of a card. Let's assume that the card in question is hand[3].suit, which has the value clubs.
switch(hand[ctr].suit) { case hearts: cout << "heart"; break; case diamonds: cout << "diamond"; break; case spades: cout << "spade"; break; case clubs: cout << "club"; break; default: cout << "Invalid suit!"; }
Another advantage of using enumerated types is that they can be easily assigned and compared (using >, <, ==).
For example, suppose two cards in different hands have the same suit but you want to see which has the higher face value: if (hand1[ctr].faceValue > hand2[ctr].faceValue) ... works just fine!
Here are the constant and structure declarations:
const CARDSINHAND = 5; const CARDSINDECK = 52; struct CardType { SuitType suit; FaceValType faceValue; };
struct DeckCardType { CardType card; bool taken; // initialized to false; set to true when that card is chosen }; // so that same card won't be drawn twice
typedef DeckCardType DeckType[CARDSINDECK]; typedef CardType HandType[CARDSINHAND]; ... HandType hand1, hand2; DeckType deck; ...
Let's look at one way to set up the deck of cards:
cardCounter=0; for (int suitCtr=hearts; suitCtr<=clubs; suitCtr++) for (int faceValCtr=two; faceValCtr<=ace; faceValCtr++); { Deck[cardCounter].card.suit = suitCtr; Deck[cardCounter].card.faceValue = faceValCtr; Deck[cardCounter].taken = false; cardCounter++; }
The deck now looks like this:
(suit) (faceValue) (taken) 0 hearts two false (reps the 2 of hearts) 1 hearts three false 2 hearts four false 3 hearts five false 4 hearts six false ... 12 hearts ace false 13 diamonds two false 14 diamonds three false ... 51 clubs ace false
To randomly generate a hand of cards:
Assume that the random number generator has been seeded prior to this:
for (int ctr=1; ctr <=CARDSINHAND; ctr++) { // keep randomly generating next card til you get one that is not taken do { location = rand(CARDSINDECK); } while (Deck[location].drawn == true)
// you got a card so mark it as taken Deck[location].taken = true; hand[ctr].faceValue = deck[location].card.faceValue; hand[ctr].suit = deck[location].card.suit; }
Note re representing the deck of cards: There is a better scheme than this one, but this one is simple and it shows how you can store a deck of cards as an array of records. The other scheme doesn't need the "taken" field. Rather, when it selects a card, it replaces that card in the deck with the last card in the deck and decrements the number of cards so that when random() is called it is called with the current number of cards left in the deck, as in ... random(numCardsInDeck). This is actually more efficient because, as long as there is at least one card in the deck, you'll get a card on the first try.
Now let's assume the existence of the following functions to help us determine the ranking for a hand of cards (the hardest part of this program):
bool SameSuit (HandType hand) Returns true if all five cards have same suit bool InSequence (HandType hand) Returns true if all five cards are in numeric sequence by face value, as in 3-4-5-6-7 or 9-10-J-Q-K bool RoyalFlush (HandType hand) Returns true if the hand is a royal flush bool StFlush (HandType hand) Returns true if the hand is a straight flush
AND SO ON… WITH A FUNCTION FOR EACH TYPE OF HAND !
Here's a possible design for the RankHand procedure:
Name RankHand PreCondition A hand of five cards has been drawn from the deck PostCondition The hand has been given a ranking and the ranking has been passed out Data passed in: hand HandType An array of 5 cards Data passed out: rank Ranking The type of hand (ex royalflush, threeofkind, etc) Local vars: ... Algorithm:
if (RoyalFlush(hand)) ranking <-- royalflush else if (StraightFlush(hand)) rank <-- stflush else ... and so on...
Pseudocode algorithm for RoyalFlush function, where Sort is a procedure that sorts the hand in descending order by face value:
Sort(hand) if hand[0].faceValue = ace and InSequence(hand) and SameSuit(hand) // note user of Boolean functions here return true else return false // note: should be programmed with a single return but this is more readable for this case study
Pseudocde algorithm for StraightFlush function:
Same as the above function except that you don't need to check to see if ace is the high card.
Pseudocode algorithm for FourOfAKind function:
Call Sort (hand) if (hand[1].faceValue = hand[2].faceValue and hand[2].faceValue = hand[3].faceValue) and (hand[0].faceValue = hand[1].faceValue or hand[3].faceValue=hand[4].faceValue) return true else return false
Similar functions would take advantage of the Sort(), InSequence(), and SameSuit() subroutines.
To be efficient, the function to check for a full house (3 of one face value, 2 of another face value) could call the function ThreeOfAKind and the function Pair to avoid redundant code.
To display a hand?
See the C++ program CardsEDT.CPP . This program is available to you on the course Web site. Download it and run it. It tests the creation of a deck of cards and then displays the cards in the deck in two different ways: printing out numbers for the face values 2 to 10, printing out words for the face values 2 to 10.