0

对于一个学校项目,我正在用python创建游戏大师头脑,如果你不知道大师头脑是如何工作的,它的基本要点是通过已知的相同颜色的正确位置和相同颜色的错误位置来破解代码。

无论如何,我的代码中的主要问题是为每个线索计算线索答案的不正确位置的逻辑。我的解决方案是:假设答案是 1234,线索是 1223 我需要找到正确的位置和不正确的位置以添加到侧面。首先,我会创建一个 2 个变量,即行和答案代码,这在我之前的代码中是这样做的。我将线的字符串索引测量到答案的屏幕索引,如果这是真的,那么我将 1 加到红心上,然后继续下一个字符串索引。如果它不在正确的位置,那么我使用以下代码:

def calc_hearts(line,num,num2,answernum,dootnum): #num is the number your comparing to the answer's index, num2 is the hearts final number, and answernum is the compared answer number
    global line1,line2,line3,line4,line5,heart,answer,og_answer,board,og_line
    heart = False
    og_line = line
    og_answer = answer
    if board[num] == answer[answernum]:
        board[num2] = board[num2] + 1
        heart = True
        og_answer = answer
        og_line = line
        answer = answer.replace(board[answernum],'')
        line = line.replace(board[num],'')
    else:
        heart = False
    if heart == True:
        pass
    elif heart == False:
        if line.count(board[num]) == 0:
            board[dootnum] = 0
            answer = og_answer
            line = og_line
        elif answer.count(board[num]) == 0:
            board[dootnum] = 0
            answer = og_answer
            line = og_line
        else:
            ie = line.count(board[num]) + answer.count(board[num])
            board[dootnum] = ie / 2
            if isinstance(board[dootnum], float):
                board[dootnum] = board[dootnum] - 0.5
                answer = og_answer
                line = og_line
            else:
                answer = og_answer
                line = og_line

心脏只是更换正确的位置。无论如何,如果编辑的答案(删除了两者的正确位置)是== 0,或者如果编辑的行== 0,那么它会跳过所有这些。但如果他们不是,它立即进入数学,在这种情况下,它需要给定数字的数量,或者在这种情况下,在编辑的答案和编辑的行中,将它们加起来,然后将它们除以二得到该线索的不正确位置的数量,如果它以浮点数形式出现,则将其四舍五入,假设我们在编辑的答案中有 3 个,在编辑的行中有 2 个,它得到 5,除以 2,得到 2.5 和减去 0.5 得到错误位置的数量。

我知道有多种方法可以解决或优化它,但我只想让我当前的方法正常工作,我的整个班级都找不到我的问题的解决方案,我是班上唯一尝试任何东西的人我自己的时间。

完整代码如下:

import random
emoji = ['','','','','','']
fart = 0
board = ['-','-','-','-','-', #line 1          | 0 1 2 3 4
         '-','-','-','-','-', #line 2          | 5 6 7 8 9
         '-','-','-','-','-', #line 3          | 10 11 12 13 14
         '-','-','-','-','-', #line 4          | 15 16 17 18 19
         '-','-','-','-','-', #line 5          | 20 21 22 23 24
         '-','-','-','-','-', #guess           | 25 26 27 28 29
         0,0,0,0,0,0,0,0,0,0, #hearts and dots | 30 31 | 32 33 | 34 35 | 36 37 | 38 39
         '-','-','-','-','-', #answer          | 40 41 42 43 44
         ]  
def start():
    global fart
    print('-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------')
    print('-----------------------------------------------------------------____     Pick The Lock    ----------------------------------------------------------------------------------------') 
    print('--------------------------------------------------------------- /    \ ____________________ ---------------------------------------------------------------------------------------')
    print('---------------------------------------------------------------|   ◠                       \ --------------------------------------------------------------------------------------') 
    print('---------------------------------------------------------------|   ◡  ___   _____   ___^___/ --------------------------------------------------------------------------------------')  
    print('--------------------------------------------------------------- \____/    \/      \/         --------------------------------------------------------------------------------------') 
    print('----------------------------------------------------------------------  Play! (3) (4) (5)  ----------------------------------------------------------------------------------------')
    print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
    gameplay = input('3, 4, or 5?(This is difficulty)')
    if gameplay == '3':
        fart = 3
        play()
    elif gameplay == '4':
        fart = 4
        play()
    elif gameplay == '5':
        fart = 5
        play()
    else:
        print('incorrect difficulty choice')
        start()
