我正在尝试实施Donald Knuth的算法来解决Mastermind,详见此处:http://en.wikipedia.org/wiki/Mastermind_(board_game)#Five-guess_algorithm
我的大多数代码都在运行,但我无法使Minimax步骤工作(在ComputerGuesser
类的class Feedback(object):
def __init__(self, number_in_correct_place, number_in_incorrect_place):
self.number_in_correct_place = number_in_correct_place
self.number_in_incorrect_place = number_in_incorrect_place
def __str__(self):
return (self.number_in_correct_place, self.number_in_incorrect_place).__str__()
def __eq__(self, other):
return self.number_in_correct_place == other.number_in_correct_place and self.number_in_incorrect_place == other.number_in_incorrect_place
def __hash__(self):
prime1 = 31
prime2 = 17
return (prime1 * self.number_in_correct_place) + (prime2 * self.number_in_incorrect_place)
方法中)。我已经看过Mastermind minimax algorithm,但我无法弄清楚我做错了什么。
帮助将不胜感激,谢谢。
def generate_all_possibilities(code_length, number_of_colours):
return [p for p in itertools.product(range(1, number_of_colours + 1), repeat=code_length)]
def get_feedback(code, guess):
assert len(guess) == len(code)
number_in_correct_place = 0
number_in_incorrect_place = 0
matched = [False] * len(code)
for i in range(len(code)):
if guess[i] == code[i]:
number_in_correct_place += 1
matched[i] = True
for i in range(len(code)):
if guess[i] != code[i] and guess[i] in code:
for j in range(len(code)):
if guess[i] == code[j] and not matched[j]:
number_in_incorrect_place += 1
matched[j] = True
break
return Feedback(number_in_correct_place, number_in_incorrect_place)
def filter_possibilities(possibilities, feedback, guess):
return [p for p in possibilities if feedback == get_feedback(guess, p)]
from collections import Counter
from guesser import Guesser
from utils import generate_all_possibilities, filter_possibilities, get_feedback
class ComputerGuesser(Guesser):
def __init__(self, code_length, number_of_colours):
super(ComputerGuesser, self).__init__(code_length, number_of_colours)
self.all_possibilities = generate_all_possibilities(code_length, number_of_colours)
self.filtered_possibilities = generate_all_possibilities(code_length, number_of_colours)
def alert_feedback(self, feedback, guess):
self.filtered_possibilities = filter_possibilities(self.filtered_possibilities, feedback, guess)
def get_guess(self):
key = lambda g: max(Counter(get_feedback(g, c) for c in self.filtered_possibilities).values())
guess = min(self.all_possibilities, key=key)
return guess
Code: [3, 1, 4, 3]
12 turns remaining
Guess: (1, 1, 2, 2) Feedback: (1, 0)
Number of possibilities left: 256
11 turns remaining
Guess: (1, 3, 4, 4) Feedback: (1, 2)
Number of possibilities left: 21
10 turns remaining
Guess: (3, 1, 4, 5) Feedback: (3, 0)
Number of possibilities left: 3
9 turns remaining
Guess: (1, 1, 1, 3) Feedback: (2, 0)
Number of possibilities left: 1
8 turns remaining
Guess: (1, 1, 1, 1) Feedback: (1, 0)
Number of possibilities left: 1
7 turns remaining
Guess: (1, 1, 1, 1) Feedback: (1, 0)
Number of possibilities left: 1
6 turns remaining
Guess: (1, 1, 1, 1) Feedback: (1, 0)
Number of possibilities left: 1
5 turns remaining
Guess: (1, 1, 1, 1) Feedback: (1, 0)
Number of possibilities left: 1
4 turns remaining
Guess: (1, 1, 1, 1) Feedback: (1, 0)
Number of possibilities left: 1
3 turns remaining
Guess: (1, 1, 1, 1) Feedback: (1, 0)
Number of possibilities left: 1
2 turns remaining
Guess: (1, 1, 1, 1) Feedback: (1, 0)
Number of possibilities left: 1
1 turns remaining
Guess: (1, 1, 1, 1) Feedback: (1, 0)
Number of possibilities left: 1
编辑:运行代码(带有一些添加的打印语句)会产生以下结果:
.hiddenMenu {
display: none;
height: 100%;
left: 0;
position: fixed;
top : 0;
width: 100%;
z-index: 10;
overflow:scroll;
}