Trouble applying knuth's method on a python MasterMind code breaker

34 views Asked by At

I am currently working on my own code breaker for the boardgame MasterMind in python. (If you are not aware of how it is played and want to provide me help, the link of the rules and algorithm solver are here (https://en.wikipedia.org/wiki/Mastermind_(board_game)).

I am stuck on step 6 of the code breaker:

"The next guess is chosen by the minimax technique, which chooses a guess that has the least worst response score. In this case, a response to a guess is some number of colored and white key pegs, and the score of such a response is defined to be the number of codes in S that are still possible even after the response is known. The score of a guess is pessimistically defined to be the worst (maximum) of all its response scores.

From the set of guesses with the best (minimum) guess score, select one as the next guess, choosing a code from S whenever possible. (Within these constraints, Knuth follows the convention of choosing the guess with the least numeric value; e.g., 2345 is lower than 3456. Knuth also gives an example showing that in some cases no code from S will be among the best scoring guesses and thus the guess cannot win on the next turn, yet will be necessary to assure a win in five.)"

My issue is that I'm not able to create that part of the code-breaker. When calculating the array of the "best possible combinations" which are supposed to greatly reduce the combinations remaining and declares Knuth "the ability to beat the game in 5 tries or less", my code is not capable of doing so (The following code is just the application of step 6). I would like to now if i made a mistake, because it often returns combination options that are not in the set_S (too often). This provokes that if there are only three combinations left, there are 176 "best combinations" which really aren't because none of them are the three combinations left! This happened with the secret code (4, 5, 5, 4) And had the sequence: (1, 1, 2, 2), (3, 3, 4, 4), (1, 4, 4, 5). There are also games where from the very first set of "best combinations" none of them are in the set_S and prove to be useless.

def choose_next_guess(remaining_combinations, set_S):
    conn = sqlite3.connect('SolverMM.sqlite3')
    cur = conn.cursor()
    cur.executescript(
        """DROP TABLE IF EXISTS solver;
        CREATE TABLE solver(ID INTEGER PRIMARY KEY AUTOINCREMENT, combination TEXT, maximum INTEGER);""")
    # All possible feedbacks
    feedbacks = [(4, 0, 0), (3, 0, 1), (2, 2, 0), (2, 1, 1), (2, 0, 2), (1, 3, 0), (1, 2, 1), (1, 0, 3), 
        (1, 0, 3),(0, 4, 0), (0, 3, 1), (0, 2, 2), (0, 0, 4), (0, 0, 4)]

    for combination in remaining_combinations:
        max_remaining = 0
        for feedback in feedbacks:
            # evaluation method returns quantity of remaining possible combinations (returns an integer)
            set_S = evaluate(feedback, combination)
            # Storing worst-case-scenario (the remaining combinations that a feedback provided with a guess)
            if set_S > max_remaining:
                max_remaining = set_S
        cur.execute("INSERT INTO solver (combination, maximum) VALUES (?, ?);", (','.join(map(str, combination)),   max_remaining))

    conn.commit()
    cur.execute("SELECT combination FROM solver WHERE maximum = (SELECT MIN(maximum) FROM solver);")
    best_combination = cur.fetchall()
    best_combination_copy = best_combination.copy()
    return best_combination

Nonetheless, after thinking a bit about it I've reached to the following conclusion:

The minimax technique in MasterMind is not guaranteed to succeed; because for example imagine you have a guess (1,1,2,3) with a score of 30 and a guess (1,4,4,5) with a score of 25. Even though 25 should be the option because it is the best "worst option", the algorithm is not taking into account how likely it is to happen. For instance, a score A might be "worse" than a score B but have little probability to occur while a good option might not occur. The probability of getting a feedback I understand to be different from one another. This means the algorithm could randomly fail and it be the cause of all my confusion; yet, that would not explain how Knuth argues that the algorithm consistently breaks the code in 5 tries. Anyways, i hope all the previous is enough to get my answer, answered. Thx!

0

There are 0 answers