Python Mastermind Game Troubles
我是Python编程的初学者,在当前的任务中遇到了一个问题。作业内容如下…
- 您的程序应该秘密地生成一个4位数字(或者只包含数字的字符串,4个字符长),它没有重复的数字。
- 然后,您的程序应该要求用户输入代码的猜测。用户猜测应该是4个字符长,只包含数字,不重复数字。您的程序应该在评分前验证输入是否有效,并在必要时提示用户重新输入输入。无效输入不计入代码断路器的猜测次数。您的代码可能会忽略(去掉)输入中的前导或尾随空格,但应将内部空格计为不正确的输入。
- 在每次转弯时,打印转弯编号并得到用户的猜测。如果输入有效,则该回合的输出应该是用户的猜测,然后是反馈。反馈是一个4个字符的字符串:"x"代表正确位置的每个数字;"o"代表代码中但不在正确位置的每个数字;"-"代表所有其他数字。请注意,x和o应该在一起;反馈只是每种类型有多少个挂钩,而不是哪个挂钩或它们在哪里。
- 保留所有猜测和反馈的历史记录,以便在每次轮到用户时打印。
现在我只关注任务的"反馈"部分。我的代码当前为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | import random def validateInput(): inputGuess = input("Enter your guess as 4 numbers:") while True: if len(inputGuess) != 4: inputGuess = input("Enter your guess as 4 numbers:") else: numberList = list(inputGuess) ## invalidNumbers = False for number in numberList: if number not in ['1','2','3','4','5','6','7','8','9']: invalidNumbers = True if invalidNumbers == True: print ("Possible numbers are 1, 2, 3, 4, 5, 6, 7, 8, 9.") inputGuess = input("Enter your guess as 4 numbers:") else: return numberList guessesRemaining=10 code=['1','2','3','4'] while guessesRemaining > 0: report=[] guess=validateInput() guessesRemaining -= 1 if guess[0] == code[0]: report.append("X") if guess[1] == code[1]: report.append("X") if guess[2] == code[2]: report.append("X") if guess[3] == code[3]: report.append("X") tempCode=sorted(code) tempGuess=sorted(guess) if tempCode[0]==tempGuess[0]: report.append("O") if tempCode[1]==tempGuess[1]: report.append("O") if tempCode[2]==tempGuess[2]: report.append("O") if tempCode[3]==tempGuess[3]: report.append("O") report2=report[0:4] dash=["-","-","-","-"] report3=report2+dash report4=report3[0:5] print(report4) |
例如,如果用户猜测1879,代码是1234,我将收到"x o--",但我希望收到"x--"。而且,任何关于简化代码的建议都是非常棒的。为了简单起见,我现在只做了[1,2,3,4]的"随机代码"。
您可以使用python函数map()非常优雅地解决您的问题。(不像我原来想的那么优雅,但仍然很漂亮。)
1 2 | guess ="1879" # or [ '1', '8', '7', '9' ] answer ="1234" |
map()的工作原理是:给它一个函数作为第一个参数,然后给它一个或多个序列作为参数。然后它接受该函数,并首先将其应用于第一个元素,然后应用于第二个元素,依此类推。例如:
1 2 3 4 5 | >>> def f(a,b): >>> return a + b >>> map( f, [1,2,3,4], [ 10, 20, 30, 40 ] ) [ 11, 22, 33, 44 ] |
号
现在,您有两个字符序列,"猜测"和"回答"。可以编写一个函数,如果它们相等,则返回x,否则如下所示:
1 2 | >>> def mastermind_hint( a, b ): >>> return"X" if a == b else"-" |
这还不够,你还需要把"O"放进去。为此,你需要同时使用整个"应答"序列:
1 2 3 4 | >>> def mastermind_hint( a, b ): >>> if a == b: return"X" >>> if a in answer: return"O" >>> return"-" |
。
使用map,我们可以将其应用于您拥有的序列:
1 2 | >>> map( mastermind_hint, guess, answer ) ["X","-","-","-" ] |
现在,我们给出的信息比我们想象的要多,因为提示的位置与猜测字符的位置相对应。隐藏此信息的一个简单方法是对序列进行排序。python有一个sorted()函数,它执行以下操作:
1 2 | >>> sorted( map( mastermind_hint, guess, answer ) ) ["-","-","-","X" ] |
。
剩下的就是把它连接成一个字符串:
1 2 | >>>"".join( sorted( map( mastermind_hint, guess, answer ) ) ) "---X" |
。
我必须说的是非常有趣的家庭作业。我希望我能有这样的家庭作业。
通常我不提供家庭作业的完整答案,但我是为了好玩才解决了这个问题,而你自己也尝试过解决这个问题,所以现在开始:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | import random def main(): print '>> New game started. >> Good luck! ' answer = generateAnswer() while True: userGuess = getUserGuess() if userGuess == answer: print '>> Congratulations, you won!' return print '>> The answer you provided is incorrect. >> Perhaps this hint will help you: ' giveFeedback(answer, userGuess) def generateAnswer(): digits = [str(x) for x in range(10)] answer = '' for i in range(4): digit = random.sample(digits, 1)[0] digits.remove(digit) answer += digit return answer def getUserGuess(): while True: guess = raw_input('>> Please enter a 4-digit number: ').strip() if len(guess) != 4: continue guessIsValid = True for x in guess: if guess.count(x) != 1 or ord(x) not in range(48, 58): guessIsValid = False break if guessIsValid: return guess def giveFeedback(answer, guess): for i in range(4): if guess[i] == answer[i]: print 'X', continue if guess[i] in answer: print 'O', continue print '-', print ' ' if __name__ == '__main__': try: main() except Exception, e: print '>> Fatal error: %s' % e |
我希望你能在这段代码中找到答案。
下面是一个示例,它使用了一些更高级的Python习惯用法来提高可读性和简洁性。这也使得更通用的解决方案变得更容易,例如M基的N位数答案。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | import random from itertools import permutations # for"lucky guess" Easter egg def main(base=10, size=4): print 'New game started. Good luck! ' answer, turn = list(genAnswer(range(base), size)), 1 while True: hint = ['X' if a == g else '0' if g in answer else '-' for a, g in zip(answer, guess(base, size))] if set(hint) == set(['X']): if turn == 1: choices = len(list(permutations(range(base), size))) print 'You won, lucky guess out of %d possible choices' % choices else: print 'Congratulations, you won in %d turns!' % turn return print ' Hint %d: %s' % (turn, ''.join(hint)) turn += 1 def genAnswer(digits, size): '''Python generator function''' for i in range(size): choice = random.choice(digits) yield str(choice) digits.remove(choice) def guess(base, size): '''Uses duck typing for error detection''' while True: guess = raw_input('>> Guess: ').strip() try: if int(guess, base) < base**size and len(set(guess)) == size: return guess except ValueError: pass print 'Enter %d unique digits from 0 to %d' % (size, base -1) >>> main(6, 4) New game started. Good luck! >> Guess: 1234 Hint 1: 0-X- >> Guess: 12345 Enter 4 unique digits from 0 to 5 >> Guess: 01227 Enter 4 unique digits from 0 to 5 >> Guess: 0123 Hint 2: XX-0 >> Guess: 0135 Congratulations, you won in 3 turns! |
。