首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >大师游戏中的后勤错误

大师游戏中的后勤错误
EN

Stack Overflow用户
提问于 2017-02-16 22:46:39
回答 1查看 150关注 0票数 0

作为背景: Meirovitz在20世纪70年代初发明了一个破解代码的游戏。在这个游戏中,一个玩家--代码主--负责生成一个由四种颜色组成的代码。第二个玩家--密码破解者--试图猜出代码。每次猜测后,代码母版将提供有关猜测的反馈。具体来说,代码主将告诉代码断路器有多少颜色是正确的,有多少颜色处于正确的位置。密码破解者继续猜测代码,直到他们能够成功地破解代码。代码破解者的“分数”是指它破坏代码的次数。

该方案的目标是:

  1. 定义有效的颜色。
  2. 使用有效的颜色生成秘密代码。
  3. 只要我还没有猜到代码:让用户猜测;更新猜测的次数。确保猜测是有效的
  4. 确定正确颜色的数目
  5. 确定正确位置的数目
  6. 向代码破解者提供此反馈。
  7. 向用户报告最终得分。

我只自信地通过了步骤1和步骤2,我的问题是如何获得isValidGuess函数来确定只有validColors存在。正如现在编写的那样,函数没有包含validColors参数,我也不完全确定为什么。对于每个函数,都有一个简短的说明,说明该功能应该做什么。

-注意-我不希望有人为我编写完整的代码,只是为了帮助我使用isValidGuess函数。

代码语言:javascript
复制
import random 
validColors = ["R", "G", "B", "Y", "O", "P"]

def generateCode(validColors):
    #input: takes a string that represents validColors
    #action: builds a string of length 4; each character is picked at random
    #       from the validColors
    #return:  a string of length 4 representing the codemaster's secretCode
    secretCode = random.sample(validColors, 4)
    return secretCode

def isValidGuess(guess, validColors):
    #input: guess and validColors as strings
    #action: ensures that the guess is the right length and only contains
    #       valids colors
    #return: boolean value to isValidGuess
    isValidGuess = False 
    if len(guess) != 4:
        print("Invalid, guess again.")
    elif len(guess) == 4:
        isValidGuess = True 
    return isValidGuess 

#def correctLocationCount(secretCode, guess):
    #input: secretCode and guess as strings
    #action: character by character comparison of the two strings and
    #       updates a counter every time they match
    #return: the counter

#def correctColorCount(secretCode, guess, validColors):
    #input: secretCode, guess, and valid colors as strings
    #action: looks at each valid color and counts how many times that color
    #       appears in the code and how many times it appears in the guess
    #       Compare these counts to determine how many colors were guessed
    #return: the count


def masterMind():
     generateCode(validColors)
     guess = input("What is your guess: ")
     result = isValidGuess(guess, validColors)
     while result == False:
        guess = input("What is your guess: ")
        result = isValidGuess(guess, validColors)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-16 23:36:09

您可以检查code中的每一种颜色是否也在validColors中。在Python中最惯用的方法是使用内置的all函数和生成器表达式:all(color in validColors for color in guess)

另一种方法(对于新程序员来说可能更容易理解)是使用一个显式循环:

代码语言:javascript
复制
for color in guess:
    if color not in validColors:
        # do whatever is appropriate for an invalid color here (e.g. return False)

您可能需要反转当前isValidGuess变量的逻辑(默认为True,如果猜测无效,则重置为False )。或者,当您发现一些无效的东西时,您可以立即删除该变量,只需要return False (如果没有任何证明无效的东西,则在函数末尾的return True )。

请注意,我的猜测中没有检查重复颜色。如果不允许这样做,您可能需要单独检查它,可能需要使用set。如果您也将validColors设置为一个集合,则可以在一个非常紧凑的表达式中检查所有内容:

代码语言:javascript
复制
def isValidGuess(guess, validColors):
    valid_set = set(validColors) # if validColors was already a set this would be unnecessary
    guess_set = set(guess)
    return len(guess) == len(guess_set) == 4 and guess_set.issubset(valid_set)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42286135

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档