Question

I am trying to implement an algorithm for guessing secrets in at the most 5 guesses in Mastermind. This is a class assignment and I am, in general, somewhat new to programming (understatement), so please have patience with any obvious things I might've missed. NOTE: the only library I'm allowed to use is <stdio.h>.

For starters the code:

#include <stdio.h>

#define feeddef() \
    feed[0] = '0'; \
    feed[1] = ' '; \
    feed[2] = 'B'; \
    feed[3] = ' '; \
    feed[4] = '0'; \
    feed[5] = ' '; \
    feed[6] = 'C';

int secret[4];

void initSet(int (*gs)[4],int *gsGd,int *guess);
void goodGuess(int (*gs)[4],int *gsGd,char fb[20],int *guess);
void feedback(int *gues,char feed[20]);

void feedback(int *gues,char feed[20])
{
    int i,j,temp[4], temp2[4];
    feeddef();
    for(i=0;i<4;i++)
    {
        temp[i] = gues[i];
        temp2[i] = secret[i];
    }
    for(i = 0;i<4;i++)
        if(temp[i] == temp2[i])
        {
            feed[0]++;
            temp[i] = 7;
            temp2[i] = 8;
        }
    for(i=0;i<4;i++)
        for(j=0;j<4;j++)
            if(temp[i] == temp2[j])
            {
                feed[4]++;
                temp[i] = 9;
                temp2[j] = 10;
            }
}

void initSet(int (*gs)[4],int *gsGd,int *guess)
{
    int i;

    for(i=0;i<1296;i++)
    {
        gs[i][3] = i%6;
        gs[i][2] = (i/6)%6;
        gs[i][1] = ((i/6)/6)%6;
        gs[i][0] = (((i/6)/6)/6)%6;
    }
    for(i=0;i<4;i++)
        guess[i] = (i+2)/2;
}

void goodGuess(int (*gs)[4],int *gsGd,char fb[20],int *guess)
{
    int i,j,count[1296],max_count = 0,max_index;
    char fbck[1296][2];
    char feed[20];
    for(i=0;i<1296;i++)
    {
        printf("Guess #%d in set: ",i);
        feedback(gs[i],feed);
        if(feed[0] != fb[0] || feed[4] != fb[4])
        {
            printf("Not matching\n");
            gsGd[i] = 0;
        }
        fbck[i][0] = feed[0];
        fbck[i][1] = feed[4];
    }
    printf("minimax start---\n");
    for(i=0;i<1296;i++)
    {
        count[i] = 0;
        for(j=0;j<1296;j++)
        {
            if(gsGd[j] == 0)
                continue;
            else
            {
                if(fbck[i][0] != fbck[j][0] || fbck[i][1] != fbck[j][1])
                    count[i]++;
            }
        }
    }
    printf("---minimax end\n");
    for(i=0;i<1296;i++)
        printf("Index at %d cancels out %d indices.\n",i,count[i]);
    for(i=0;i<1296;i++)
    {
        if(max_count < count[i])
        {
            printf("New max at index: %d\n",i);
            max_count = count[i];
            max_index = i;
        }
    }
    printf("New guess is: ");
    for(i=0;i<1296;i++)
    {
        if(fbck[i][0] != fbck[max_index][0] || fbck[i][1] != fbck[max_index][1])
            gsGd[i] = 0;
    }
    for(i=0;i<4;i++)
    {
        guess[i] = gs[max_index][i];
        printf("%d",guess[i]);
    }
    printf("\n");
}

int main()
{
    int gs[1296][4],gsGd[1296],guess[4],i,count = 0;
    char fback[20];
    while(1)
    {
        for(i=0;i<4;i++)
            scanf("%d",&secret[i]);
        initSet(gs,gsGd,guess);
        do
        {
            count++;
            printf("Goodguess %d",count);

            feedback(guess,fback);
            goodGuess(gs,gsGd,fback,guess);
        }while(fback[0] != '4');
        printf("Solved in %d guesses\n",count);
    }
    return 0;
}

For some reason on the second iteration of the goodGuess function I get an access violation error at this line:

if(fbck[i][0] != fbck[max_index][0] || fbck[i][1] != fbck[max_index][1])

Error:

First-chance exception at 0x012643a6 in Mastermind.exe: 0xC0000005: Access violation reading location 0x99b00cc0.
Unhandled exception at 0x012643a6 in Mastermind.exe: 0xC0000005: Access violation reading location 0x99b00cc0.

As I understand it, 0xC0000005 being close to null means it probably is trying to access null, but I have no idea what causes it. It works just fine the first time over.

I have a feeling that I missed something vital, but I'll be damned if I know what it is.

Was it helpful?

Solution

My guess is that max_index is uninitialized.

The code does not assign a value when you create max_index. You only assign a value inside an if statement. So on the first iteration, the if statement finds a true and you get a value to max_index. On the second iteration, it doesn't find a true, and max_index is unintialized.

One thing I'd suggest in cases like this - you have a fairly complex statement. So it's nice to see which one of the four fails. So just create four little do nothing statements - if (fbck[i][0] == 1) printf("hi") for example.

That lets you narrow down to which part of that statement fails. It also helps to print out the value of each variable in a run - then you can run the code, look at the last line that printed, and you'll be able to see what i and max_index were and that should be a huge clue about what's happening.

And as you learn more about the debugger and conditional breakpoints, you'll find better ways than printing variables!

OTHER TIPS

if(fbck[i][0] != fbck[max_index][0] || fbck[i][1] != fbck[max_index][1])

max_index may be used uninitialized in the above expression.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top