From 8d89338afd892e6d43baf6229e48fde96ffb80ff Mon Sep 17 00:00:00 2001 From: EpicLulz1509 Date: Tue, 18 Oct 2022 20:06:06 +0530 Subject: [PATCH] Create mastermind.py --- mastermind.py | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 mastermind.py diff --git a/mastermind.py b/mastermind.py new file mode 100644 index 00000000..bfb09031 --- /dev/null +++ b/mastermind.py @@ -0,0 +1,76 @@ +#recreation of the famous mastermind game + +import random +import re + +code = "" +count3 = False + +for i in range(0,4): + rand = random.randint(1, 9) + code = code + str(rand) +#print(code) + +def check(user_code, code): + count1 = 0 + count2 = 0 + global count3 + count4 = [] + count5 = False + for i in range(0,4): + if code[i] == user_code[i]: + count1 += 1 + count4.append(i) + continue + n = len(count4) + for i in range(0, 4): + #print("Y3") + for k in range(0,n): + try: + if count4[k] == i: + count5 = True + break + else: + count5 = False + except IndexError: + pass + if count5: + continue + #temp_code = user_code + for j in range(0,4): + val1 = code[i] + val2 = user_code[j] + if(val1 == val2) & (i != j): + count2 += 1 + #print(count2) + #print("Y1") + user_code = user_code.replace(val1, "0") + break + #print("Y2") + if(count2 == 4): + print("Oooof so close!!! ALmost there!!!") + elif(count1 == 4): + print("Congrats you won!!!!") + count3 = True + #print(count3) + print(f"{count1} reds pins and {count2} blacks pins.") + + + +print("Welcome to the mastermind game!!!!") +print("Here you will have to guess 4 digit code (digits are from 1 to 8) randomly generated by the system with the help of pins") +print("A black pin means you got only the digit right and a red pin means you got the digit and the placement of the digit right") +print("For example if you enter 1234 and random code was 4321 you would get 4 black pins and 0 red pins") +print("But if you had entered 1256 you would get 0 black pins and 2 red pins") +print("If code is 7867 and you entered 5678, you would get 3 blacks and 0 reds") +print("You will have 10 tries.") + +for i in range(0,10): + print("Enter your code: ") + user_code = input() + check(user_code, "2882") + if count3 == True: + break +if count3 == False: + print("Better luck next time!!!") +print(f"The random code was: {code}")