da derpy man
da derpy man

Reputation: 1

my logic on this mastermind python works on paper, but when i use it in python it gives me two different errors, one logic and the other string index

for a school project i am creating the game master mind in python, if you dont know how master mind works the basic jist of it is to crack the code through known correct positions of the same colors and incorrect positions of the same colors.

anyways, the main issue in my code is the logic for calculating the incorrect positions of the answer to the clue, for each clue. my solution is: lets say the answer is 1234 and the clue is 1223 i need to find both the correct positions and incorrect positions to add to the side. first, i would make a 2 variables, the line and the answer code, which in my code i do previously. i measure the string index of the line to the answers' screen index, and if that is true then i add 1 to the hearts and move on to the next string index of line. if it isnt in the right position, then i use the following code:

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

heart is just a replacement of correct position. anyways, if the edited answer(removed the correct positions of both) is == 0, or same of if the edited line == 0 then it skips all that. but if their not, it goes imediantly to the math, where it takes the amount of the given number, or color in this case, in both the edited answer and the edited line, adds them both up, then divides them by two to get the amount of incorrect positions there are for that clue, and if it comes as a float it rounds it down, persay we have 3 in the edited answer and 2 in the edited line, its gets 5, divides it by 2, gets 2.5 and subtracts 0.5 to get the amount of incorrect positions.

i know that there are multiple ways i could approach this, or optimize it, but i just want to get my current method working and my entire class cannot find a solution to my problem, granted im the only person in the class to try anything on my own time.

the full code is below:

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()

i know fart is an immature variable name for using the game type but naming things like that keeps me sane

Upvotes: 0

Views: 57

Answers (0)

Related Questions