0

Hello I'm working on a version of Mastermind and I'm almost done. I tried implementing a "debug mode" where it would show the answer if a flag is entered as input. My implementation has gone very wrong as it's stuck in an infinite loop. The problem is in the function getGuess. any suggestions on a fix or alternate solution will be greatly appreciated

I updated the code with suggestions from you great people. but now upon executing I run into a segmentation error 11.

#include<stdio.h>
 #include<math.h>
 #include<stdlib.h>
 #include<time.h>
 #define CODELENGTH 4
 #define NUMSYMBOLS 6




void genCode (int MasterCode[])
{
int i=0;
int k;
while (i < CODELENGTH){

MasterCode[i] =rand() %NUMSYMBOLS +1;
    i++;

}//end while loop.
for ( k = 0 ; k < 4; k++ ) {
    printf( "%d ", MasterCode[ k ] );
}

printf( "\n" );
}


void printMasterCode(int MasterCode[]){
int k;
for ( k = 0 ; k < 4; k++ ) {
    printf( "%d ", MasterCode[ k ] );
    printf("\n");
}
}



void getGuess (int guess[], int argc, char **argv, int MasterCode[])
{

int number = 0;
int j;
int k;
printf( "Please enter your list of 4 numbers between 1 and 6: " );


    if (strcmp(argv[1], "-b") == 0) {
        printf("Flag -b passed\n");
        printMasterCode(MasterCode);

    }


for ( j = 0 ; j < 4; j++ ) {
    scanf( "%d", &number );
    guess[ j ] = number;
}

printf( "The guess of " );

for ( k = 0 ; k < 4; k++ ) {
    printf( "%d ", guess[ k ] );
}

printf( "\n" );
}


int main (int argc, char **argv)
{
srand ( time(NULL) );
int MasterCode[4];
int guess[ 4 ];
int exactMatch;
int closeMatch=0;
int exactResult;
int closeResult = 0;

genCode(MasterCode);
do {
    getGuess(guess, argc, argv, MasterCode);
    exactResult = checkExactMatches(MasterCode, guess, exactMatch);
    closeResult = checkCloseMatches(MasterCode, guess, closeMatch);
    printf("%d = Ending exactMatches \n", exactResult);
    printf("%d  = Ending closeMatches \n", closeResult);

} while (exactResult != CODELENGTH);






}
int checkExactMatches (int MasterCode[], int guess[], int exactMatch )
{
int woot;
for(woot=0; woot<4; woot++){


        if (MasterCode[woot] == guess[woot]){
            printf("Exact Match found \n");
            exactMatch ++;
            printf( "%d = Guess \n" , guess[ woot ]);
            printf( "%d = MasterCode \n", MasterCode[ woot ]);
            printf("%d = exactMatch \n", exactMatch);

        }// end if

        if (MasterCode[woot] != guess[woot])
            printf("No EXACT match \n");


}//end for loop

return exactMatch;
} // end checkExactMatches



int checkCloseMatches (int MasterCode[], int guess[], int closeMatch )
{
int k;
int j;
for(k=0; k<4; k++){

    for (j=0; j<4; j++) {


        if (MasterCode[j] == guess[k]){
    printf("CLOSE Match found \n");
    closeMatch ++;
    printf( "%d = Guess \n" , guess[ j ]);
    printf( "%d = MasterCode \n \n", MasterCode[ k ]);
    printf("%d = closeMatch \n \n", closeMatch);

}// end if

if (MasterCode[j] != guess[k])
    printf("No CLOSE match \n");


    }//end nested for loop
}//end for loop

return closeMatch;
} // end checkCloseMatches
4

2 回答 2

1

这不是传统意义上的无限循环。您的程序要求输入第五个数字。
参数“-b”也应该在 argv[1] 中。
argv[0] 将包含程序名称。

当没有命令行参数时,段错误现在是由于 argv[1] 为 NULL。
所以应该检查

if (argc>1 && strcmp(argv[1],"-b")==0)
于 2012-10-09T04:40:23.897 回答
1

您#define CODELENGTH,然后在整个代码中使用幻数(某些地方为 4,其他地方为 5)。使用#defined 值,您将不再有无限循环(您需要输入 5 个数字才能工作)。

正如其他人所说: argv[0] 包含程序运行时的名称(或者来自 Ed S. 的更多细节 - 也许它没有)。您要查看的参数是 argv[1]。但请确保首先测试 argc,因为 argv[1] 不能保证存在!(如果 argc 为 0,则 argv[0] 也不是)。

于 2012-10-09T04:53:30.587 回答