【问题标题】:MasterMind Game Python CodeMasterMind 游戏 Python 代码
【发布时间】:2014-12-14 20:50:07
【问题描述】:
import random 
def create_code(characters,length):
    """(str,int) -> list
    Return a list of length size of single character of the characters in the given str
    >>>create_code('ygbopr')
    ['g','b','r','b']
    """
    characters = 'grbyop'
    length = 4 
    return list(random.sample(characters,length))
    pass

def find_fully_correct(answer, guess):
    """(list,list) -> list
    Return a list containing a 'b' for each correctly positioned color in the guess
    >>>find_fully_correct(['g','b','r','b'], ['g','b','r','b'])
    ['b', 'b', 'b', 'b']
    """
    res= []
    for x, y in  zip(guess, answer):
            if x == y:
                res.append("b")
    return res if res else None
    pass

def remove_fully_correct(answer, guess):
    """(list,list) -> list 
    Return a list that removes the chars from the first list that are the same and in the same postion in the second list
    >>>remove_fully_correct(['a','b','c','d'], ['d','b','a','d'])
    ['a','c']
    """
    res= answer
    for x,y in zip(answer, guess):
        if x == y:
            res.remove(x)
    list3 = remove_fully_correct(guess,answer)
    list4 = remove_fully_correct(answer,guess)

    for char in list4:
        if char in list3:
            return res
        pass

def find_colour_correct(answer, guess):
    """(list,list) -> list
    Return a list of 'w''s where the number of 'w''s is qual to the number of strs in the second list that have the same value as the str in the first list but a different position
    >>>find_colour_correct(['y','n','g','g'], ['g','n','o','r'])
    ['w']
    """
    res = []
    for str in guess:
        if str in answer:
            res.append("w")   
    return res
    pass

def print_game(guesses, clues):
    """(list,list) -> display
    Print to display headers, Guess and Clue, with the corresponding sublists of the given lists
    >>>print_game(guesses,clues)
    Guesses     Clues
    o o o o     b
    r r r r     b b
    """
    print ('Guesses \t Clues')

    guess = ''
    clue = ''
    guesses = guess_list 
    clues = clue_list
    clue_list = (find_fully_correct, find_correct,colour)

    for guess_list in guesses: 
        for index in range(len(guess_list)):
        guess = guess + ' ' + guess_list[index]
        guess = guess.lstrip()

    for clue_list in clues:
        for char in range (len(clue_list)): 
            clue = clue + ' ' + clue_list[char]
            clue = clue.lstrip()

        print (guess + ' \t ' + clue + '\n')
        pass

def valid(user_guess, valid_chars, size):

    if len(user_guess) != size or user_guess not in valid_chars:
        return False
    else:
        return True
    pass

if __name__ == '__main__':
    size = 4
    tries = 10
    valid_chars= 'grbyop'

尝试编写涉及创建隐藏颜色组合或代码的 Mastermind 游戏 - 由程序从 6 种颜色中选择的 4 种颜色代码组成。用户是密码破解者,并试图通过猜测密码来找出密码。然后程序通过告诉第二个玩家在他们猜测的颜色代码中有多少正确定位以及猜测有多少正确颜色但定位不正确的颜色来给出猜测的正确性,直到用户猜出实际颜色代码或运行在他们的 10 次尝试中。

我在使用 print_game 函数和有效函数时遇到问题

此外,我不确定如何在 if__name__ == 'ma​​in' 部分之后命令实际程序像游戏一样运行。就像调用实际函数向用户发出命令以查看他们的进度一样。

非常感谢任何帮助。

【问题讨论】:

    标签: python for-loop boolean


    【解决方案1】:

    如果要让用户输入,可以在 Python 2.X 中使用 raw_input,在 Python 3 中使用 input。 为了测试你的代码,我会从那开始。

    valid 在我看来在概念上是正确的,除了最后的 pass 语句是不必要的。可以简单地改写为:

    valid(user_guess, valid_chars, size):
        return len(user_guess) == size and reduce(operator.and , map(lambda x: x in valid_chars ,user_guess))
    

    我不确定你想用 print_game 实现什么。给定一个猜测和线索列表 print_game 应该是:

    print_game(guesses,clues):
        print ('Guesses \t Clues')
        assert len(guesses) == len(clues)
        for g,c in zip(guesses,clues):
            print g,'\t',c
    

    【讨论】:

    • 我该如何输入
    • 命令行。您使用的是 IDE 还是如何运行代码?
    • 只是通过机翼运行它
    • 我没有看到您也在询问有效和打印功能。编辑我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多