在 python 中使用字母和验证的策划游戏

Mastermind game using alphabets and validation in python

我对编码比较陌生,想用字母而不是 colors/numbers 来创建一个策划游戏。

我的 MasterMind 中的密码是 4 个字母的序列。密码中的每个字母都是唯一的,从“A”到“H”。一些有效密码的示例是 “ABDF”、“EGHC”和“DAFE”

以下示例无效:

我目前已经完成了:

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"]

但是,我想再包含 2 个变量。第一个变量引用正确位置的字母列表,如果猜测中的字母不正确,则引用 None。第二个变量引用一个字典,其中字母被猜错的位置作为键,字母被猜错的次数作为值。

E.g. if the secret code is BAFD,

The first variable references ['B', None, None, None]. The player has correctly guessed that letter B is in the first position, but all other letters in other positions are incorrect.

The second variable references {'A': 2, 'B': 1, 'D': 2}. The player has guessed 3 correct letters thus far: A is guessed in wrong positions twice, B in a wrong position once, and D in wrong positions twice.

我也在找游戏引擎提示用户玩另一个游戏,如果不符合条件就重新输入字符串。它应该看起来像这样。

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

非常感谢您的帮助!

这里有一种方法可以做到这一点。 random.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
>>>