-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMastermind.py
202 lines (157 loc) · 6.16 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
"""
In this game, the computer generates a secret "code", which is a list of four
colors e.g. "green green blue red". There are six possible colors: red, yellow,
blue, green, orange, and purple.
The object of the game is to win. The player wins by guessing the secret code.
Each turn, the player enters a guess for the code e.g. "blue orange yellow
blue". The computer then reports how many exact matches the player had, and how
many inexact matches the player had. An inexact match is when the player guesses
the color correctly, but not the position, and an exact match is when the player
guesses a color correctly and puts it in the correct position.
The player has ten turns to guess the secret code. If they do, they win!
Otherwise, the computer wins.
Author: Pedro Cruz
October 2021
"""
def print_colors(color_list):
print(color_list)
def get_guess(colors):
"""
This function asks for the user to enter four colors and checks if they are
valid input options. Parameter "colors" (the list of valid colors) is
required.
"""
colors_guessed = []
print("Please enter four legal colors. Your choices are: red, orange, "+
"yellow, green, blue, purple.")
for i in range(1,5):
guess = input("Enter color %d: "%(i))
while guess not in colors:
print()
print("%s is not a valid color."%(guess))
guess = input("Please choose from the following colors: red, " +
"orange, yellow, green, blue, purple: ")
colors_guessed.append(guess)
return colors_guessed
def generate_code(colors):
"""
This function generates the secret code that the user needs to guess.
Parameter "colors" (the list of valid colors) is required.
"""
from random import choice
secret_code = []
for i in range(4):
color = choice(colors)
secret_code.append(color)
return secret_code
def exact_matches(secret_code, guess, status):
"""
This function compares the user's guess and the secret code in order to
determine the number of exact matches, i.e. how many correct colors are in
the correct position. Parameters "secret_code" (the game's secret code),
"guess" (the colors entered by the user), and "status" (a list that will be
later given as feedback to the user) are required.
"""
exact_matches = 0
for i in range(4):
if guess[i] == secret_code[i]:
status[i] = "exact"
exact_matches += 1
return exact_matches
def inexact_matches(secret_code, guess, status):
"""
This function compares the user's guess and the secret code in order to
determine the number of inexact matches, i.e. how many correct colors are in
the wrong position. Parameters "secret_code" (the game's secret code),
"guess" (the colors entered by the user), and "status" (a list that will be
later given as feedback to the user) are required.
"""
inexact_matches = 0
for i in range(4):
if status[i] != "exact":
for j in range(4):
if guess[i] == secret_code[j]:
if status[j] == "":
inexact_matches += 1
status[j] = "inexact"
return inexact_matches
def print_introduction():
"""
This function prints the instructions of the game to the user. No parameters
required.
"""
print("""
Welcome to Mastermind!
Your goal is to guess a sequence of four colors.
Each color can be blue, green, red, orange, purple, or yellow.
Then I'll tell you how many times you guessed the correct color
in the correct position, and how many times you guessed the
correct color, but in the wrong position.
You have ten turns to guess the correct sequence.
Good luck!
""")
def is_game_over(num_exact_matches, turn):
"""
This function checks the conditions for the game to end and returns 'True'
in case they are met. Parameters "num_exact_matches" (the number of exact
matches between the secret code and the user's guess) and "turn" (the
current turn of the game) are required.
"""
game = False
if (num_exact_matches == 4) or (turn == 11):
game = True
return game
def player_won(num_exact_matches):
"""
This function checks the condition for the user to win the game and returns
'True' in case it is met. Parameter "num_exact_matches" (the number of
exact matches between the secret code and the user's guess) is required.
"""
won = False
if num_exact_matches == 4:
won = True
return won
def main():
"""
Here, we are putting the pieces together and structuring our game by passing
arguments along to the called functions. No parameters required for 'main.'
"""
color_list = ["red", "orange", "yellow", "green", "blue", "purple"]
print_introduction()
code = generate_code(color_list)
print(code)
turn = 0
for i in range(1,11):
status = ["","","",""]
print()
print("Turn %s:"%(i))
userGuess = get_guess(color_list)
exactMatches = exact_matches(code, userGuess, status)
inexactMatches = inexact_matches(code, userGuess, status)
print()
print("You guessed:")
print(*userGuess,sep = ", ")
if exactMatches == 1:
print("There is %d exact match." %(exactMatches))
else:
print("There are %d exact matches." %(exactMatches))
if inexactMatches == 1:
print("There is %d inexact match." %(inexactMatches))
else:
print("There are %d inexact matches." %(inexactMatches))
turn += 1
if is_game_over(exactMatches, inexactMatches) == True:
break
if player_won(exactMatches) == True:
print()
print("The answer was:\n%s.\n\nYou won!"%(code))
if turn == 1:
print("It took you %d turn to get the correct answer."%(turn))
else:
print("It took you %d turns to get the correct answer."%(turn))
else:
print()
print("Time's up! Sorry, buddy :(\n\nThe answer was:")
print(*code,sep = ", ")
print("\nTry playing again until you defeat me ;)")
main()