0

JavaでKnuths Mastermind Algorithmを実装しようとしています。このアルゴリズムについては、この論文 paper で説明されています。これは私がこれまでに実装した方法ですが、アルゴリズムが必要とするよりも最大5回以上の推測が必要です。

    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);

}

スコアリング メソッドや getMax メソッドなどの他のメソッドが意図したとおりに機能すると仮定すると、なぜこれが機能しないのでしょうか?

4

0 に答える 0