I trying to write a program in C++ using vectors its called a card shuffle progr
ID: 3702200 • Letter: I
Question
I trying to write a program in C++ using vectors its called a card shuffle program and I don't know where to start. Here is a copy of what is required for this assignment.
This is the first part of a project to deal 13 cards to 4 players from a shuffled deck of 52 cards. Each card in the deck will be represented by an integer from 0 to 51. We will see later how to translate the integer into a string representing a card. The deck and each hand will be represented by a vector of ints. The vectors should be declared in the main function and not declared globally.
• void unwrap(deck) - load the deck vector with integers in order from 0 to 51
• void shuffle(deck) - shuffle the deck vector using random_shuffle method - see pages 1008 and 1014 in the text (you'll need to "#include <algorithm>" in order to use this)
You can use the debugger to show these functions are working correctly. Future labs will have you write a "deal" function and "showHand" function
Explanation / Answer
The following program will be used to achieve this with the assumption given below:
1. Cards are given random number in the deck starting from 1 to 52.
2. The cards once are assigned random number will be distributed to 4 players hence the players are hard coded.
3. Procedures are created as per the problem required to be solved.
//Programme starts from here...........................
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
void unwrap(vector<int> &deck)
{
for(int i=1; i <= 52; i++)
deck.push_back(i);
//Status of cards before shuffling
cout <<"Before shuffling: " << endl;;
for(int i=0; i < 52; i++)//printing index: value
cout << i << ": " << deck[i] << endl;
}
void shuffle( vector<int> &deck)
{
random_shuffle(deck.begin(), deck.end());
}
int main()
{
vector<int> Vct;
unwrap(Vct);
shuffle(Vct);
cout << " ------------ ";
//after
cout <<"Status of cards after shuffling: " << endl;
for(int i=0; i < 52; i++)
cout << i << ": " << Vct[i] << endl;
cout <<"PLayer 1 cards will be" << endl;
//PLayer 1
for(int i=0; i < 13; i++)
cout << i << ": " << Vct[i] << endl;
cout <<"PLayer 2 cards will be" << endl;
//PLayer 2
for(int i=13; i < 26; i++)
cout << i << ": " << Vct[i] << endl;
cout <<"PLayer 3 cards will be" << endl ;
//PLayer 3
for(int i=26; i < 39; i++)
cout << i << ": " << Vct[i] << endl;
cout <<"PLayer 4 cards will be" << endl;
//Player 4
for(int i=39; i < 52; i++)
cout << i << ": " << Vct[i] << endl;
}