Question & Answer: (C++) For this project, you will create a function for a larger magic card trick game. Your task is to write a function tha…..

(C++) 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 beginning 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. You can use more than one array, but for an extra challenge, try to do this in place (using only one array).

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 card values to the array. You do not need to enter all 52 card values, but you need at least a few to make sure your function works properly. (5 points).

You will also need a client program that creates an object of your class and calls the function(s) you created to meet the requirements of this project (5 points).

Expert Answer

#include<iostream>
#include<string>

using namespace std;

class cards{

private:
string deck[5];
public:
void fill_deck(){

deck[0] = “QH”;
deck[1] = “KS”;
deck[2] = “AC”;
deck[3] = “1D”;
deck[4] = “KD”;
}
void insertAtTop(string a){

string data;
int index;
for (int i = 0; i<5; i++){
if (deck[i] == a){
index = i;
break;
}
}

for (int i = index; i>=0; i–){

deck[i] = deck[i-1];
}
deck[0] = a;
}
void printDeck(){
for (int i =0; i<5; i++){
cout << deck[i] << ” “;
}
cout << endl;;
}

};

int main(){

cards cd;
string inp;

cd.fill_deck();
cout << “Initial Deck:”;
cd.printDeck();
cout << “Enter your choice (card value):”;
cin >> inp;
cd.insertAtTop(inp);
cout << “After Insertion:”;
cd.printDeck();
return 0;
}

Still stressed from student homework?
Get quality assistance from academic writers!