Question

In: Computer Science

IN C++ Write a program to play the Card Guessing Game. Your program must give the...

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.

Solutions

Expert Solution

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!


Related Solutions

Write a program using Python that allows the user to play a guessing game (please provide...
Write a program using Python that allows the user to play a guessing game (please provide a picture of code so it is easier to read). The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: Normally, we would have the program select a random number as the "secret number". However, for the purpose of testing your program (as well as grading it), simply use an assignment...
Number guessing Game (20 Marks) Write a C program that implements the “guess my number” game....
Number guessing Game Write a C program that implements the “guess my number” game. The computer chooses a random number using the following random generator function srand(time(NULL)); int r = rand() % 100 + 1; that creates a random number between 1 and 100 and puts it in the variable r. (Note that you have to include <time.h>) Then it asks the user to make a guess. Each time the user makes a guess, the program tells the user if...
Random Number Guessing Game Write a program in C++ that generates a random number between 1...
Random Number Guessing Game Write a program in C++ that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high. Try again.” If the user’s guess is lower than the random number, the program should display “Too low. Try again.” The program should use a loop that repeats until the user correctly guesses the random number....
Random Number Guessing Game C++. Write a program that generates a random number between 5 and...
Random Number Guessing Game C++. Write a program that generates a random number between 5 and 20 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display Too high. Try again. If the user’s guess is lower than the random number, the program should display Too low, Try again. The program should use a loop that repeats while keeping a count of the number of guesses...
C++ The program implements the Number Guessing Game. However, in that program, the user is given...
C++ The program implements the Number Guessing Game. However, in that program, the user is given as many tries as needed to guess the correct number. Rewrite the program so that the user has no more than five tries to guess the correct number. Your program should print an appropriate message, such as “You win!” or “You lose!”.
Write a Java program that lets the user play a game where they must guess a...
Write a Java program that lets the user play a game where they must guess a number between zero and one hundred (0-100). The program should generate a random number between 0 and 100 and then the user will try to guess the number. The program should help the user guess the number (see details below). The program must allow the user five attempts to guess the number. Further Instruction: (a) Declare a variable diff and assign to it the...
1. [100] Create a C program for a number guessing game. Here are the requirements: a....
1. [100] Create a C program for a number guessing game. Here are the requirements: a. Your program generates a random number between -100 and 100 and keeps asking the user to guess the number until the user guesses it correctly. Your random number generator should be implemented as a C function which takes min and max values as input parameters and returns a random number between those values including the min and the max. b. If the user guesses...
I am using C++ Write a program that allows two players to play a game of...
I am using C++ Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Displays the contents of the board array. Allows player 1 to select a location on the board for an X. The program should ask the user...
Write a program in Basic to play the game of Nim with acom­puter.
Write a program in Basic to play the game of Nim with acom­puter.
Write a guessing game java program (basic program, we didn't study further than loops and files)...
Write a guessing game java program (basic program, we didn't study further than loops and files) where the user has to guess a secret number between 1 and 100. After every guess the program tells the user whether their number was too large or too small. At the end the number of tries needed should be printed. It counts only as one try if they input the same number multiple times consecutively.Example of running this program: Enter your secret number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT