Question

I wrote a card shuffle program and I am trying to see how I can add...

I wrote a card shuffle program and I am trying to see how I can add a sorting function to the program to sort the players hands before printing them. How would I do this?

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>

using namespace std;
void printHand(int []);
void deal(vector<int> &, int[], int[], int[], int[]);
void unwrapDeck(vector<int> &deck)
{
    for(int i=0; i<=51; i++)
        deck.push_back(i);
}
void shuffleDeck(vector<int> &deck) {
    //Status of cards before shuffling
    cout << "Before shuffling: " << endl;
    for (int i = 0; i < 52; i++) {
        cout << i << ": " << deck[i] << endl;
    }

    random_shuffle(deck.begin(), deck.end());
}


int main()
{
    vector<int>deck;
    unwrapDeck(deck);
    shuffleDeck(deck);



    int player1Hand[13], player2Hand[13], player3Hand[13], player4Hand[13];

    deal(deck, player1Hand, player2Hand, player3Hand, player4Hand);

    cout << "Player 1's Hand is: " << endl;
    printHand(player1Hand);

    cout << "Player 2's Hand is: " << endl;
    printHand(player1Hand);

    cout << "Player 3's Hand is: " << endl;
    printHand(player1Hand);

    cout << "Player 4's Hand is: " << endl;
    printHand(player1Hand);

    return 0;
}

void printHand(int hand[])
{

    const string RANKS[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                            "Jack", "Queen", "King"};
    const string SUITS[] = {"Spades", "Hearts", "Diamonds", "Clubs"};

    for(int i = 0; i < 13; i++)
    {
        cout<<RANKS[hand[i] % 13]<< "of"<<SUITS[hand[i]/13]<<endl;
    }

        cout << endl;
}


