Documente Academic
Documente Profesional
Documente Cultură
Homeworkping.com
Homework Help
https://www.homeworkping.com/
Research Paper help
https://www.homeworkping.com/
Online Tutoring
https://www.homeworkping.com/
click here for freelancing tutoring sites
CSC 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
Straight flush
4 of a kind
Full house
Flush
Straight
3 of a kind
2 pair
1 pair
High card
Shuffle
Deck
(deck)
GenHand
(deck,
hand1)
GenerateHands
GenHand
(deck,
hand2)
DisplayHands
DisplayHand
(hand1)
DisplayHand
(hand2)
RankHand
(hand1,
h1Rank)
RankHand
(hand2,
h2Rank)
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
Tell Winner
(h1Rank,
h2Rank)
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:
0
1
2
3
4
...
12
13
14
...
51
ace
false