def randomize_pegs():
    global board,emoji,fart,line1,line2,line3,line4,line5,answer
    board = ['-','-','-','-','-', #line 1          | 0 1 2 3 4
             '-','-','-','-','-', #line 2          | 5 6 7 8 9
             '-','-','-','-','-', #line 3          | 10 11 12 13 14
             '-','-','-','-','-', #line 4          | 15 16 17 18 19
             '-','-','-','-','-', #line 5          | 20 21 22 23 24
             '-','-','-','-','-', #guess           | 25 26 27 28 29
             0,0,0,0,0,0,0,0,0,0, #hearts and dots | 30 31 | 32 33 | 34 35 | 36 37 | 38 39
             '-','-','-','-','-', #answer          | 40 41 42 43 44
             ]
    ie = 0
    ei = 40
    while ie < 25:
        board[ie] = random.choice(emoji)
        ie = ie + 1
    while ei < 45:
        board[ei] = random.choice(emoji)
        ei = ei + 1
    if fart == 3:
        line1 = board[0] + board[1] + board[2]
        line2 = board[5] + board[6] + board[7]
        line3 = board[10] + board[11] + board[12]
        line4 = board[15] + board[16] + board[17]
        answer = board[40] + board[41] + board[42]
    elif fart == 4:
        line1 = board[0] + board[1] + board[2] + board[3]
        line2 = board[5] + board[6] + board[7] + board[8]
        line3 = board[10] + board[11] + board[12] + board[13]
        line4 = board[15] + board[16] + board[17] + board[18]
        line5 = board[20] + board[21] + board[22] + board[23]
        answer = board[40] + board[41] + board[42] + board[43]
    elif fart == 5:
        line1 = board[0] + board[1] + board[2] + board[3] + board[4]
        line2 = board[5] + board[6] + board[7] + board[8] + board[9]
        line3 = board[10] + board[11] + board[12] + board[13] + board[14]
        line4 = board[15] + board[16] + board[17] + board[18] + board[19]
        line5 = board[20] + board[21] + board[22] + board[23] + board[24]
        answer = board[40] + board[41] + board[42] + board[43] + board[44]
    else:
        print('ERROR! DEFINING LINES AND ANSWER DIDNT WORK')
def play():
    global line1,line2,line3,line4,line5,heart,answer,og_answer,board,og_line
    randomize_pegs()
    calc_both()
    print(answer)
    display_board()
def calc_hearts(line,num,num2,answernum,dootnum): #num is the number your comparing to the answer's index, num2 is the hearts final number, and answernum is the compared answer number
    global line1,line2,line3,line4,line5,heart,answer,og_answer,board,og_line
    heart = False
    og_line = line
    og_answer = answer
    if board[num] == answer[answernum]:
        board[num2] = board[num2] + 1
        heart = True
        og_answer = answer
        og_line = line
        answer = answer.replace(board[answernum],'')
        line = line.replace(board[num],'')
    else:
        heart = False
    if heart == True:
        pass
    elif heart == False:
        if line.count(board[num]) == 0:
            board[dootnum] = 0
            answer = og_answer
            line = og_line
        elif answer.count(board[num]) == 0:
            board[dootnum] = 0
            answer = og_answer
            line = og_line
        else:
            ie = line.count(board[num]) + answer.count(board[num])
            board[dootnum] = ie / 2
            if isinstance(board[dootnum], float):
                board[dootnum] = board[dootnum] - 0.5
                answer = og_answer
                line = og_line
            else:
                answer = og_answer
                line = og_line
        print("HEART = 0")