void deal(vector <int> &deck, int player1Hand[], int player2Hand[], int player3Hand[], int player4Hand[])
{
    int deckSize = deck.size();


    for(int i = 0; i < deckSize/4; i++)
    {
        player1Hand[i] = deck[deck.size()-1];
        deck.pop_back();

        player2Hand[i] = deck[deck.size()-1];
        deck.pop_back();

        player3Hand[i] = deck[deck.size()-1];
        deck.pop_back();

        player4Hand[i] = deck[deck.size()-1];
        deck.pop_back();


    }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>

using namespace std;
void printHand(int []);
void deal(vector<int> &, int[], int[], int[], int[]);
void unwrapDeck(vector<int> &deck)
{
    for(int i=0; i<=51; i++)
        deck.push_back(i);
}
void shuffleDeck(vector<int> &deck) {
    //Status of cards before shuffling
    cout << "Before shuffling: " << endl;
    for (int i = 0; i < 52; i++) {
        cout << i << ": " << deck[i] << endl;
    }

    random_shuffle(deck.begin(), deck.end());
}


int main()
{
    vector<int>deck;
    unwrapDeck(deck);
    shuffleDeck(deck);

    int player1Hand[13], player2Hand[13], player3Hand[13], player4Hand[13];

    deal(deck, player1Hand, player2Hand, player3Hand, player4Hand);

    cout << "Player 1's Hand is: " << endl;
    printHand(player1Hand);

    cout << "Player 2's Hand is: " << endl;
    printHand(player1Hand);

    cout << "Player 3's Hand is: " << endl;
    printHand(player1Hand);

    cout << "Player 4's Hand is: " << endl;
    printHand(player1Hand);

    return 0;
}

void sort(int hand[]) {
   int temp;
   for(int i = 0; i < 13; i++) {
       for(int j =0 ; j < 12; j++) {
           if(hand[j] % 13 > hand[j+1] % 13) {
               temp = hand[j];
               hand[j] = hand[j+1];
               hand[j+1] = temp;
           }
       }
   }
}

void printHand(int hand[])
{

   sort(hand);
    const string RANKS[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                            "Jack", "Queen", "King"};
    const string SUITS[] = {"Spades", "Hearts", "Diamonds", "Clubs"};

    for(int i = 0; i < 13; i++)
    {
        cout<<RANKS[hand[i] % 13]<< "of"<<SUITS[hand[i]/13]<<endl;
    }

        cout << endl;
}


void deal(vector <int> &deck, int player1Hand[], int player2Hand[], int player3Hand[], int player4Hand[])
{
    int deckSize = deck.size();


    for(int i = 0; i < deckSize/4; i++)
    {
        player1Hand[i] = deck[deck.size()-1];
        deck.pop_back();

        player2Hand[i] = deck[deck.size()-1];
        deck.pop_back();

        player3Hand[i] = deck[deck.size()-1];
        deck.pop_back();

        player4Hand[i] = deck[deck.size()-1];
        deck.pop_back();


    }
}

50: 50 51: 51 Player 1s Hand is: AceofHearts ceofClubs ofHearts ofHearts 6ofDiamonds PofHearts 8ofDiamonds 8ofClubs ofHearts

Please\;let\;me\;know\;if\;you\;need\;me\;to\;change\;anything\\ Please\;upvote\;this\;answer!

Add a comment
Know the answer?
Add Answer to:
I wrote a card shuffle program and I am trying to see how I can add...
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
  • (Card Shuffling and Dealing) Modify the program below so that the card-dealing function deals a five-card...

    (Card Shuffling and Dealing) Modify the program below so that the card-dealing function deals a five-card poker hand. Then write the following additional functions: a) Determine whether the hand contains two pairs b) Determine whether the hand contains a full house (i.e., three of a kind with pair). c) Determinewhetherthehandcontainsastraight flush (i.e.,fivecardsofconsecutivefacevalues). d) Determine whether the hand contains a flush (i.e., five of the same suit) #include <stdio.h> #include <stdlib.h> #include <time.h> #define SUITS 4 #define FACES 13 #define CARDS...

  • 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...

  • HELP NEED!! Can some modified the program Below in C++ So that it can do what...

    HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include <time.h> /* time */ #include <sstream> // for ostringstream using namespace std; // PlayingCard class class PlayingCard{ int numRank; int numSuit; string rank; string suit; public: PlayingCard(); PlayingCard(int numRank,int numSuit); void setRankString(); void setSuitString(); void setRank(int numRank); void setSuit(int numSuit); string getRank(); string getSuit(); int getRankNum(); int getSuitNum(); string getCard(); };...

  • HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include...

    HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include <time.h> /* time */ #include <sstream> // for ostringstream using namespace std; // PlayingCard class class PlayingCard{ int numRank; int numSuit; string rank; string suit; public: PlayingCard(); PlayingCard(int numRank,int numSuit); void setRankString(); void setSuitString(); void setRank(int numRank); void setSuit(int numSuit); string getRank(); string getSuit(); int getRankNum(); int getSuitNum(); string getCard(); };...

  • In java---- The DeckTester.java file, provides a basic set of Deck tests. Add additional code at...

    In java---- The DeckTester.java file, provides a basic set of Deck tests. Add additional code at the bottom of the main method to create a standard deck of 52 cards and test the shuffle method ONLY in the Deck class. After testing the shuffle method, use the Deck toString method to “see” the cards after every shuffle. Deck: import java.util.List; import java.util.ArrayList; /** * The Deck class represents a shuffled deck of cards. * It provides several operations including *...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • Can somebody help me with this coding the program allow 2 players play tic Tac Toe....

    Can somebody help me with this coding the program allow 2 players play tic Tac Toe. however, mine does not take a turn. after player 1 input the tow and column the program eliminated. I want this program run until find a winner. also can somebody add function 1 player vs computer mode as well? Thanks! >>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>> #include "myheader.h" int main() { const int NUM_ROWS = 3; const int NUM_COLS = 3; // Variables bool again; bool won;...

  • Hi! I need help with a C++ program In this assignment, you will create some routines...

    Hi! I need help with a C++ program In this assignment, you will create some routines that will later be used in a "Black Jack" program. Your output on this first part will look something like: card's name is QC with a black jack value of 10 Unshuffled Deck AC/1 2C/2 3C/3 4C/4 5C/5 6C/6 7C/7 8C/8 9C/9 TC/10 JC/10 QC/10 KC/10 AD/1 2D/2 3D/3 4D/4 5D/5 6D/6 7D/7 8D/8 9D/9 TD/10 JD/10 QD/10 KD/10 AH/1 2H/2 3H/3 4H/4 5H/5...

  • ////****what am i doing wrong? im trying to have this program with constructors convert inches to...

    ////****what am i doing wrong? im trying to have this program with constructors convert inches to feet too I don't know what im doing wrong. (the program is suppose to take to sets of numbers feet and inches and compare them to see if they are equal , greater, less than, or not equal using operator overload #include <stdio.h> #include <string.h> #include <iostream> #include <iomanip> #include <cmath> using namespace std; //class declaration class FeetInches { private:    int feet;   ...

  • Deck of Cards Program I need help printing a flush, which is showing the top 5...

    Deck of Cards Program I need help printing a flush, which is showing the top 5 cards of the same suite. Below is the code I already have that answers other objectives, such as dealing the cards, and finding pairs. Towards the end I have attempted printing a flush, but I cannot figure it out. public class Shuffler {    /**    * The number of consecutive shuffle steps to be performed in each call    * to each sorting...

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
ADVERTISEMENT