his assignment you are going to write a guessing game. The computer will randoml
ID: 3870190 • Letter: H
Question
his assignment you are going to write a guessing game. The computer will randomly guess a letter between a and z and the user e will type in characters guessing which letter the computer picked. Example Game Play: * Welcome to Midterm Lab 1 - Guess the Letter Game Written by M. Theys * The computer has chosen a letter. * Please guess which letter you think the computer has chosen: 1 Your guess of 1 is not the letter the computer chose, and the letter the computer chose is closer to z. Please guess which letter you think the computer has chosen: Your guess of w is not the letter the computer chose, and the letter the computer chose is closer to a. * Please guess which letter you think the computer has chosen: t * Congratulations!!! You have guessed the letter the computer has chosen. * Thanks for playing, would you like to play again (Y/N)? N GoodBye If the user chose Y at the end the game would restart and pick a new letter * for the user to guess. GRADING - 10 points header in code with student information (name, lab, ..) - 10 points comments in code detailing what you did 10 points test cases showing your code is working 15 points prompts printing correctly * 15 points getting input from user correctly - 20 points random character creation done correctly (different letter each , letten between a-z time game run, different letter if play again " 20 points able to determine whether letter computer has is closer to z or a from users guessExplanation / Answer
Hi, here is the code you can use, i have added comments so that you can understand,
#include <stdio.h>
int main()
{
char user;
char computer;
/* please add print statements here for your studentId*/
int given[26]={0};//for setting used characters
do
{
computer = 'a' + (random() % 26);//random char between a-z
if(given[(int)computer]==1)
{
computer = 'a' + (random() % 26);//since already chosen
given[(int)computer]=1;
}
printf("please guess which letter computer has chosen : ");
scanf("%c",&user); //get the char from user
if(user<computer) //check where the user char lies closer to
{
printf("your guess %c is not correct and the letter computer choose is closer to z: ",user);
continue;
}
else if(user>computer)
{
printf("your guess %c is not correct and the letter computer choose is closer to a: ",user);
continue;
}
else //if matched, prompt to play again
{
printf("Congrats! you have chosen the letter correctly ");
printf("Thanks for playing, would you like to play again? Y:N ");
}
}while(user!='N'); //if N entered then break
return 0;
}
Thumbs up if this was helpful, otherwise let me know in comments
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.