简体   繁体   中英

Mastermind game in C(with words)

I am probably not going to get help here because my question is far from being specific (I don't even know what exactly wrong with it) but, according to my professor's tests, there is something wrong with it (wrong in terms of correctness - it doesn't provide correct number of direct and indirect matches) (I have no access to his tests). As far as I have been testing, it passes all of my tests. However, there are over a couple hundred million possible outcomes (I think) and I can't test them all because I don't know how to do automated testing...

Here is my code that performs the "logic" part of the game called mastermind, which is compares a string of randomly generated letter (8 max) with user input string (a guess). I wanted to see if anyone has encountered this game in the past and knows the logic of how it supposed to compare two strings and generate the correct number of exact and inexact guesses.

// userInput->position - a length of a string(max 8) 
// userInput->code - randomly generated code
// userInput->arr  - user input string
 void checkForExactMatch(Data* userInput) {
    int i;

    for (i = 0; i < userInput->position; i++) {
       if (userInput->code[i] == userInput->arr[i]) {
          userInput->exactMatch++;
          userInput->arr[i] = 'a';
       }
       else
          checkForInExactMatch(userInput, i);
    }
 }

 void checkForInExactMatch(Data* userInput, int i) {
    int j;

    for (j = 0; j < userInput->position; j++) {
       if (userInput->arr[j] == userInput->code[i]) {
          userInput->arr[j] = 'a';
          userInput->inExactMatch++;
          break;
       }
    }
 }

Looking over your code there were a couple of observations to be made. First in your for checkForExactMatch() the call to checkForInExactMatch is inside your for loop. So on the first mismatch you call checkForInExactMatch and when you return from checkForInExactMatch -- you call it again on the next iteration unless your first mismatch just happens to be on the final character.

To address that issue, you should fully determine whether you have an exact match or not, completing the for loop before checkForInExactMatch is called.

In your checkForInExactMatch , you have to decide whether a single common-character or some minimum length substring constitutes an inexact match.

It sounds like you have things worked out, and good job for pushing through to a solution. Depending on how you approached it, keeping a simple flag in checkForExactMatch() such as int matched = 1; and then loop turning your test around

    for (i = 0; i < userInput->position; i++)
        if (userInput->code[i] != userInput->arr[i]) {
            matched = 0;
            break;
        }

Then it's just a simple test of

    if (matched) {
        userInput->exactMatch++;
        userInput->arr[i] = 'a';
    }
    else
        checkForInExactMatch(userInput, i);

So long as what you have done accomplishes something similar, you are fine. Let me know if you have further questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM