Question

(C++) Write a program to play the Card Guessing game. Your program must give the user...

(C++)

Write a program to play the Card Guessing game. Your program must give the user the following choices:

a. Guess the face value of my card.

b. Guess only the suit of the card.

c. Guess both the face value and the 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. Also include vector into the program. (C++)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here i am providing the answer. hope it helps. please give me a like, it helps me a lot.

step: 1 of 6

Program Plan:

• Declare a function num_gen().

• Declare a function game().

• Define the main() function.

o Create a list containing numbers from 1 to 13.

o Create another list containing numbers from 1 to 4.

o Randomize these lists using the function random_shuffle().

o Pass these lists as argument to the function game().

• Define the game() function.

o Ask the user to input the game which is to be played.

o Pick a card at random.

o Display winning message if the user won the guessing game.

o Display the losing message otherwise.

• Define the num_gen() function.

o Define an integer variable with value 1.

o Return the value of the variable and then increase the value of the variable by 1.

step: 2 of 6

Program:

/**********************************************************

* The following program creates a card guessing game. *

**********************************************************/

//Remove this line if not using visual studio.

#include "stdafx.h"

//Include the required header files.

#include

#include

#include

#include

#include

#include

#include

//Use the standard namespace.

using namespace std;

//Create a function num_gen() to

// insert the numbers in the value array.

int num_gen();

//Create a function game() to play the game.

void game(int x[], int y[]);

//Define the main() function.

int main()

{

//Declare the required variables.

int val[13],i=0;

int shape[4]={1,2,3,4};

//Call the srand() function so that the

// rand() function generates a different

// random sequence eacj time.

srand(time(0));

//Insert values from 1 to 13 in the array

//to use as the face values.

generate(val,val+13,num_gen);

//Randomize the list of containing the values.

random_shuffle(val,val+13);

//Randomize the list containing the suits

// of the cards.

random_shuffle(shape,shape+4);

//Pass both the lists

// to the function game.

game(val,shape);

//Remove this line if not using visual studio.

system("pause");

//Return value for the main() function.

return 0;

}

//Define the function num_gen().

int num_gen()

{

//Define the variable x to

//store the numbers.

static int x=1;

//Store the value in variable y.

int y=x;

//Increase the value of x by 1.

x++;

//Return the value of y.

return y;

step: 3 of 6

}

//Define the function game().

void game(int x[], int y[])

{

//Declare the required variables.

int check_shape;

int check_val;

int uval,ushape,ch;

//Prompt the user for the input.

cout<<"Choose one of the following options:"<

cout<<"1-> Guess the number of the card"<

cout<<"2-> Guess the shape of the card"<

cout<<"3-> Guess both the number and"

"the shape of the card"<

//Store the choice of the user.

cin>>ch;

//Check if the choice is 1.

if(ch==1)

{

//Prompt the user for the input.

cout<<"Enter the number of the card"<

//Store the input in the uval variable.

cin>>uval;

//Store a random value from the list

// into the check_val variable.

check_val=x[rand()%13];

cout<<"The value of the card is: "

<

//Check if the user guessed the value correctly

// or not.

if(check_val==uval)

{

//Display this message.

cout<<"You guessed the value"

"of the card correctly :) .";

}

//Else case.

else

{

//Display this message.

cout<<"Better Luck next time :( .";

}

}

//Check if the choice is 2.

else if(ch==2)

{

//Store a random suit into the

// check_shape variable.

check_shape=y[rand()%4];

//Prompt the user for the input.

cout<<"Enter the number of the suit"

" that you want to choose:"<

cout<<"1. Spade"<

cout<<"2. Club"<

cout<<"3. Heart"<

cout<<"4. Diamond"<

//Store the input in ushape variable.

cin>>ushape;

//Display the suit of the randomly

// picked card.

cout<<"The suit of the card is: "<

//Switch case for displaying the suit.

switch(check_shape)

{

//If 1 was picked from the list shape.

case 1:

cout<<"Spade"<

break;

//If 2 was picked from the list shape.

case 2:

cout<<"Club"<

break;

//If 3 was picked from the list shape.

case 3:

cout<<"Heart"<

break;

//If 4 was picked from the list shape.

case 4:

cout<<"Diamond"<

break;

//Default case.

default:

step: 4 of 6

cout<<"Wrong choice entered";

}

//Check if the user enetred the correct shape.

if(ushape==check_shape)

{

//Display this messsage.

cout<<"You guessed the suit of the card correctly"

<<":) .";

}

//Else case.

else

{

//Display this message.

cout<<"Better Luck next time :( .";

}

}

//Check if the user entered 3 as the choice.

else if(ch==3)

{

//Pick a random card from the deck.

check_val=x[rand()%13];

check_shape=y[rand()%4];

//Prompt the user for the input.

cout<<"Enter the number of the card"<

cin>>uval;

cout<<"Enter the number of the suit that"

" you want to choose:"<

cout<<"1. Spade"<

cout<<"2. Club"<

cout<<"3. Heart"<

cout<<"4. Diamond"<

cin>>ushape;

//Displaythe randomly

// picked card.

cout<<"The card is: "<

//Switch case for displaying the suit.

switch(check_shape)

{

//If 1 was picked from the list shape.

case 1:

cout<<"Spades"<

break;

//If 2 was picked from the list shape.

case 2:

cout<<"Clubs"<

break;

//If 3 was picked from the list shape.

case 3:

cout<<"Hearts"<

break;

//If 4 was picked from the list shape.

case 4:

cout<<"Diamonds"<

break;

//Default case.

default:

cout<<"Wrong choice entered";

}

//Check if the user guessed the correct card.

if(ushape==check_shape&&uval==check_val)

{

//Display this message.

cout<<"You guessed the card correctly :) .";

}

//Else case.

else

{

//Display this message.

cout<<"Better Luck next time :( .";

}

}

//Else case.

else

cout<<"Wrong choice";

}

step: 5 of 6

Sample Output:

Choose one of the following options:

1-> Guess the number of the card

2-> Guess the suit of the card

3-> Guess both the number and the suit of the card

3

Enter the number of the card

12

Enter the number of the suit that you want to choose:

step: 6 of 6

1. Spade

2. Club

3. Heart

4. Diamond

4

The card is: 6 of Hearts

Thank you. please upvote.

Better Luck next time :( .

Add a comment
Know the answer?
Add Answer to:
(C++) Write a program to play the Card Guessing game. Your program must give the user...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ program to play the number guessing game with user as follows.

    Write a C++ program to play the numberguessing game with user as follows.The user determines a number between 1~100.The program has 4 tries to guess the number byproviding a range (low, high) on each try.The user tells the program whether the number isbelow, within, or above the range.The program announces a game loss after 4 incorrecttries.

  • Write a program that allows a user to play a guessing game. Pick a random number...

    Write a program that allows a user to play a guessing game. Pick a random number in between 1 and 100, and then prompt the user for a guess. For their first guess, if it’s not correct, respond to the user that their guess was “hot.” For all subsequent guesses, respond that the user was “hot”if their new guess is strictly closer to the secret number than their old guess and respond with“cold”, otherwise. Continue getting guesses from the user...

  • CS102 : JAVA Object-Oriented Programming. 1 Write a class Card whose instances represent a single playing...

    CS102 : JAVA Object-Oriented Programming. 1 Write a class Card whose instances represent a single playing card from a deck of cards. Playing cards have two distinguishing properties an integer for the rank (1 (corresponding to Ace) ,2,3, 13 (correspond ing to King) and suit (Spades, Hearts, Diamonds, or Clubs). Make the suit an enumerated data type. Include getters and setters and a method that tests if a card is valid. Write a class named Deck whose instances are full...

  • Specification Write a program that allows the user to play number guessing games. Playing a Guessing...

    Specification Write a program that allows the user to play number guessing games. Playing a Guessing Game Use rand() function from the Standard C Library to generate a random number between 1 and 100 (inclusive). Prompt the user to enter a guess. Loop until the user guesses the random number or enters a sentinel value (-1) to give up. Print an error message if the user enters a number that is not between 1 and 100. Print an error message...

  • (Card Shuffling and Dealing) Modify the program below so that the card-dealing function deals a five-card...

    (Card Shuffling and Dealing) Modify the program below so that the card-dealing function deals a five-card poker hand. Then write the following additional functions: a) Determine whether the hand contains two pairs b) Determine whether the hand contains a full house (i.e., three of a kind with pair). c) Determinewhetherthehandcontainsastraight flush (i.e.,fivecardsofconsecutivefacevalues). d) Determine whether the hand contains a flush (i.e., five of the same suit) #include <stdio.h> #include <stdlib.h> #include <time.h> #define SUITS 4 #define FACES 13 #define CARDS...

  • 7.12 (Card Shuffling and Dealing) Modify the program in Fig. 7.24 so that the card-dealing function deals a five-card...

    7.12 (Card Shuffling and Dealing) Modify the program in Fig. 7.24 so that the card-dealing function deals a five-card poker hand. Then write the following additional functions: a) Determine whether the hand contains a pair. b) Determinewhetherthehandcontainstwopairs. c) Determine whether the hand contains three of a kind (e.g., three jacks). d) Determinewhetherthehandcontainsfourofakind(e.g.,fouraces). e) Determine whether the hand contains a flush (i.e., all five cards of the same suit). f) Determine whether the hand contains a straight (i.e., five cards of...

  • Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, al...

    Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, allow the user to try up to 10 times to guess the number, and tell the user if each guess is too high, too low, or correct. The actual game portion must be a function...you can use more than one function in your program. The amount...

  • Using C++: 1. Array and Struct in Card applications: In card game design, card suit and...

    Using C++: 1. Array and Struct in Card applications: In card game design, card suit and face values can be defined with enumeration values. Declare suit values - – hearts, clubs, diamonds, spades using enum, declare face values of 2 – 10, Jack, Queen, King, Ace using enum Declare a structure called Card, and it includes two data members, whose data types are the above two defined enumerations suit and face typedef an array called Deck, and it includes all...

  • War—A Card game Playing cards are used in many computer games, including versions of such classic...

    War—A Card game Playing cards are used in many computer games, including versions of such classics as solitaire, hearts, and poker. War:    Deal two Cards—one for the computer and one for the player—and determine the higher card, then display a message indicating whether the cards are equal, the computer won, or the player won. (Playing cards are considered equal when they have the same value, no matter what their suit is.) For this game, assume the Ace (value 1) is...

  • NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET...

    NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET CODE TO RUN!! THANKS Extend the DeckofCards and the Card class in the book to implement a card game application such as BlackJack, Texas poker or others. Your game should support multiple players (up to 5 for BlackJack). You must build your game based on the Cards and DeckofCards class from the book. You need to implement the logic of the game. You can...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT