-
Notifications
You must be signed in to change notification settings - Fork 1
/
Mastermind2.py
141 lines (101 loc) · 3.07 KB
/
Mastermind2.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
from tkinter import *
import random
#https://pypi.org/project/tkmacosx/
#pip3 install tkmacosx
from tkmacosx import Button
currRow = []
Rows = []
PegColors = ['magenta2','cyan','purple2','teal','yellow','dark green']
Solution = []
def DrawRow(currRow, row):
offset = 20
size = 20
spacing = 25
# draw the current row
col = 0
for pc in currRow:
canvas1.create_oval(spacing * col + offset,
spacing * row + offset,
spacing * col + size + offset ,
spacing * row + size + offset, fill=pc)
col += 1
pass
def DrawBoard(status):
canvas1.delete(ALL)
row = 0
#draw the rows
for r in Rows:
DrawRow(r, row)
row += 1
DrawRow(currRow, row)
#draw the solution... just for testing.
DrawRow(Solution, 12)
canvas1.create_text(80,380,text = status)
pass
def Color(c):
global currRow
global Rows5
if len(currRow) < 4:
currRow.append(c)
DrawBoard('keep choosing')
pass
def Delete():
pass
def Check():
global currRow
global Rows
tempRow = currRow.copy()
tempSol = Solution.copy()
totalBlack = 0
status = ''
if len(currRow) == 4:
# check to see if we win
for i in range(len(Solution)):
if currRow[i] == Solution[i]:
currRow.append('black')
totalBlack += 1
tempSol[i] = 'X'
tempRow[i] = 'X'
for i in range(len(tempRow)):
if tempRow[i] in tempSol and tempRow[i] != 'X':
currRow.append('pink')
tempSol.remove(tempRow[i])
Rows.append(currRow.copy())
currRow = []
if totalBlack == 4:
print('you win!')
status = 'you win!'
DrawBoard(status)
#print(Rows)
pass
root = Tk()
root.title("MasterMind")
root.configure(background = 'white')
frame1 = Frame(root, bg='sky blue', bd= 10)
frame1.pack(side=TOP)
canvas1 = Canvas(frame1, width = 250, height=400)
canvas1.pack()
frame2 = Frame(root )
c1 = Button(frame2, text=" ", borderless=1, bg=PegColors[0], command=lambda: Color(PegColors[0]))
c1.pack(side=LEFT)
c2 = Button(frame2, text=" ", borderless=1, bg=PegColors[1], command=lambda: Color(PegColors[1]))
c2.pack(side=LEFT)
c3 = Button(frame2, text=" ", borderless=1, bg=PegColors[2], command=lambda: Color(PegColors[2]))
c3.pack(side=LEFT)
c4 = Button(frame2, text=" ", borderless=1, bg=PegColors[3], command=lambda: Color(PegColors[3]))
c4.pack(side=LEFT)
c5 = Button(frame2, text=" ", borderless=1, bg=PegColors[4], command=lambda: Color(PegColors[4]))
c5.pack(side=LEFT)
c6 = Button(frame2, text=" ", borderless=1, bg=PegColors[5], command=lambda: Color(PegColors[5]))
c6.pack(side=LEFT)
b1 = Button(frame2, text="Delete", command=Delete)
b1.pack(side=RIGHT)
b2 = Button(frame2, text="Check", command=Check)
b2.pack(side=RIGHT)
frame2.pack(side=BOTTOM)
#generate a solution - randomly
for i in range(4):
Solution.append(PegColors[random.randint(0,len(PegColors)-1)])
print(Solution)
DrawBoard('Choose a peg')
mainloop()