Computer Science, asked by waleedayu41, 3 months ago

Deck of card contains 52 cards in total and catogrised in 4 different categories named as Heart, Diamond, Spade and Club. Each category contains 13 cards. Each category having cards denomination [2,3,4,5,6,7,8,9,10, J(Jack), Q(Queen), K(King), A(Ace)].
Suppose 4 cards are missing (we are unknown about missing cards) and remaining 48 mixed cards are distributed arbitrarily/randomly to 4 different players. Write a C++ program to demonstrate the above-mentioned problem by adding three functions other than main() function:
A) First one is Sort() function which pack all these cards into one packet by arranging them in a sequence of all Spade card comes 1st in asending order, then all Club cards, then all Heart cards and at the end all Diamond cards.
B) Second one is displayCards() function which displays all sorted cards achieved in Part-A in the following format:
The Pack Cards are:
Card1: Spade 2
Card2: Spade 3
…………………………….
…………………………….
Card48: Diamond A

C) Third one is findMissing() function which displays the category and number of missing cards in the following format e.g.:
The Missing Cards of 20-roll-yourNo:
Card1: Spade 4
Card2: Heart J
Card3: Club 10
Card4: Diamond 3

Note: You have to add the screen shot of your output of missing cards.

D) Fourth one is findMissingFromFile() function which reads all 48 sorted cards data from file “your_roll_No.txt” (assume file is already available with all the sorted data e.g. Spade 2) and find & write missing cards into another file “missingCards.txt” in the following format:
Card1 Spade 4
Card2 Heart J
Card3 Club 10
Card4 Diamond 3

Answers

Answered by shahvaiz123satti
2

Answer:

Explanation:

#include<iostream>

#include<string>

#include<map>

#include<fstream>

#include<ctype.h>

#include<algorithm>

using namespace std;

struct Card{

   int value;

   string category;

};

Card cards[52];

map<int, string> cardFace;

void initMap(){

   for(int i = 1; i <= 13; ++i){

       if(i >= 1 && i < 10)

           cardFace[i] = to_string(i+1);

       else{

           if(i == 10)

               cardFace[i] = "J";

           else if(i == 11)

               cardFace[i] = "Q";

           else if(i == 12)

               cardFace[i] = "K";

           else if(i == 13)

               cardFace[i] = "A";

       }

   }

}

void initCards(){

   int k = 0;

   for(int i = 1; i <= 13; ++i){

       Card card;

       card.value = i;

       card.category = "Spade";

       cards[k++] = card;

   }

   for(int i = 1; i <= 13; ++i){

       Card card;

       card.value = i;

       card.category = "Spade";

       cards[k++] = card;

   }

   for(int i = 1; i <= 13; ++i){

       Card card;

       card.value = i;

       card.category = "Spade";

       cards[k++] = card;

   }

   for(int i = 1; i <= 13; ++i){

       Card card;

       card.value = i;

       card.category = "Spade";

       cards[k++] = card;

   }

}

bool comparator(Card card1, Card card2){

   return card1.value < card2.value;

}

/*

* Sorts down the cards array

*/

void sortCards(Card randomCards[]){

   int size = 48;

   Card spadeCards[13], clubCards[13], heartCards[13], diamondCards[13];

   int j = 0, k = 0, l = 0, m = 0;

   for(int i = 0; i < size; ++i)

   {

       Card card = randomCards[i];

       if(card.category == "Spade")

           spadeCards[j++] = card;

       else if(card.category == "Club")

           clubCards[k++] = card;

       else if(card.category == "Heart")

           heartCards[l++] = card;

       else if(card.category == "Diamond")

           diamondCards[m++] = card;

   }

   sort(spadeCards, spadeCards + j, comparator);

   sort(clubCards, clubCards + k, comparator);

   sort(heartCards, heartCards + l, comparator);

   sort(diamondCards, diamondCards + m, comparator);

   int n = 0;

   for(auto val: spadeCards) randomCards[n++] = val;

   for(auto val: clubCards) randomCards[n++] = val;

   for(auto val: heartCards) randomCards[n++] = val;

   for(auto val: diamondCards) randomCards[n++] = val;

}

void displayCards(Card sortedCards[]){

   int n = 48;

   for(int i = 1; i <= n; ++i)

       cout<<"Card"<<to_string(i)<<": "<<sortedCards[i-1].category<<" "<<cardFace[sortedCards[i-1].value]<<"\n";

}

void findMissing(Card randomCards[]){

   cout<<"The Missing Cards of 20-Arid-yourNo:\n";

   sortCards(randomCards);

   int a = 1;

   for(int i = 0; i < 48; ++i){

       if(find(cards, cards + 52, randomCards[i]) == (cards + 52))

           cout<<"Card"<<to_string(a)<<": "<<randomCards[i].category<<" "<<cardFace[randomCards[i-1].value]<<"\n";

   }

}

/*

* Uses File handling to read and write data

*/

void findMissingFromFile(){

   ofstream fout;

   ifstream fin;

   string line;

   Card randomCards[48];

   int i = 0;

   fin.open("your_Arid_No.txt");

   while(fin){

       getline(fin, line);

       Card card;

       if(line.find("Spade") != string::npos) card.category = "Spade";

       else if(line.find("Spade") != string::npos) card.category = "Club";

       else if(line.find("Spade") != string::npos) card.category = "Heart";

       else if(line.find("Spade") != string::npos) card.category = "Diamond";

       if(isdigit(line.at(line.size()-1))){

           if(line.find("0") != string::npos) card.value = 9;

           else card.value = stoi(line.substr(line.size()- 1)) - 1;

       }

       else{

           if(line.substr(line.size()-1) == "J") card.value = 10;

           else if(line.substr(line.size()-1) == "Q") card.value = 11;

           else if(line.substr(line.size()-1) == "K") card.value = 12;

           else if(line.substr(line.size()-1) == "A") card.value = 13;

       }

       randomCards[i++] = card;

   }

   fout.open("missingCards.txt");

   sortCards(randomCards);

   int a = 1;

   for(int i = 0; i < 48; ++i){

       if(find(cards, cards + 52, randomCards[i]) == (cards + 52))

           fout<<"Card"<<to_string(a)<<": "<<randomCards[i].category<<" "<<cardFace[randomCards[i-1].value]<<"\n";

   }

   fin.close();

   fout.close();

}

int main(){

   initMap();

   initCards();

   Card playerCards[48];

   for(int i = 0; i < 48; ++i){

       Card card;

       cout<<"\nEnter Category: ";

       cin>>card.category;

       cout<<"\nEnter value: ";

       cin>>card.value;

       playerCards[i] = card;

   }

   displayCards(playerCards);

   findMissing(playerCards);

   sortCards(playerCards);

   displayCards(playerCards);

   return 0;

}

Similar questions