2

I'm working onto the Donald Knuth 1977 algorithm for Mastermind (here). I've implemented some steps, but I don't know how to calculate the number of possibilites that would be eliminated for each possible score.

Integer Bulls <- 0
WHILE Bulls <= 4
    Integer Cows <- 0
    WHILE Cows <= 4
        Integer CurrentSetSize <- 0
        WHILE CurrentSetSize <= Set.size
           // Now, I should know if this possibility can be eliminated. But how ?
        END WHILE
    END WHILE
END WHILE

Do you have any way to do this ?

Thanks.

Edit : I did it with C. It isn't Mastermind in reality but Bulls & Cows, but it's the same.

// ASCII-compatible

#define POSSIBILITIES           6720

struct Guess {
    int i[5];
};

struct Answer {
    int bulls;
    int cows;
};

static struct Set {
    struct Guess value;
    int    score;
} *CurrentSet;

void
initSet(void)
{
    for (int i = 'a'; i <= 'h'; ++i) {
        for (int j = 'a'; j <= 'h'; ++j) {
            if (j == i) continue;
            for (int k = 'a'; k <= 'h'; ++k) {
                if (k == j || k == i) continue;
                for (int l = 'a'; l <= 'h'; ++l) {
                    if (l == k || l == j || l == i) continue;
                    for (int m = 'a'; m <= 'h'; ++m) {
                        if (m == l || m == k || m == j || m == i) continue;
                        CurrentSet->value.i[0] = i;
                        CurrentSet->value.i[1] = j;
                        CurrentSet->value.i[2] = k;
                        CurrentSet->value.i[3] = l;
                        CurrentSet->value.i[4] = m;
                        CurrentSet->score    = 0;
                    }
                }
            }
        }
    }
}

void
computeScore(int index)
{
    struct answer p;

    for (p.bulls = 0; p.bulls <= 4; ++p.bulls) {
        for (p.cows = 0; p.cows <= 4; ++p.cows) {
            for (int i = 0; i < POSSIBILITIES; ++i) {

            }
        }
    }
}

void
updateSet(void)
{
    for (int i = 0; i < POSSIBILITIES; ++i)
        computeScore(i);
}
md5
  • 23,373
  • 3
  • 44
  • 93
  • Whatever language that is, it isn't C. As for the algorithm, you haven't even done the first step of creating the initial set of possibilities. Once you do that, you ought to be able to figure out how to eliminate the ones that don't match. – Jim Balter Jun 28 '12 at 09:34
  • I did it with C programming language. I'm editing my post. – md5 Jun 28 '12 at 09:36
  • 1
    You need to actually read the algorithm given: First create a set of all the combinations. Then guess "aabb". Then get the response from the opponent. Then eliminate the combinations that don't fit that response -- that's steps 1 and 2. – Jim Balter Jun 28 '12 at 09:44
  • I didn't know how to determine « the combinations that don't fit that response ». But now, I understand. I apologize for the inconvenience... – md5 Jun 28 '12 at 09:51
  • No problem and no need to apologize; you came here to learn and you did. – Jim Balter Jun 28 '12 at 09:54
  • 1
    BTW, with five letter words, you'll need some different initial guess from "aabb", and which one is optimal isn't obvious, but you can calculate it using the same minimax method of step 3: enumerate all possible guesses, and for each of those enumerate all possible cow/bull responses, and for each of those take the min of the number of words that would be eliminated. Any guess that yields the highest min can be used as an initial guess. – Jim Balter Jun 28 '12 at 10:04
  • OK, thanks, I'm going to think about it. – md5 Jun 28 '12 at 10:27

0 Answers0