def calc_both():
    global fart,board
    if fart == 3:
        calc_hearts(line1,0,30,0,31)
        #
        calc_hearts(line1,1,30,1,31)
        #
        calc_hearts(line1,2,30,2,31)
        ##
        calc_hearts(line2,5,32,0,33)
        #
        calc_hearts(line2,6,32,1,33)
        #
        calc_hearts(line2,7,32,2,33)
        ###
        calc_hearts(line3,10,34,0,35)
        #
        calc_hearts(line3,11,34,1,35)
        #
        calc_hearts(line3,12,34,2,35)
        ##
        calc_hearts(line4,15,36,0,37)
        #
        calc_hearts(line4,16,36,1,37)
        #
        calc_hearts(line4,17,36,2,37)
    elif fart == 4:
        calc_hearts(line1,0,30,0,31)
        #
        calc_hearts(line1,1,30,1,31)
        #
        calc_hearts(line1,2,30,2,31)
        #
        calc_hearts(line1,3,30,3,31)
        ##
        calc_hearts(line2,5,32,0,33)
        #
        calc_hearts(line2,6,32,1,33)
        #
        calc_hearts(line2,7,32,2,33)
        #
        calc_hearts(line2,8,32,3,33)
        ##
        calc_hearts(line3,10,34,0,35)
        #
        calc_hearts(line3,11,34,1,35)
        #
        calc_hearts(line3,12,34,2,35)
        #
        calc_hearts(line3,13,34,3,35)
        ###
        calc_hearts(line4,15,36,0,37)
        #
        calc_hearts(line4,16,36,1,37)
        #
        calc_hearts(line4,17,36,2,37)
        #
        calc_hearts(line4,18,36,3,37)
        ##
        calc_hearts(line5,20,38,0,39)
        #
        calc_hearts(line5,21,38,1,39)
        #
        calc_hearts(line5,22,38,2,39)
        #
        calc_hearts(line5,23,38,3,39)
    elif fart == 5:
        calc_hearts(line1,0,30,0,31)
        #
        calc_hearts(line1,1,30,1,31)
        #
        calc_hearts(line1,2,30,2,31)
        #
        calc_hearts(line1,3,30,3,31)
        #
        calc_hearts(line1,4,30,4,31)
        ##
        calc_hearts(line2,5,32,0,33)
        #
        calc_hearts(line2,6,32,1,33)
        #
        calc_hearts(line2,7,32,2,33)
        #
        calc_hearts(line2,8,32,3,33)
        #
        calc_hearts(line2,9,32,4,33)
        ##
        calc_hearts(line3,10,34,0,35)
        #
        calc_hearts(line3,11,34,1,35)
        #
        calc_hearts(line3,12,34,2,35)
        #
        calc_hearts(line3,13,34,3,35)
        #
        calc_hearts(line3,14,34,4,35)
        ###
        calc_hearts(line4,15,36,0,37)
        #
        calc_hearts(line4,16,36,1,37)
        #
        calc_hearts(line4,17,36,2,37)
        #
        calc_hearts(line4,18,36,3,37)
        #
        calc_hearts(line4,19,36,4,37)
        ##
        calc_hearts(line5,20,38,0,39)
        #
        calc_hearts(line5,21,38,1,39)
        #
        calc_hearts(line5,22,38,2,39)
        #
        calc_hearts(line5,23,38,3,39)
        #
        calc_hearts(line5,24,38,4,39)
    else:
        print('catastrophic failure')
