-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMastermind_GUI_VERSION.py
More file actions
244 lines (210 loc) · 9.83 KB
/
Mastermind_GUI_VERSION.py
File metadata and controls
244 lines (210 loc) · 9.83 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import tkinter as tk
import numpy as np
import sys
import os
screen = tk.Tk()
screen.title('Mastermind Game')
screen.geometry("400x600")
screen.resizable(0, 0)
colorguessed = []
colorFillCount = 0
def codemaker():
""" generates 4 random colors for the code-maker and returns a list
"""
colorguess = np.random.choice(['yellow','blue', 'green', 'red','magenta', 'cyan'], 4)
nonNumpyComp = []
for i in colorguess:
nonNumpyComp.append(i)
colorguess = nonNumpyComp[:]
return colorguess
if colorFillCount == 0:
computerColor = codemaker()
def hintColor(guesses, compCode = computerColor):
"""
:param guesses: list--takes the user input and determine hint(black or white) based on codemarker
:return: list -- with elements 'black' and 'white'
"""
initialX = len(guesses) - 4
endX = len(guesses)
hint = []
compCod = compCode[:]
guess = guesses[initialX:endX]
hint = []
compCod = compCode[:]
for i in range(len(guess)):
if guess[i] == compCode[i]:
hint.append("BLACK")
guess[i] = 'remove'
compCod[i] = 'jack'
for i, j in zip(range(len(guess)), guess):
if j in compCod:
hint.append("WHITE")
compCod.remove(j)
return hint
def winDetermine(listofHint):
"""
:param listofHint: list--to determine if the guess match the computer code
:return: boolean--true if the color match and false o.t
"""
allBlack = False
if len(listofHint) == 4:
for i in listofHint:
if i == 'WHITE':
return allBlack
else:
allBlack = True
continue
return allBlack
firstRowBut_height = 4
firstRowBut_width = 400
wn = tk.Frame(screen, relief='groove', borderwidth=5, bg='#DCDCDC', width=firstRowBut_width, height=firstRowBut_height)
wn.pack_propagate(0) #Don't allow the widgets inside to determine the frame's width / height
wn.pack(fill=tk.BOTH, expand=1)
secRowBut_height = 4
secRowBut_width = 360
wn1 = tk.Frame(screen, width=secRowBut_width, height=secRowBut_height, bg='#808080')
wn1.pack_propagate(0) #Don't allow the widgets inside to determine the frame's width / height
wn1.pack(fill=tk.BOTH, expand=1)
trdRowBut_height = 600-secRowBut_height-firstRowBut_height
trdRowBut_width = 400
wnCanvas = tk.Canvas(screen, height=trdRowBut_height, relief='sunken', borderwidth=7, width=trdRowBut_width, bg='#808080')
wnCanvas.pack_propagate(0) #Don't allow the widgets inside to determine the frame's width / height
wnCanvas.pack(fill=tk.BOTH, expand=1)
dividerHeight = 600-secRowBut_height-firstRowBut_height-70
dividerWidth = 20
dividerFrame = tk.Frame(wnCanvas, width=dividerWidth, height=dividerHeight, borderwidth=0.5, bg='#DCDCDC', relief='raised')
dividerFrame.pack_propagate(0) #Don't allow the widgets inside to determine the frame's width / height
dividerFrame.pack(anchor='center', expand=0)
#ttk.Separator(screen, orient='vertical')
beginLX = 25
beginTopY = 20
beginRghtX = beginLX+28
beginBotY = beginTopY+28
space = 13 #16 ideal
circleName = ['one','two','three', 'four', 'five','six','seven','eight','nine','ten',
'eleven','twelve','thirteen','fourteen', 'fifteen','sixteen','seventeen','eighteen', 'ninteen',
'twenty','twenty_one','twenty_two', 'twenty_three', 'twenty_four','twenty_five','twenty_six',
'twenty_seven','twenty_eight','twenty_nine','thirty','thirty_one','thirty_two',
'thirty_three','thirty_four','thirty_five','thirty_six','thirty_seven','thirty_eight',
'thirty_nine', 'forty','forty_one','forty_two','forty_three','forty_four','forty_five',
'forty_six','forty_seven','forty_eight']
circleName2 = ['forty_nine','fifty','fifty_one','fifty_two','fifty_three','fifty_four',
'fifty_five','fifty_six','fifty_seven','fifty_eight','fifty_nine','sixty','sixty_one','sixty_two', 'sixty_three',
'sixty_four','sixty_five','sixty_six','sixty_seven','sixty_eight','sixty_nine','seventy','seventy_one','seventy_two',
'seventy_three','seventy_four','seventy_five','seventy_six','seventy_seven','seventy_eight','seventy_night','eighty',
'eighty_one','eighty_two','eighty_three','eighty_four','eighty_five','eighty_six','eighty_seven','eighty_eight',
'eighty_nine','ninty','ninty_one','ninty_two','ninty_three','ninty_four','ninty_five','ninty_six']
for j, i in zip(range(1,len(circleName)+1), circleName):
if j % 4 == 0:
circleName[j - 1] = wnCanvas.create_oval(beginLX, beginTopY, beginRghtX, beginBotY, outline='#696969')
beginTopY = beginBotY+space
beginBotY = beginTopY + 28
beginLX = 25
beginRghtX = beginLX + 28
else:
circleName[j - 1] = wnCanvas.create_oval(beginLX, beginTopY, beginRghtX, beginBotY, outline='#696969')
beginLX = beginRghtX + space
beginRghtX = beginLX + 28
beginLX_2 = 220
beginTopY_2 = 26
beginRghtX_2 = beginLX_2 + 20
beginBotY_2 = beginTopY_2 + 20
space_y = 20
for j, i in zip(range(1,len(circleName2)+1), circleName2):
if j % 4 == 0:
circleName2[j - 1] = wnCanvas.create_oval(beginLX_2, beginTopY_2, beginRghtX_2, beginBotY_2, outline='#696969')
beginTopY_2 = beginBotY_2 + space_y
beginBotY_2 = beginTopY_2 + 21
beginLX_2 = 220
beginRghtX_2 = beginLX_2 + 20
else:
circleName2[j - 1] = wnCanvas.create_oval(beginLX_2, beginTopY_2, beginRghtX_2, beginBotY_2, outline='#696969')
beginLX_2 = beginRghtX_2 + space_y
beginRghtX_2 = beginLX_2 + 20
def buttonRedClick():
global colorguessed, colorFillCount
wnCanvas.itemconfig(circleName[colorFillCount], fill ='red')
colorFillCount += 1
colorguessed.append('red')
def buttonBlueClick():
global colorguessed, colorFillCount
wnCanvas.itemconfig(circleName[colorFillCount], fill='blue')
colorFillCount += 1
colorguessed.append('blue')
def buttonGreenClick():
global colorguessed, colorFillCount
wnCanvas.itemconfig(circleName[colorFillCount], fill ='green')
colorFillCount += 1
colorguessed.append('green')
def buttonYellowClick():
global colorguessed, colorFillCount
wnCanvas.itemconfig(circleName[colorFillCount], fill ='yellow')
colorFillCount += 1
colorguessed.append('yellow')
def buttonCyanClick():
global colorguessed, colorFillCount
wnCanvas.itemconfig(circleName[colorFillCount], fill ='cyan')
colorFillCount += 1
colorguessed.append('cyan')
def buttonMagentaClick():
global colorguessed, colorFillCount
wnCanvas.itemconfig(circleName[colorFillCount], fill ='magenta')
colorFillCount += 1
colorguessed.append('magenta')
def fillHintColor(colorlist, computercol = computerColor):
"""accepts two lists, and fills the circles with appropriate colors"""
initialX = len(colorlist)-4
endX = len(colorlist)
hint = hintColor(colorlist, computercol)
if len(hint)==0:
return
elif len(hint)==1:
wnCanvas.itemconfig(circleName2[initialX], fill=hint[0])
elif len(hint)==2:
wnCanvas.itemconfig(circleName2[initialX], fill=hint[0])
wnCanvas.itemconfig(circleName2[initialX+1], fill=hint[1])
elif len(hint)==3:
wnCanvas.itemconfig(circleName2[initialX], fill=hint[0])
wnCanvas.itemconfig(circleName2[initialX + 1], fill=hint[1])
wnCanvas.itemconfig(circleName2[initialX + 2], fill=hint[2])
else:
wnCanvas.itemconfig(circleName2[initialX], fill=hint[0])
wnCanvas.itemconfig(circleName2[initialX + 1], fill=hint[1])
wnCanvas.itemconfig(circleName2[initialX + 2], fill=hint[2])
wnCanvas.itemconfig(circleName2[initialX + 3], fill=hint[3])
def winMessage():
from tkinter import messagebox
messagebox.showinfo("WON", "Good Job, You Won!")
def loseMessage():
from tkinter import messagebox
messagebox.showinfo("LOST", "Sorry, you've ran out of guesses.")
def restartButton():
""" Restarts the game by re-running the entire code"""
python = sys.executable
os.execl(python, python, * sys.argv)
def checkButton():
"""determines win and call appropriate function"""
global colorguessed, computerColor, colorFillCount
if len(colorguessed) % 4 == 0:
fillHintColor(colorguessed, computerColor)
if winDetermine(hintColor(colorguessed, computerColor)):
winMessage()
elif colorFillCount >= 48:
loseMessage()
buttonRestart = tk.Button(wn, text='RESTART', width=8, bg='#808080', command=restartButton)
buttonRestart.grid(row=0, column=0, padx=[345/2,398/2], pady=[5, 5])
buttonRed = tk.Button(wn1,borderwidth=1, relief='raised', width=2, height=1, bg='red', command=buttonRedClick)
buttonBlue = tk.Button(wn1, borderwidth=1, relief='raised', width=2, height=1, bg ='blue', command=buttonBlueClick)
buttonYellow = tk.Button(wn1, borderwidth=1, relief='raised', width=2, height=1, bg='yellow', command=buttonYellowClick)
buttonGreen = tk.Button(wn1, borderwidth=1, relief='raised',width=2, height=1, bg ='green', command=buttonGreenClick)
buttonCyan = tk.Button(wn1, borderwidth=1, relief='raised', width=2, height=1, bg='cyan', command=buttonCyanClick)
buttonMagenta = tk.Button(wn1, borderwidth=1, width=2, relief='flat', height=1, bg ='magenta', command=buttonMagentaClick)
buttonCheck = tk.Button(wn1, text='CHECK', width=8, bg='#DCDCDC',fg='blue', command=checkButton)
buttonCheck.grid(row=0, column=12, padx=[135,5], pady=[5, 5])
buttonBlue.grid(row=0, column=0, padx=4)
buttonCyan.grid(row=0, column=2, padx=4)
buttonGreen.grid(row=0, column=4, padx=4)
buttonMagenta.grid(row=0, column=6, padx=4)
buttonYellow.grid(row=0, column=8, padx=4)
buttonRed.grid(row=0, column=10, padx=4)
screen.mainloop()