-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmastermind.py
75 lines (66 loc) · 2.66 KB
/
mastermind.py
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
72
73
74
75
# I'm (a) coding Mastermind here!
# Written by Danny Brown on November 19, 2017
import random, re, sys
if sys.version_info[0] < 3:
raise "Must be using Python 3"
# Function Definitions
def validateDifficulty():
while True:
try:
newDiff = int(input("Select a difficulty between 2 and 9: "))
except ValueError:
print("That's not a valid value. ", end='')
continue
if newDiff > 9 or newDiff < 2:
print("That's not a valid value. ", end='')
continue
else:
return newDiff
def generateBoard(difficulty):
newBoard = []
for x in range(4):
newBoard.append(random.randint(1, difficulty))
return newBoard
def guessFunc(diff):
guess = str(input(": ")) # accepts guess in format like '1234'
while not re.match("[1-" + str(diff) + "][1-" + str(diff) + "][1-" + str(diff) + "][1-" \
+ str(diff) + "]", guess) or len(guess) != 4: # validates guess
guess = str(input("No. Guess exactly four numerals between 1 and " + str(diff) +":\n > "))
guessList = list(guess) # turn guess into a list of characters like ['1', '2', '3', '4']
guessList = list(map(int, guessList)) # turn lists of chars into list of ints like [1, 2, 3, 4]
return guessList
def compareLists(list1, list2):
matchCount = 0
for index in range(4):
if list2[index] == list1[index]:
matchCount += 1
print(" *", end='')
if matchCount == 4:
print("\n\nYou win!")
print("The board was: ", end='')
print(board)
return True
else:
print()
return False
# Main
difficulty = validateDifficulty()
board = generateBoard(difficulty)
print("\n*********************************INSTRUCTIONS*********************************")
print("You are guessing four numbers in a particular order ranging from 1 to " + str(difficulty) + ".")
print("The * symbols indicate how many of your numbers are correct, but not which.")
print("You will have 10 guesses before you are a loser. Guess right to be a winner!")
print("Format your guesses like this: 1234, 1414, 1111, etc. Duplicates are possible.")
print("******************************************************************************\n")
guessCount = 0
winner = False
while winner == False:
guessCount += 1
if guessCount > 10:
print("You lose!")
print("The board was: ", end='')
print(board)
break
print("Guess #" + str(guessCount), end='')
guessList = guessFunc(difficulty)
winner = compareLists(board, guessList)