def display_board():
    global fart,board
    print("-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------")
    if fart == 3:
        print("-----------------------------------------------------------------------------------------------------------------------------------_" + board[25] + "_---_" + board[26] + "_---_" + board[27] + "_----------right pos||wrong pos---")
    elif fart == 4:
        print("-----------------------------------------------------------------------------------------------------------------------------_" + board[25] + "_---_" + board[26] + "_---_" + board[27] + "_---_" + board[28] + "_----------right pos||wrong pos---")
    elif fart == 5:
        print("-----------------------------------------------------------------------------------------------------------------------_" + board[25] + "_---_" + board[26] + "_---_" + board[27] + "_---_" + board[28] + "_---_" + board[29] + "_----------right pos||wrong pos---")
    else:
        print("------------------------------------------------------------------------------------------------------------------------------------------------------------right pos||wrong pos---")
    print("---------------------------------------------------------------------------------------------------------------------------___________________________-----------------------------")
    if fart == 3:
        print("----------------------------------------------------------------------------------------------------------------------------------_" + board[0] + "_---_" + board[1] + "_---_" + board[2] + "_----------" + str(board[30]) + "-----------" + str(board[31]) + "------")
    elif fart == 4:
        print("---------------------------------------------------------------------------------------------------------------------------_" + board[0] + "_---_" + board[1] + "_---_" + board[2] + "_---_" + board[3] + "_----------" + str(board[30]) + "-----------" + str(board[31]) + "------")
    elif fart == 5:
        print("--------------------------------------------------------------------------------------------------------------------_" + board[0] + "_---_" + board[1] + "_---_" + board[2] + "_---_" + board[3] + "_---_" + board[4] + "_----------" + str(board[30]) + "-----------" + str(board[31]) + "------")
    else:
        print("-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------")
    print("---------------" + emoji[0] + "---" + emoji[1] + "---" + emoji[2] + "---" + emoji[3] + "---" + emoji[4] + "---" + emoji[5] + "--------------------------------------------------------------------------------------------------------------------------------------")
    if fart == 3:
        print("----------------------------------------------------------------------------------------------------------------------------------_" + board[5] + "_---_" + board[6] + "_---_" + board[7] + "_----------" + str(board[32]) + "-----------" + str(board[33]) + "------")
    elif fart == 4:
        print("---------------------------------------------------------------------------------------------------------------------------_" + board[5] + "_---_" + board[6] + "_---_" + board[7] + "_---_" + board[8] + "_----------" + str(board[32]) + "-----------" + str(board[33]) + "------")
    elif fart == 5:
        print("--------------------------------------------------------------------------------------------------------------------_" + board[5] + "_---_" + board[6] + "_---_" + board[7] + "_---_" + board[8] + "_---_" + board[9] + "_----------" + str(board[32]) + "-----------" + str(board[33]) + "------")
    else:
        print("-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------")
    print("-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------")
    if fart == 3:
        print("----------------------------------------------------------------------------------------------------------------------------------_" + board[10] + "_---_" + board[11] + "_---_" + board[12] + "_----------" + str(board[34]) + "-----------" + str(board[35]) + "------")
    elif fart == 4:
        print("---------------------------------------------------------------------------------------------------------------------------_" + board[10] + "_---_" + board[11] + "_---_" + board[12] + "_---_" + board[13] + "_----------" + str(board[34]) + "-----------" + str(board[35]) + "------")
    elif fart == 5:
        print("--------------------------------------------------------------------------------------------------------------------_" + board[10] + "_---_" + board[11] + "_---_" + board[12] + "_---_" + board[13] + "_---_" + board[14] + "_----------" + str(board[34]) + "-----------" + str(board[35]) + "------")
    else:
        print("-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------")
    print("-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------")
    if fart == 3:
        print("----------------------------------------------------------------------------------------------------------------------------------_" + board[15] + "_---_" + board[16] + "_---_" + board[17] + "_----------" + str(board[36]) + "-----------" + str(board[37]) + "------")
    elif fart == 4:
        print("---------------------------------------------------------------------------------------------------------------------------_" + board[15] + "_---_" + board[16] + "_---_" + board[17] + "_---_" + board[18] + "_----------" + str(board[36]) + "-----------" + str(board[37]) + "------")
    elif fart == 5:
        print("--------------------------------------------------------------------------------------------------------------------_" + board[15] + "_---_" + board[16] + "_---_" + board[17] + "_---_" + board[18] + "_---_" + board[19] + "_----------" + str(board[36]) + "-----------" + str(board[37]) + "------")
    else:
        print("-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------")
    print("-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------")
    if fart == 3:
        print("-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------")
    elif fart == 4:
        print("---------------------------------------------------------------------------------------------------------------------------_" + board[20] + "_---_" + board[21] + "_---_" + board[22] + "_---_" + board[23] + "_----------" + str(board[38]) + "-----------" + str(board[39]) + "------")
    elif fart == 5:
        print("--------------------------------------------------------------------------------------------------------------------_" + board[20] + "_---_" + board[21] + "_---_" + board[22] + "_---_" + board[23] + "_---_" + board[24] + "_----------" + str(board[38]) + "-----------" + str(board[39]) + "------")
    else:
        print("-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------")
    print("-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------")
    print
("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
start()

我知道放屁是使用游戏类型的一个不成熟的变量名,但命名这样的东西让我保持理智

4

0 回答 0