Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help writing the code for a \"Code breaker\" game in C. OBJECTIVE : The p

ID: 3848388 • Letter: I

Question

I need help writing the code for a "Code breaker" game in C.

OBJECTIVE : The player must guess the computer's secret code by making guesses and acting on clues the computer gives in response to each guess. To win, the player must guess the secret code within a certain number of attempts.
The secret code will be a string of 3 to 8 characters, and the player will get to choose how many. The longer the code, the more difficult the challenge.

There are a few more requirements but to start this should help!

Thank you in advance!

Explanation / Answer

#include<stdio.h>
#include<stdlib.h>
//main function definition
int main()
{
int c, d, attempt, size;
//To store secret code generated for computer
char secretCode[9];
//To store secret code entered by the use
char userCode[9];
//Accept the size of the secret code to be generated
printf(" To generate secrete code, enter the size: ");
scanf("%d", &size);
//Loops till the size entered by the user
for(c = 0; c < size; c++)
{
//Generates a random character (between A to Z)
secretCode[c] = 'A' + (rand() % 26);
}//End of for loop
//Stores null character
secretCode[c] = '';
printf("%s", secretCode);
//Asks the user to guess the secret code
printf(" Guess the secret code within 4 attempts [Use Capital letters only]");
//Loops 4 times
for(c = 0, d = 0; c < 4; c++)
{
//Accept the secret code from the user
printf(" Enter the secrete code of size %d: ", size);
scanf("%s", userCode);
//Compares the secret code entered by the user with computer secret code
//If the result is zero it is matching
if(strcmp(userCode, secretCode) == 0)
{

printf(" Right Secret Code [Number of attempt = %d]", c+1);

break;

}//End of if
//Loop till the size of the secret code
while(d < size)
{
//Compares each character of user entered secret code with computer secret code
//If it is not matching
if(secretCode[d] != userCode[d])
{
//Display the character which is invalid
printf(" Invalid character %c ", userCode[d]);
printf(" Try again!");
//Come out of the loop
break;
}//End of if
//Increase the counter by one
d++;
}//End of while
}//End of for
return 0;
}//End of main

Sample Run 1:

To generate secrete code, enter the size: 4
PHQG
Guess the secret code within 4 attempts [Use Capital letters only]
Enter the secrete code of size 4: MHTR

Invalid character M
Try again!
Enter the secrete code of size 4: PHTR

Invalid character T
Try again!
Enter the secrete code of size 4: PHQR

Invalid character R
Try again!
Enter the secrete code of size 4: PHQS

Invalid character S
Try again!

Sample Run 2:


To generate secrete code, enter the size: 6
PHQGHU
Guess the secret code within 4 attempts [Use Capital letters only]
Enter the secrete code of size 6: PHQTHR

Invalid character T
Try again!
Enter the secrete code of size 6: PHQGHR

Invalid character R
Try again!
Enter the secrete code of size 6: PHQGHU

Right Secret Code [Number of attempt = 3]