Program Specifications
Your program will play the mastermind game as follows:
1. Prompt for the 1/2/3/4/5/6/7/8 digit key we will play with. No error checking here.
2. Prompt the user for a guess.
1. we check to see if the guess if exactly length 4
2. we check to see if the guess contains all numbers
3. we check to see if the guess has no repeats
any violation of these rules prints an error message and prompts for a guess again. Bad input does not count as a guess.
1. End the game if:
1. the guess is correct. Print out a winner message
2. it was the last guess (the twelth) and the guess is incorrect. Print out a loser message and the value of the key
2. If no end condition occurs, print out how many of the guess numbers are exactly right (correct number in the correct position) and how many of the guess numbers are correct but not in the correct position. Example. The key is 1234 and the guess is 4256. The feedback would be 1 number exactly correct (2) and 1 number in the wrong position (4). That is, each digit in the guess produces at most one result (exact or wrongPosition).
3. You should keep a history of guesses so that the user can see how they are progressing in a file.
4. Continue the game until an end condition occurs
Above is the program and below is my coding I need to fix but couldnt so I heard that this website is really helpful can anybody help me "I am a real life nob in C programming" and THANKS
Code:
#include <stdio.h>
#include <string.h>
int main()
{
char master[8]={0x00};
char guess[8]={0x00};
int guesses, broken, match, almost, i, n;
do {
printf("Please enter your master key :");
fgets(master,8,stdin);
} while ( strlen(master)==0 );
for( guesses=1; guesses<13; guesses++ )
{
printf("Please enter your guess :");
fgets(guess, 8,stdin);
match = 0;
almost = 0;
for( n=0; n<strlen(guess); n++ )
{
for( i=0; i<strlen(master); i++)
{
if( guess[n]==master[i] )
{
if( n==i )
match++;
else
almost++;
}
}
}
if( match==strlen(master) ) {
broken = 1;
break;
}
else {
printf(" Matches = %d, Almost = %d\n", (match-1), almost );
}
}
if( broken ) {
printf("End of game .. you took %d guesses \n",guesses);
}
else {
puts(" Try again...\n");
}
return 0;
}