Question

******ONLY C++****** Objectives: • Appropriate use of arrays • Use of Sorting functions • Analysis of...


******ONLY C++******

Objectives:
• Appropriate use of arrays
• Use of Sorting functions
• Analysis of data using conditional structures
• Organize code into usable functions
Problem:
Poker is an easy game to learn and a hard game to win. Write a program that will generate five card hands from a shuffled deck and then keep track of what type of hand it is, providing a count and a percentage at the end.
Details:
​Program Flow:
1. Ask the user for how many hands to generate
2. Shuffle the “deck”
3. Select a 5-card hand from the top of the deck
4. Evaluate hand for ranking in poker
5. Store type of hand in an array
6. Return to step 2 until all hands have been generated
7. Print a formatted list of how many hands fall into each ranking and the percentage of total hands in each ranking
8. End program
Functions:
1. Shuffle – inputs an integer array and a number, swaps random elements of the array that number of times
2. Separate – inputs an array of integers and an empty 5-element array of integers, selects the first 5 elements of the first array, and fills the empty array with copies of those elements
3. Sort – inputs an array of integers, and sorts the array in place
4. Select – inputs an array of 5 integers, returns an integer representing the type of hand
Representation:
1. All cards will be represented by integers (0 – 51)
2. Card rank (A-2-3-4-5-6-7-8-9-10-J-Q-K) can be calculated by x/4 + 1
3. Card suit can be calculated by x%4 (♠=0,♥=1,♦=2,♣=3)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include <bits/stdc++.h>
using namespace std;

struct card{
int val;
int col;
int shuffleVal;
};

bool shuffleFunc(card a, card b){
return a.shuffleVal < b.shuffleVal;
}

bool sortFunc(card a, card b){
return a.val < b.val;
}

bool isFlash(vector<card> v){
return (v[0].col == v[1].col) && (v[0].col == v[2].col) &&
(v[0].col == v[3].col) && (v[0].col == v[4].col);
}

bool isStraight(vector<card> v){
return (v[0].val+1 == v[1].val) && (v[1].val+1 == v[2].val) &&
(v[2].val+1 == v[3].val) && (v[3].val+1 == v[4].val);
}

int evaluate(vector<card> h) {
sort(h.begin(),h.end(),sortFunc);
if(isFlash(h) && isStraight(h))
return 1;
if((h[0].val == h[1].val || h[4].val == h[3].val) && h[1].val == h[2].val && h[3].val == h[2].val)
return 2;
if((h[0].val==h[1].val && h[1].val==h[2].val && h[3].val==h[4].val)
|| (h[0].val==h[1].val && h[2].val==h[3].val && h[3].val==h[4].val))
return 3;
if(isFlash(h))
return 4;
if(isStraight(h))
return 5;
if((h[0].val==h[1].val && h[1].val==h[2].val) ||
(h[1].val==h[2].val && h[2].val==h[3].val) ||
(h[2].val==h[3].val && h[3].val==h[4].val))
return 6;
if((h[0].val==h[1].val && h[2].val==h[3].val) ||
(h[0].val==h[1].val && h[3].val==h[4].val) ||
(h[1].val==h[2].val && h[3].val==h[4].val))
return 7;
if((h[0].val==h[1].val || h[1].val==h[2].val) ||
(h[2].val==h[3].val || h[3].val==h[4].val))
return 8;
return 9;
}

int main() {
vector<card> deck;
for(int i = 1; i <= 13; i++) {
card c;
c.col = 0;
c.val = i;
deck.push_back(c);
}
for(int i = 1; i <= 13; i++) {
card c;
c.col = 1;
c.val = i;
deck.push_back(c);
}
for(int i = 1; i <= 13; i++) {
card c;
c.col = 2;
c.val = i;
deck.push_back(c);
}
for(int i = 1; i <= 13; i++) {
card c;
c.col = 3;
c.val = i;
deck.push_back(c);
}


// 1. Ask for number of hands
int n;
cin >> n;

vector<int> handRank(10,0);

for(int i = 1; i <= n; i++) {
// 2. Shuffle
for(int i = 0; i < 52; i++)
deck[i].shuffleVal = rand()%1000;
sort(deck.begin(),deck.end(),shuffleFunc);

// 3. Get 5-card hand
vector<card> hand(5);
for(int i = 0; i < 5; i++)
hand[i] = deck[i];

// 4. evaluate hand
int rnk = evaluate(hand);

// 5. store type
handRank[rnk]++;
}

// 7. print rank info
for(int i = 0; i <= 9; i++) {
cout<<"Rank #"<<i<<": "<<handRank[i]<<" - "<<(double)((double)handRank[i]/n*100.0)<<"%\n";
}

return 0;
}


Output:

D:\!1Programming\main\bin\Debug\main.exe 10000 Rank #0: 0 - 0% er Rank #1: 0 - 0% Rank #2: 1 - 0.01% eRank #3: 12 - 0.12% KeR

