我对编码相对陌生,我想用字母而不是颜色/数字创建一个主谋游戏。
我的MasterMind中的密码是一个由4个字母组成的序列。密码中的每个字母都是唯一的,从"a"到"H"。有效密码的一些示例是"ABDF"、"EGHC"one_answers"DAFE">。
以下示例无效:
- "ABBG"–它包含"B"的重复字符
- "LHAD"–它包含的字符"L"超出了"A"到"H"之间的范围
- "DBA"–它只包含3个字符,而不是必需的4个字符
我目前已经完成了这项工作:
import random
def chooseOneLetter (base1, base2):
ratio = 10
seed = int(random.uniform (0, ratio*len(base1)+len(base2)))
if seed < ratio*len(base1):
chosenLetter = base1[int(seed/ratio)]
base1.remove(chosenLetter)
else:
chosenLetter = base2[(seed - ratio*len(base1))]
base2.remove(chosenLetter)
return chosenLetter
def getSecretCode(base1, base2):
secretCode = ""
for i in range(4):
chosenLetter = chooseOneLetter (base1, base2)
secretCode += chosenLetter
return secretCode
# base1 = ["A", "B", "C", "D"]
# base2 = ["E", "F", "G", "H"]
然而,我想再包括两个变量。第一个变量引用位于正确位置的字母列表,如果猜测中的字母不正确,则引用None。第二个变量引用了一本字典,其中以猜错位置的字母为关键字,以猜错错误位置的字母的次数为值。
例如,如果密码为BAFD,则
第一个变量引用了['B',None,None]。玩家正确地猜到字母B在第一个位置,但其他位置的所有其他字母都不正确。
第二个变量引用了{'A':2,'B':1,'D':2}。到目前为止,玩家已经猜到了3个正确的字母:A被猜错了两次,B被猜错一次,D被猜错两次。
我还在寻找游戏引擎,以提示用户玩另一个游戏,如果不符合标准,则重新输入字符串。它应该看起来像这样。
Enter a guess to continue or RETURN to quit: abda
Please enter 4 unique letters, A to H
Enter a guess to continue or RETURN to quit: ade
Please enter 4 unique letters, A to H
Enter a guess to continue or RETURN to quit: asbc
Please enter 4 unique letters, A to H
Enter a guess to continue or RETURN to quit: abcd
The guess is not correct, attempt no. 1
The correct letters in correct positions: [None, None, None,
None]
The correct letters and the number of times found in incorrect
positions: {'B': 1, 'C': 1, 'D': 1}
Enter a guess to continue or RETURN to quit: dhcb
The guess is not correct, attempt no. 2
The correct letters in correct positions: [None, None, None,
'B']
The correct letters and the number of times found in incorrect
positions: {'B': 1, 'C': 2, 'D': 2, 'H': 1}
Enter a guess to continue or RETURN to quit: cdhb
You guessed it correctly in 3 attempts, the secret word is CDHB
Do you want to play again? Y to play again: y
非常感谢你的帮助!
这里有一种方法可以做到这一点。sample((形成了密码。每次猜测后都会准备一份反馈列表。对于猜测中的每个字母,它将显示(1(字母,如果它在正确的位置(2(True,如果字母在代码中但在错误的位置,或者(3(False,如果它根本不在代码中。计数字典会记录有多少字母被猜错了位置。
import random
def mastermind():
key = random.sample('ABCDEFGH', 4)
count = {}
feedback = []
guess = ''
while guess != key:
feedback.clear()
guess = list(input('Enter your guess: '))
for i, v in enumerate(guess):
if v == key[i]:
feedback.append(v)
elif v in key:
feedback.append(True)
count[v] = 1 if v not in count else count[v] + 1
else:
feedback.append(False)
print(feedback, count)
print('Correct!')
while True:
mastermind()
if input('Play again? (Y/N): ').lower() == 'n':
break
游戏玩法:
Enter your guess: ABCD
[False, True, True, False] {'B': 1, 'C': 1}
Enter your guess: BCEF
[True, True, True, False] {'B': 2, 'C': 2, 'E': 1}
Enter your guess: CEBG
['C', 'E', True, False] {'B': 3, 'C': 2, 'E': 1}
Enter your guess: CEHB
['C', 'E', 'H', 'B'] {'B': 3, 'C': 2, 'E': 1}
Correct!
Play again? (Y/N): y
Enter your guess: ABCD
[True, False, False, True] {'A': 1, 'D': 1}
Enter your guess: DAEF
['D', True, False, 'F'] {'A': 2, 'D': 1}
Enter your guess: DGAF
['D', 'G', 'A', 'F'] {'A': 2, 'D': 1}
Correct!
Play again? (Y/N): n
>>>