Question: The code breaker's program (codeBreakerSimple.c) is very simple-minded. You need to improve the codes to include the following logic: - The code breaker should NOT

The code breaker's program (codeBreakerSimple.c) is very simple-minded. You need to improve the codes to include the following logic:

- The code breaker should NOT submit a guess that is the same as a previous guess. That is, it should maintain a history of the guesses made so far. If a guess is not "new", it should generate another guess instead.

- The code breaker should check to see if the new guess is consistent with all the previous replies from the code maker. For example, if the reply to the guess

2 4 3 1

is (0, 0). In that case, it can be concluded that the correct guess should not include colors 1, 2, 3 or 4. Therefore, the new guess should not be, say,

0 5 0 2

as it is wrong to include color 2.

- You can include other logic to make your code breaker program more "smart".

codeBreakerSimple.c (Code to be editted) :

#include

#include

#include

int main(int argc, char *argv[])

{

char line[128];

int gs1, gs2, gs3, gs4;

int a1, a2;

FILE *logf;

logf = fopen("codeBreaker.log","w");

srand(0);

do {

gs1=rand()%6; gs2=rand()%6; gs3=rand()%6; gs4=rand()%6;

fprintf(logf, "write to controller: %d %d %d %d ", gs1, gs2, gs3,

gs4);

printf( "%d %d %d %d ", gs1, gs2, gs3,

gs4);

fflush(stdout);

if (fgets(line,sizeof(line),stdin) < 0)

{

fprintf(logf,"End of input reached ");

fclose(logf);

return;

}

if (sscanf(line,"%d%d", &a1, &a2) != 2)

{

fprintf(logf,"error: couldn't extract all 2 values! ");

return;

}

fprintf(logf, "read from controller: %d %d ",a1,a2);

} while (a1 < 4);

fprintf(logf,"Done! ");

close(logf);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!