Add a comment
Know the answer?
Add Answer to:
******ONLY C++****** Objectives: • Appropriate use of arrays • Use of Sorting functions • Analysis of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • We are playing with a smaller deck of cards which is: A♠, 2♠, 3♠, 4♠ (Spades)...

    We are playing with a smaller deck of cards which is: A♠, 2♠, 3♠, 4♠ (Spades) A♣, 2♣, 3♣, 4♣ (Clubs) A♥, 2♥, 3♥, 4♥ (Hearts) A♦, 2♦, 3♦, 4♦ (Diamonds) Let “S” be the event the card drawn is a spade Let “C” be the event that the card draw is a club Let “4” be the event the card is a 4 Using this information answer the questions below (9 points): 1. P (S) = 2. P (C)...

  • C++ Purpose: To practice arrays of records, and stepwise program development. 1. Define a record data...

    C++ Purpose: To practice arrays of records, and stepwise program development. 1. Define a record data type for a single playing card. A playing card has a suit ('H','D','S',or 'C'), and a value (1 through 13). Your record data type should have two fields: a suit field of type char, and a value field of type int. 2. In main(), declare an array with space for 52 records, representing a deck of playing cards. 3. Define a function called initialize()...

  • the card game Acey Deucey, which is also known by several other names. In general, the...

    the card game Acey Deucey, which is also known by several other names. In general, the game is played with three or more people that continue to gamble for a pot of money. The pot grows and shrinks depending on how much General description of the game ● Two cards are dealt face up to a player from a shuffled deck of cards. ○ If the face of each card is the same then the player adds $1 into the...

  • C Programming The following code creates a deck of cards, shuffles it, and deals to players....

    C Programming The following code creates a deck of cards, shuffles it, and deals to players. This program receives command line input [1-13] for number of players and command line input [1-13] for number of cards. Please modify this code to deal cards in a poker game. Command line input must accept [2-10] players and [5] cards only per player. Please validate input. Then, display the sorted hands, and then display the sorted hands - labeling each hand with its...

  • *in Java 1. Create a card class with two attributes, suit and rank. 2.In the card...

    *in Java 1. Create a card class with two attributes, suit and rank. 2.In the card class, create several methods: a. createDeck – input what type of deck (bridge or pinochle) is to be created and output an array of either 52 or 48 cards. b.shuffleDeck – input an unshuffled deck array and return a shuffled one. + Create a swap method to help shuffle the deck c. countBridgePoints – inputs a 13-card bridge ‘hand’-array and returns the number of...

  • Write a C++ program that simulates a lottery game. Your program should use functions and arrays....

    Write a C++ program that simulates a lottery game. Your program should use functions and arrays. Define two global constants: - ARRAY_SIZE that stores the number of drawn numbers (for example 5) -MAX_RANGE that stores the highest value of the numbers ( for example 9 ) The program will use an array of five integers named lottery, and should generate a random number in the range of 0 through 9 for each element of the array. The user should enter...

  • Answer the following questions and use Excel to show your work. A standard deck of playing...

    Answer the following questions and use Excel to show your work. A standard deck of playing cards consists of 52 cards. The cards in each deck consist of 4 suits, namely spades (♠), clubs (♣), diamonds (♦), and hearts (♥). Each suit consists of 13 cards, namely ace, king, queen, jack, 10, 9, 8, 7, 6, 5, 4, 3, and 2. In the game of poker, a royal flush consists of the ace, king, queen, jack, and 10 of the...

  • 7.12 (Card Shuffling and Dealing) Modify the program in Fig. 7.24 so that the card-dealing function deals a five-card...

    7.12 (Card Shuffling and Dealing) Modify the program in Fig. 7.24 so that the card-dealing function deals a five-card poker hand. Then write the following additional functions: a) Determine whether the hand contains a pair. b) Determinewhetherthehandcontainstwopairs. c) Determine whether the hand contains three of a kind (e.g., three jacks). d) Determinewhetherthehandcontainsfourofakind(e.g.,fouraces). e) Determine whether the hand contains a flush (i.e., all five cards of the same suit). f) Determine whether the hand contains a straight (i.e., five cards of...

  • Suppose you draw two cards with replacement. Round your answers to 3 significant digits*. (a) What...

    Suppose you draw two cards with replacement. Round your answers to 3 significant digits*. (a) What is the probability of getting a queen then a queen again? P(queen on the first and queen on the second) = (b) What is the probability of getting a queen then a jack? P(queen on the first and jack on the second) = (c) What is the probability of getting a queen then a spade? P(queen on the first and spade on the second)...

  • Use inheritance and classes to represent a deck of playing cards. Create a Cardclass that stores the suit (e.g. Clubs, Diamonds, Hearts, Spades), and name (e.g. Ace, 2, 10, Jack) along with appropriat...

    Use inheritance and classes to represent a deck of playing cards. Create a Cardclass that stores the suit (e.g. Clubs, Diamonds, Hearts, Spades), and name (e.g. Ace, 2, 10, Jack) along with appropriate accessors, constructors, and mutators. Next, create a Deck class that stores a vector of Card objects. The default constructor should create objects that represent the standard 52 cards and store them in the vector. The Deck class should have functions to: • Print every card in the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT