Goals 1. To use an array. 2. To use functions from the math library. 3. To imple
ID: 3887959 • Letter: G
Question
Goals
1. To use an array.
2. To use functions from the math library.
3. To implement some simple logic.
4. To use C input and output.
5. To read single characters or numbers from the keyboard
We will build a program where the program will think select a card at
random and you have to guess what it is. This logic might be reused in later
projects.
2 Requirements
Create a game that picks a card at random and the player has to guess the card. You must follow the
following requirements.
1. Create an array with 13 slots (one per card value – ace to king or two to ace). The slots can be an
integer showing the value or a string showing the name. Remember in C, a string is an array of
characters.
2. Create a second array with four slots – the four card suits. The order of suits is clubs (),
diamonds (), hearts () and spades (). You do not have to show symbols for the suits,
they can be shown as text. The contents of the arrays slots can be a number or the
name of the suit.
3. Use the rand() function from the math library (#include <math.h>) to randomly select a suit and
card value. You can show these on the screen when you are testing, but comment the code to
show the suit when you submit your program. Include a comment identifying the line.
4. Use a for loop to go through the suits array and print the menu for selecting the suit. The menu
must use the array.
5. Ask the user to enter a number/letter/word to pick their suit. The last item on the menu should
be an option to quit. Read the input using scanf().
6. If the user quits, the program should end with a message thanking them for using the program.
7. If does not guess the correct suit, the program should tell them it is wrong and invite them to try
again. Then the program should end with a message thanking them for using the program.
8. If the user correctly selects the secret suit:
a. Show the menu of cards and ask them to guess the value of the secret card.
b. Use the array of cards and a for loop to display the menu. The last item should be the
option to quit.
c. Use scanf() to read their input.
d. If the user quits, the program should end with a message thanking them for using the
program.
e. The program should show the suit and card selected by the user on one line.
f. If the user does not guess the correct suit, the program should tell them it is wrong and
invite them to try again. The program should end.
g. If the user guesses the correct card, the program should congratulate them for winning.
Then the program should end with a message thanking them for using the program.
CSCI 2212 Intermediate Programming C++ Fall 2017
Program 1 Requirements
Pick a Card, Any Card
9. When the user enters values that are invalid (card is not a card, suit is not a suit) make sure the
exit is graceful. The program should give an error message and end with the normal exit
message.
Style Guide
Follow the instructions in the style guide fully. There are deductions for style.
Testing and Output.
• Test your program with at least 5-10 tries, include one to three correct guesses, try correct suit
but wrong card, try correct card but wrong suit and both wrong and a few more tries. You can
print the secret card selected to the screen so you can guess as needed for the tests.
• Test that when the user enters values that are invalid (card is not a card, suit is not a suit) make
sure the exit is graceful. E.g. program should not crash.
• Make sure valid input works correctly.
• Enter some digits or punctuation and be sure they are handled right.
Explanation / Answer
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void main()
{
//create array of cards from Ace to king
char *cardValue[] = {"ace","two","three","four","five","six","seven","eight","nine","ten","joker","queen","king"};
//create array of suites
char *cardSuite[] = {"clubs","diamonds","hearts","spades"};
//declare time variable to use in random as a seed
time_t t;
//this will use t as a seed, and will give the next random different everytime
srand((unsigned) time(&t));
//computer will select a random card
char *randomCardValue;
//computer will select a random suit
char *randomSuite;
int suiteChoice=0,cardChoice=0;
do
{
//generate random card
randomCardValue = cardValue[rand() % 13];
//generate random suite
randomSuite = cardSuite[rand() % 4];
//make these lines comment
printf("%s ",randomCardValue);
printf("%s ",randomSuite);
//for loop to display suites
int i=0;
for(i=0;i<4;i++)
{
printf(" %d. %s ",(i+1),cardSuite[i]);
}
//quit option
printf("5. Quit the game");
printf(" Choose your suite, Enter corresponding value :");
//scan choice using scanf
scanf("%d",&suiteChoice);
//quit the game if
if(suiteChoice==5)
break;
if(suiteChoice < 1 || suiteChoice > 5)
{
printf(" Invalid choice ");
break;
}
//if suite matches
if(strcmp(randomSuite,cardSuite[suiteChoice-1])==0)
{
//display 13 cards
for(i=0;i<13;i++)
{
printf("%d. %s ",(i+1),cardValue[i]);
}
//display quit option
printf("14. Quit the game");
printf(" Choose your card, Enter corresponding value :");
//enter choice using scanf
scanf("%d",&cardChoice);
//quit game if
if(cardChoice==14)
break;
if(cardChoice < 1 || cardChoice > 14)
{
printf(" Invalid choice ");
break;
}
//if card matches
if(strcmp(randomCardValue,cardValue[cardChoice-1])==0)
{
//print Congratulations message
printf(" Congratulations, you have won. ");
}
//you loose
else
{
//print loosing message
printf(" You loose");
//print selected suite
printf(" Your selected suite = %s",cardSuite[suiteChoice-1]);
//print random suite
printf(" Random suite = %s",randomSuite);
//print selected card
printf(" Your selected card = %s",cardSuite[cardChoice-1]);
//print random card
printf(" Random Card = %s",randomCardValue);
break;
}
}
//wrong suite selected
else
{
printf(" You have selected wrong suite.");
break;
}
}while(cardChoice!=14 || suiteChoice!=5);
//thanks and inviting message
printf(" Thanks for playing, Try next time");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.