试图破解类似于学校项目的名为“ Mastermind”的代码控制台游戏。示例:http://www.webgamesonline.com/mastermind/ whitecheck应该是:正确的数字,但位置错误。 redcheck应该是:正确的数字和位置。
但是,如果我运行代码,它将出错,并告诉我根据猜测数字的位置有1个白核和1个红核,无论最后是否给出答案。
玩家选择1-4范围内的4个数字。 如果玩家猜对了正确的数字,但是位置错误,则应该显示一个白点来表示。 如果玩家猜出正确的数字和位置,它应该显示一个红点来表示。
问题是,如果我像这样运行代码,它将首先检查第一个数字,如果它是正确的数字,但位置错误,则会将其添加到白名单中。
如果在稍后的代码中检查另一个完全相同的数字时给出了正确的数字和位置,则不应发生这种情况。
我尝试将猜测的答案追加到一个空的检查表中,但是我需要该程序立即检查所有这些答案,因此如果结尾处是否存在相同数字的重做检查,则不会给我白检查。
import random
code = []
attempts = 3
while len(code) != 4:
for x in range(4):
n = random.randint(1, 4)
if n not in code:
code.append(n)
print(code)
pos1 = str(code[0])
pos2 = str(code[1])
pos3 = str(code[2])
pos4 = str(code[3])
answer = str(pos1) + str(pos2) + str(pos3) + str(pos4)
guess = None
while guess != answer:
positionguess1 = str(input("position 1: "))
positionguess2 = str(input("position 2: "))
positionguess3 = str(input("position 3: "))
positionguess4 = str(input("position 4: "))
checklist = []
whitecheck = 0
redcheck = 0
""" Row 1 code check """
if positionguess1 == pos1:
redcheck += 1
checklist.append(positionguess1)
elif positionguess1 != pos1 and positionguess1 in answer and positionguess1 not in checklist:
checklist.append(positionguess1)
whitecheck += 1
""" Row 2 code check """
if positionguess2 == pos2:
redcheck += 1
checklist.append(positionguess2)
elif positionguess2 != pos2 and positionguess2 in answer and positionguess2 not in checklist:
checklist.append(positionguess2)
whitecheck += 1
""" Row 3 code check """
if positionguess3 == pos3:
redcheck += 1
checklist.append(positionguess3)
elif positionguess3 != pos3 and positionguess3 in answer and positionguess3 not in checklist:
checklist.append(positionguess3)
whitecheck += 1
""" Row 4 code check """
if positionguess4 == pos4:
checklist.append(positionguess4)
redcheck += 1
elif positionguess4 != pos4 and positionguess4 in answer and positionguess4 not in checklist:
checklist.append(positionguess4)
whitecheck += 1
crackattempt = str(positionguess1) + str(positionguess2) + str(positionguess3) + str(positionguess4)
print ("You've entered:", crackattempt)
if crackattempt == answer:
print ("Amount in wrong position with right value:", whitecheck)
print("Amount in the right position and the right value:", redcheck)
print ("cracked the code, you win")
elif attempts == 0:
print ("you lose.")
break
elif crackattempt != answer:
print ("Wrong! Try again.")
print("Amount in wrong position with right value:", whitecheck)
print("Amount in the right position and the right value:", redcheck)
attempts -= 1
如果随机生成的代码是'1234',并且猜到的代码是'4234,它应该给我3个redcheck而不是3个redcheck + 1个whitecheck
我认为我通过将redcheck与whitecheck语句分开来解决了它。
""" correct number and correct position check """
if positionguess1 == pos1:
redcheck += 1
checklist.append(positionguess1)
if positionguess2 == pos2:
redcheck += 1
checklist.append(positionguess2)
if positionguess3 == pos3:
redcheck += 1
checklist.append(positionguess3)
if positionguess4 == pos4:
checklist.append(positionguess4)
redcheck += 1
""" correct number but wrong position checks """
if positionguess1 != pos1 and positionguess1 in answer and positionguess1 not in checklist:
checklist.append(positionguess1)
whitecheck += 1
if positionguess2 != pos2 and positionguess2 in answer and positionguess2 not in checklist:
checklist.append(positionguess2)
whitecheck += 1
if positionguess3 != pos3 and positionguess3 in answer and positionguess3 not in checklist:
checklist.append(positionguess3)
whitecheck += 1
if positionguess4 != pos4 and positionguess4 in answer and positionguess4 not in checklist:
checklist.append(positionguess4)
whitecheck += 1
答案 0 :(得分:3)
您的错误符合逻辑。让我们看看这个片段
""" Row 1 code check """
elif positionguess1 != pos1 and positionguess1 in answer and positionguess1 not in checklist:
checklist.append(positionguess1)
whitecheck += 1
此处代码检查用户在第一行中输入的数字是否在另一行中。如果像您的示例一样重复数字(用户输入4234,正确答案1234),它会发现第一行中没有4而是存在4,因此添加了白校验。 checklist
用于排除已经测试过的数字,但作为测试的第一行,检查表为空,因此将始终添加白检查。
要解决此问题,您应该先检查确切的数字,然后再将其全部添加到检查表(如果有)中,而不要逐行检查。然后,在错误的位置检查数字是否正确。
答案 1 :(得分:0)
编辑-每次编辑的游戏都不相同但未被删除,因为OP代码中有一些有用的提示
下面的代码是我认为是游戏的实现。
import random
code = random.randint(1000, 9999)
def checks(code, guess):
"""Return tuple with redchecks and whitechecks"""
code = str(code)
guess = str(guess)
redcheck = sum([code[i]==guess[i] for i in range(0, 4)])
whitecheck = sum([
code[i]!=guess[i] and (code[i] in guess)
for i in range(0, 4)
])
return (redcheck, whitecheck)
while True:
try:
guess = int(input('Guess:'))
except:
print('Exit game')
break
if code == guess:
print('You win!!')
break
print(checks(code, guess))