You must follow the style guide e for all projects. For this project, you will c
ID: 3574166 • Letter: Y
Question
You must follow the style guide e for all projects. For this project, you will create a function for a larger magic card trick game. Your task is to write a function that will allow a user will pick a card from a collection of cards and place it on the top of the deck of cards. The deck of cards will be an array of strings. The individual cards will be stored in the array. You can use a string to represent each card. For example, the queen of hearts might be stored as QH or "Queen of Hearts". Keep in mind that the card's value must be passed to your function, so an abbreviation for each card may be easier to work with. Here are the preconditions and post conditions for your function: Precondition: the array is not empty. Postcondition: This function removes the card value from an array and reinserts it at the begining of the array. This will reorder the elements in the array so the chosen card is in position 0 of the array and all other elements have been repositioned to make room. No card values are lost. Minimum Requirements: Create a class that has a string array as a private data member. You cannot use a vector for this assignment (5 points). You must include a function (member or non-member function) to fulfill the precondition and postcondition listed above (5 points). For testing purposes, include a class function to add some playing cardExplanation / Answer
PROGRAM CODE:
#include <iostream>
using namespace std;
class MagicTrick
{
private:
string cards[20];
public:
//method for generating randome number between a range
//this is for getting an index value which will be used to pick a suite and number
int generaterandom(int min, int max)
{
return rand()%max + min;
}
//method to check whether the combination picked is already present in the deck of cards
bool isPresent(string value)
{
for(int j=0; j<20; j++)
{
if(cards[j] == value)
return true;
}
return false;
}
//adding 20 cards into the array in random manner
void addCards()
{
//arrays to make combination
string suites[] = {"H", "C", "D", "S"};
string numbers[] = {"A","2", "3", "4", "5", "6", "7", "8", "9", "J", "Q", "K"};
//adding 20 cards
for(int i=0; i<20; i++)
{
int suitesRandom = generaterandom(0, 3);
int numbersRandom = generaterandom(0, 11);
//checking if the card combination is already present if so generating again
while(isPresent(numbers[numbersRandom] + suites[suitesRandom]))
{
suitesRandom = generaterandom(0, 3);
numbersRandom = generaterandom(0, 11);
}
cards[i] = numbers[numbersRandom] + suites[suitesRandom];
}
}
//printing the cards
void printCards()
{
cout<<" ";
for(int i=0; i<20; i++)
{
cout<<cards[i]<<" ";
if((i+1)%5==0)
cout<<" ";
}
cout<<" ";
}
//function to perform magic trick
void magicTrick()
{
int cardNumber;
cout<<"Pick a card : (Enter a number between 1 and 20): ";
cin>>cardNumber;
cout<<" ";
//if the first card is picked no need to shift the cards
if(cardNumber==1)
{
return;
}
string subArray[cardNumber];
for(int i=0; i<cardNumber; i++)
{
subArray[i] = cards[i];
}
cards[0] = cards[cardNumber-1];
for(int i=0; i<cardNumber-1; i++)
{
cards[i+1] = subArray[i];
}
}
};
//driver function
int main() {
MagicTrick trick;
trick.addCards();
trick.printCards();
trick.magicTrick();
trick.printCards();
return 0;
}
OUTPUT:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.