0

I am trying to implement Knuths Mastermind Algorithm in java. The algorithm is explained in this paper, paper. This is how I have implemented it so far but it takes more than the maximum 5 guesses than the algorithm should take.

    private static void makeGuess() {
    attempts++;
    /*Secret is the code that we are trying to guess, posGuess is a list of
     *  all possible guesses and guesses is a list of all remaining guesses*/
    if(attempts == 1){/*if first guess, guess BBGG*/
        currentGuess = "BBGG";
    }else{
    for (int i = 0; i < guesses.size(); i++) { /*Remove all guesses with a score that isn't the same as the current score*/
        if (getScore(currentGuess, secret) != getScore(currentGuess,
                    guesses.get(i))) {
                guesses.remove(currentGuess);
                guesses.remove(i);
            }
    }
    if(guesses.size() == 1){ /*If one guess is remaining then make this the new guess*/
        currentGuess = guesses.get(0);
    }else{
    /*
     * For each guess scored against each other guess find the score and the score array. 1 white marker gives score of 1, 1 black gives score of 2, etc. 
     * Then if the maximum of the current score list is greater than previous maximums set new max and set current guess
     */
    for (int i = 0; i < posGuesses.size(); i++) {
        int scores[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
        for (int j = 0; j < guesses.size(); j++) {
            scores[getScore(posGuesses.get(i), guesses.get(j))] ++;
        }
            if(getMax(scores,scores.length) < max){
                max = getMax(scores,scores.length);
                System.out.println("Max: " + max);
                currentGuess = guesses.get(i);
            }
    }
    }
    }
    guesses.remove(currentGuess);

}

Assuming the other methods, e.g scoring method and getMax method, work as intended why would this not work?

Jason C
  • 38,729
  • 14
  • 126
  • 182
Virus7711
  • 27
  • 1
  • 5
  • Perhaps you could download some code of an algorithm that does work and debug through it and check the differences with your implementation. – Sebastiaan van den Broek Nov 01 '13 at 23:16
  • Be more specific. For what input will it take more than 5 guesses? What are these guesses? How do they agree with or differ from Fig. 1 in the paper you link? Speaking of which, I see that Fig. 1 as the core of that paper, but I don't see it reflected in your implementation. How come? In what way do you intend to implement that algorithm, if you don't encode Fig. 1 more or less as it appears in that paper? – MvG Nov 01 '13 at 23:55
  • http://code.google.com/p/mastermind-strategy/source/browse/trunk/lib/OptimalCodeBreaker.cpp appears to have an implementation of that optimal strategy which you might use for comparison. That repo also has [the Text of Fig. 1](http://code.google.com/p/mastermind-strategy/source/browse/trunk/strats/p4c6r-knuth.txt?r=109) in case you want to process that using some automated tool. – MvG Nov 02 '13 at 00:12

0 Answers0