In: Computer Science
IN C++
Write a program to play the Card Guessing Game. Your program
must give the user the following choices:
- Guess only the face value of the card.
-Guess only the suit of the card.
-Guess both the face value and suit of the card.
Before the start of the game, create a deck of cards. Before each
guess, use the function random_shuffle to randomly shuffle the
deck.
CODE:
#include<iostream>
#include<bits/stdc++.h>
#include<time.h>
using namespace std;
//struct Card represents a Card
struct Card{
//suit stores the suit name
string suit;
//faceValue stores the faceValue
int faceValue;
};
//function to shuffle the deck of Cards
void randomShuffle(struct Card *deck){
//shuffling 27 times
for(int i=0;i<27;i++){
//getting a random number
int randIndex = rand()%52;
//swapping the card at randIndex and 51 - randIndex
struct Card temp = deck[randIndex];
deck[randIndex] = deck[51 - randIndex];
deck[51 - randIndex] = temp;
}
}
//main method
int main(){
srand(time(0));
int randomNum,faceValue;
string suit;
//initializing the card deck
struct Card *deck = new Card[52];
string suits[4] = {"Heart","Diamond","Spade","Club"};
for(int i=0;i<4;i++){
for(int j=0;j<13;j++){
deck[13*i + j].suit = suits[i];
deck[13*i + j].faceValue = j+1;
}
}
//shuffling the deck
randomShuffle(deck);
//asking the choice from the user
cout<<"Enter\n1 to guess the face value of the
card"<<endl;
cout<<"2 to guess only the suit of the
card"<<endl;
cout<<"3 to guess both the suit and value of the
card"<<endl;
int choice;
cin>>choice;
switch(choice){
case 1:
//if 1 was entered
randomNum = rand()%52;
//user guesses the faceValue
cout<<"Guess the face value(1-13):"<<endl;
faceValue;
cin>>faceValue;
//if he/she guessed it right the appropriate message is
displayed
if(faceValue == deck[randomNum].faceValue)
cout<<"You guessed it right!"<<endl;
else{
cout<<"Wrong guess!"<<endl;
cout<<"The card is: "<<deck[randomNum].suit<<"
"<<deck[randomNum].faceValue<<endl;
}
break;
case 2:
//if the user entered 2
randomNum = rand()%52;
cout<<"Guess the suit(case-sensitive): (Heart, Diamond,
Spade, Club)"<<endl;
suit;
//user entered suit name
cin>>suit;
//the appropriate message is displayed according to the choice
made
if(suit == deck[randomNum].suit)
cout<<"You guessed it right!"<<endl;
else{
cout<<"Wrong guess!"<<endl;
cout<<"The card is: "<<deck[randomNum].suit<<"
"<<deck[randomNum].faceValue<<endl;
}
break;
case 3:
//case 3 is a mixture of both cases
randomNum = rand()%52;
//user enters both the suit and faceValue
cout<<"Guess the face value(1-13):"<<endl;
faceValue;
cin>>faceValue;
cout<<"Guess the suit(case-sensitive): (Heart, Diamond,
Spade, Club)"<<endl;
suit;
cin>>suit;
//message is displayed accordingly
if(suit == deck[randomNum].suit && faceValue ==
deck[randomNum].faceValue)
cout<<"You guessed it right!"<<endl;
else{
cout<<"Wrong guess!"<<endl;
cout<<"The card is: "<<deck[randomNum].suit<<"
"<<deck[randomNum].faceValue<<endl;
}
break;
default:
cout<<"Invalid option selected"<<endl;
}
return 0;
}
_________________________________________
CODE IMAGES:
__________________________________________________
OUTPUT:
_______________________________________________
Feel free to ask any questions in the comments section
Thank You!