2
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次嘗試中。MasterMind遊戲Python代碼
我在與print_game功能故障和有效功能
而且我不知道如何指揮實際的程序if__name__ ==「主要」部分後,功能類似遊戲。就像在調用實際的功能給用戶看命令一樣。
任何幫助,非常感謝。
我怎麼會把輸入 – Deka 2014-10-19 22:27:55
命令行。您是使用IDE還是如何運行代碼? – Parker 2014-10-19 22:31:24
只是通過翅膀運行它 – Deka 2014-10-19 22:32:41