Mastermind求解器猜测问题

时间:2013-09-27 18:38:05

标签: python python-3.x

我需要编写一个mastermind解算器,其中包含带字母A到F的4个字母代码。 然而,我的猜测消除器正在留下本应该被淘汰的猜测。 这是我的猜测消除代码:

def getFeedback(self, feedbackStr): 
    if guesscount == 1:
        import itertools #i know this shouldnt be here but homework comes with limitations
        global guesslist
        guesslist=list("".join(k) for k in itertools.product('ABCDEF', repeat=4))        
    guess_score=feedbackStr
    for i in guesslist:
        i_score=computeFeedback(guess, i)#compares possible code to current guess
        if i_score != guess_score: #remove all non matching guesses
            guesslist.remove(i) 
    return guesslist

这里是computerFeedback代码:

def computeFeedback(code,guess):
    # Given a code and guess computes the feedback string

    b = 0
    w = 0
    inCodeCount = {'A':0,'B':0,'C':0,'D':0, 'E':0, 'F':0}
    inGuessCount = {'A':0,'B':0,'C':0,'D':0, 'E':0, 'F':0}
    for i in range(0,4):
        if code[i] == guess [i]:
            b += 1
        inCodeCount[code[i]] += 1
        inGuessCount[guess[i]] += 1
    for ch in inCodeCount:
        w += min(inCodeCount [ch], inGuessCount [ch])
    w -= b 
    feedback = str(w)+'w'+str(b)+'b'
    return feedback

然而,当代码= BBAA并且第一个猜测是AABB时,例如, guesslist是['BBAA','CCAE','DDBC','EECA','FFCE'] 我的程序应该消除除BBAA之外的所有事情,但事实并非如此。 它消除了1290/1295错误的猜测,但一些错误的猜测似乎总是漏掉。

根据输入的代码,胭脂错误猜测将始终相同。

我逐行完成了它,例如,

  computeFeedback('BBAA','AABB')  <----- this is my guess vs the code

  '4w0b' 
 computeFeedback('AABB','DDBC')   <----- this is my guess vs a
  possible code.

 '0w1b' 

 computeFeedback('AABB','CCAE') 
'1w0b'
  computeFeedback('AABB','EECA') 
 '1w0b' 
 computeFeedback('AABB','FFCE')
 '0w0b'

没有任何反馈字符串匹配,因此它们应该已被删除。 如果这个问题过于具体,我道歉,但我无法弄清楚为什么会发生这种情况。

1 个答案:

答案 0 :(得分:2)

您的问题在于getFeedback功能。

我怀疑,因为你同时迭代并同时修改列表,你的代码表现得很糟糕。如果我将其更改为更具功能性的样式(这样不会修改列表,而是不断返回新的列表),它会按预期工作。

(注意:我从getFeedback中删除了一些代码,因为它使测试有点困难。你说你有不寻常的约束,所以你可能需要改变它以补充猜测代生成代码。将它作为一个单独的函数会更清晰,但我认为这是切向的。)

import itertools

def generateInitialGuesslist():
    return list("".join(k) for k in itertools.product('ABCDEF', repeat=4))

def getFeedback(guess, feedbackStr, guess_list):
    guess_score = feedbackStr
    new_guess_list = []
    for candidate in guess_list:
        candidate_score = computeFeedback(guess, candidate)
        if candidate_score == guess_score:        #remove all non matching guesses
            new_guess_list.append(candidate)
    return new_guess_list

def computeFeedback(code,guess):
    # Given a code and guess computes the feedback string

    b = 0
    w = 0
    inCodeCount = {'A':0,'B':0,'C':0,'D':0, 'E':0, 'F':0}
    inGuessCount = {'A':0,'B':0,'C':0,'D':0, 'E':0, 'F':0}
    for i in range(0,4):
        if code[i] == guess [i]:
            b += 1
        inCodeCount[code[i]] += 1
        inGuessCount[guess[i]] += 1
    for ch in inCodeCount:
        w += min(inCodeCount [ch], inGuessCount [ch])
    w -= b 
    feedback = str(w)+'w'+str(b)+'b'
    return feedback

guesslist = generateInitialGuesslist()
guesslist = getFeedback('AABB', '1w1b', guesslist)
print guesslist
print len(guesslist)

我测试了这一点,首先是AABB作为猜测,ABCD作为正确答案。您的代码声明使用1w1b作为反馈仍然有208个猜测,这与我自己的主谋解决者所说的相同。