-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmastermind.py
More file actions
71 lines (64 loc) · 2.29 KB
/
mastermind.py
File metadata and controls
71 lines (64 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import random
def game():
answer=[random.randint(0, 9), random.randint(0, 9), random.randint(0, 9), random.randint(0, 9)]
print("Welcome to Mastermind!")
for x in range(15):
print("please enter a guess:")
while True:
try:
guess = int(input())
if guess > 9999 or guess < 0:
print("Your guess has to be a 4 digit positive number...")
print("or any number smaller than 1000 because of how I coded it")
else:
break
except:
print("Your guess has to be a 4 digit positive number...")
print("or any number smaller than 1000 because of how I coded it")
hint = check(answer, guess)
if(hint == "2222"):
print("Your guess " + str(guess) + " was correct! Congratulations!")
break
elif x == 14:
print("You lost! Haha! Noob!")
print("The answer was: " + str(answer[0])+str(answer[1])+str(answer[2])+str(answer[3]) + " all along!")
else:
print("Here is your hint: " + hint)
def check(ans, gue):
result=""
checked = [0, 0, 0, 0]
gue2 = gue
for y in range(4):
digit = gue2 % 10
gue2 //= 10
if digit == ans[3-y]:
checked[3-y] = 2
result = "2" + result
gue2 = gue
result = result + check_more(gue2, checked, ans, 3)
gue2 //= 10
result = result + check_more(gue2, checked, ans, 2)
gue2 //= 10
result = result + check_more(gue2, checked, ans, 1)
gue2 //= 10
result = result + check_more(gue2, checked, ans, 0)
return result
def check_more(guess, checked, answer, num):
if checked[num] != 2:
digit = guess % 10
cont = True
index = 0
while cont and index < 4:
if checked[index] == 0 and answer[index] == digit:
checked[index] = 1
return "1"
cont = False
else:
index += 1
return ""
play = "play again"
while play.lower() == "play again":
game()
print("Thank you for playing! Enter 'play again' to play again or anything else otherwise.")
play = input()
print("Bye Bye!")