-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
472 lines (448 loc) · 20 KB
/
main.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
from math import gcd
import pygame, random
from pygame import mixer
from Button import Button
from Posibilities import posibilities
from Scuare import Scuare
ScreenSize = (800,600)
Difficulty = 5
Multiplier = 25
#MaxScuares = (38,24)
#MineSize = (20,20)
pygame.init()
Font = pygame.font.SysFont('Comic Sans MS', 30)
LilFont = pygame.font.SysFont('Comic Sans MS',10)
Screen = pygame.display.set_mode(ScreenSize)
class Grid():#(38,24),(20,100),(20,20)
def __init__(self, screen, mines, cuantity, startpos, size):
self.screen = screen
self.mines = mines
self.flags = mines
self.started = False
self.cuantity = cuantity
self.size = size
self.startpos = startpos
#creates all images
self.scu1 = pygame.transform.smoothscale(pygame.image.load('Data/Graphics/1.png').convert(), size)
self.scu2 = pygame.transform.smoothscale(pygame.image.load('Data/Graphics/2.png').convert(), size)
self.scu3 = pygame.transform.smoothscale(pygame.image.load('Data/Graphics/3.png').convert(), size)
self.scu4 = pygame.transform.smoothscale(pygame.image.load('Data/Graphics/4.png').convert(), size)
self.scu5 = pygame.transform.smoothscale(pygame.image.load('Data/Graphics/5.png').convert(), size)
self.scu6 = pygame.transform.smoothscale(pygame.image.load('Data/Graphics/6.png').convert(), size)
self.scu7 = pygame.transform.smoothscale(pygame.image.load('Data/Graphics/7.png').convert(), size)
self.scu8 = pygame.transform.smoothscale(pygame.image.load('Data/Graphics/8.png').convert(), size)
self.flag = pygame.transform.smoothscale(pygame.image.load('Data/Graphics/flag.png').convert(), size)
self.scuare = pygame.transform.smoothscale(pygame.image.load('Data/Graphics/scuare.png').convert(), size)
self.empty = pygame.transform.smoothscale(pygame.image.load('Data/Graphics/empty.png').convert(), size)
self.mine = pygame.transform.smoothscale(pygame.image.load('Data/Graphics/mine.png').convert(), size)
self.explodedmine = pygame.transform.smoothscale(pygame.image.load('Data/Graphics/exploded mine.png').convert(), size)
#creates all the scuare objects
self.grid = pygame.sprite.Group()
self.matrix = [[Scuare(self.scuare,(0,0),(0,0)) for y in range(cuantity[1])] for x in range(cuantity[0])]
for x in range(cuantity[0]):
for y in range(cuantity[1]):
self.matrix[x][y] = Scuare(self.scuare,(startpos[0]+(x*size[0]),startpos[1]+(y*size[1])),size)
self.grid.add(self.matrix[x][y])
#connects all the scuares arround eachother
for x in range(cuantity[0]):
for y in range(cuantity[1]):
left = False
right = False
up = False
down = False
if x != 0:
self.matrix[x][y].arround.append(self.matrix[x-1][y])
left = True
if x != cuantity[0]-1:
self.matrix[x][y].arround.append(self.matrix[x+1][y])
right = True
if y != 0:
self.matrix[x][y].arround.append(self.matrix[x][y-1])
up = True
if y != cuantity[1]-1:
self.matrix[x][y].arround.append(self.matrix[x][y+1])
down = True
if up:
if left:
self.matrix[x][y].arround.append(self.matrix[x-1][y-1])
if right:
self.matrix[x][y].arround.append(self.matrix[x+1][y-1])
if down:
if left:
self.matrix[x][y].arround.append(self.matrix[x-1][y+1])
if right:
self.matrix[x][y].arround.append(self.matrix[x+1][y+1])
def image(self, scuare):#x
if not scuare.visible:
if scuare.flagged:
scuare.image = self.flag
else:
scuare.image = self.scuare
else:
if scuare.type == "empty":
scuare.image = self.empty
elif scuare.type == "mine":
scuare.image = self.mine
elif scuare.type == "explodedmine":
scuare.image = self.explodedmine
elif scuare.type == 1:
scuare.image = self.scu1
elif scuare.type == 2:
scuare.image = self.scu2
elif scuare.type == 3:
scuare.image = self.scu3
elif scuare.type == 4:
scuare.image = self.scu4
elif scuare.type == 5:
scuare.image = self.scu5
elif scuare.type == 6:
scuare.image = self.scu6
elif scuare.type == 7:
scuare.image = self.scu7
elif scuare.type == 8:
scuare.image = self.scu8
def leftclick(self, pos):#x
if self.started:
for scuare in self.grid:
scuare.high = False
scuare.probab = 0
for scuare in self.grid:
if pygame.Rect.collidepoint(pygame.Rect(scuare.rect[0],scuare.rect[1],scuare.size[0],scuare.size[1]),pos):
if not scuare.visible and not scuare.flagged:
if scuare.type == "mine":
scuare.type = "explodedmine"
self.revealmines()
scuare.visible = True
if scuare.type == "empty":
self.search()
self.image(scuare)
elif self.startpos[0]<pos[0] and pos[0]<self.startpos[0]+(self.cuantity[0]*self.size[0]) and self.startpos[1]<pos[1] and pos[1]<self.startpos[1]+(self.cuantity[1]*self.size[1]):
self.defineall(pos)
def rightclick(self, pos):#x
for scuare in self.grid:
scuare.high = False
scuare.probab = 0
for scuare in self.grid:
if pygame.Rect.collidepoint(pygame.Rect(scuare.rect[0],scuare.rect[1],scuare.size[0],scuare.size[1]),pos):
if not scuare.visible:
if scuare.flagged:#flag to scuare
scuare.flagged = False
self.image(scuare)
self.flags += 1
elif self.flags>0 and not scuare.flagged:#scuare to flag
scuare.flagged = True
self.image(scuare)
self.flags -= 1
def defineall(self,pos):#x
#places the mines
for scuare in self.grid:
if pygame.Rect.collidepoint(pygame.Rect(scuare.rect[0],scuare.rect[1],scuare.size[0],scuare.size[1]),pos):
if not scuare.visible and not scuare.flagged:
scuare.image = self.empty
scuare.visible = True
for i in range(self.mines):
added = False
while not added:
added = False
x = self.startpos[0] + (random.randint(0,self.cuantity[0]-1)*(self.size[0]))
y = self.startpos[1] + (random.randint(0,self.cuantity[1]-1)*(self.size[1]))
if not((scuare.rect[0]-21 < x and x < scuare.rect[0]+21) and (scuare.rect[1]-21 < y and y < scuare.rect[1]+21)):
for subscuare in self.grid:
if subscuare.type != "mine" and pygame.Rect.collidepoint(pygame.Rect(subscuare.rect[0],subscuare.rect[1],subscuare.size[0],subscuare.size[1]),(x,y)):
subscuare.type = "mine"
added = True
#adds mine proximity counter
for scuare in self.grid:
if scuare.type == "mine":
influence = [(scuare.rect[0]-20,scuare.rect[1]-20),(scuare.rect[0]+20,scuare.rect[1]-20),(scuare.rect[0]-20,scuare.rect[1]+20),(scuare.rect[0]+20,scuare.rect[1]+20),(scuare.rect[0]-20,scuare.rect[1]),(scuare.rect[0]+20,scuare.rect[1]),(scuare.rect[0],scuare.rect[1]-20),(scuare.rect[0],scuare.rect[1]+20)]
for i in influence:
for subscuare in self.grid:
if pygame.Rect.collidepoint(pygame.Rect(subscuare.rect[0],subscuare.rect[1],subscuare.size[0],subscuare.size[1]),i):
if subscuare.type == "empty":
subscuare.type = 1
elif type(subscuare.type) == int:
subscuare.type += 1
self.started = True
for scuare in self.grid:
scuare.checked = False
self.search()
def search(self):#x
looking = True
while looking:
looking = False
#ciclo de busqueda por cuadro
for scuare in self.grid:
if scuare.type == "empty" and scuare.visible and not scuare.checked:
scuare.checked = True
for a in scuare.arround:
if not a.visible:
looking = True
a.visible = True
self.image(a)
def revealmines(self):#x
for scuare in self.grid:
if scuare.type == "mine":
scuare.visible = True
scuare.image = self.mine
self.image(scuare)
def revealall(self):#x
for scuare in self.grid:
scuare.visible = True
self.image(scuare)
def minesleft(self):#x
minesleft = self.mines
lost = False
for scuare in self.grid:
if scuare.type == "mine" and scuare.flagged:
minesleft -= 1
if scuare.type == "explodedmine":
lost = True
if lost:
minesleft = -1
return minesleft
def FlagNReveal(self, scuare):
update = False
notvisible = 0
flags = 0
closemines = int(scuare.type)
for s in scuare.arround:#counts non visible and flagged scuares arround
if not s.visible:
notvisible += 1
if closemines == notvisible:# flags mines
update = True
scuare.checked = True
for s in scuare.arround:
if not s.visible:
s.checked = True
s.flagged = True
self.image(s)
flags -= 1
for s in scuare.arround:
if s.flagged:
flags += 1
if closemines == flags:# reveals non mines
update = True
scuare.checked = True
for s in scuare.arround:
if not s.visible and not s.flagged:
if s.type == "mine":
s.type = "explodedmine"
s.visible = True
self.image(s)
if s.type == "empty":
self.search()
#if neither probable = True
self.image(scuare)
return update
#ERROR VISUAL CUANDO GENERA TRUE TRUE
def Auto(self):#make show flags in real time
for scuare in self.grid:
scuare.high = False
scuare.posiblemines = 0
self.show()
workin = True
probable = False
#Shows with the 100% or 0% logic
while workin and not probable:
probable = True
for scuare in self.grid:
scuare.high = True
update = False
#does all the flagging and revealing
if not scuare.checked and scuare.visible and isinstance(scuare.type, int):#convertir minas a rojas
update = self.FlagNReveal(scuare)
if update:
probable = False
if update:
self.grid.draw(self.screen)
for x in range(self.startpos[0],self.startpos[0]+760+1,self.size[0]):
pygame.draw.line(self.screen,(0,100,0),(x,self.startpos[1]),(x,self.startpos[1]+480))
for y in range(self.startpos[1],self.startpos[1]+480+1,self.size[1]):
pygame.draw.line(self.screen,(0,100,0),(self.startpos[0],y),(self.startpos[0]+760,y))
for sc in self.grid:
if sc.high:
pygame.draw.rect(self.screen,(0,100,0),pygame.Rect(sc.rect,sc.size),1)
pygame.display.flip()
minesleft = self.minesleft()
if minesleft<1:
workin = False
self.flags = minesleft
for scuare in self.grid:
scuare.high = False
if probable:
for scuare in self.grid:
number = False
for sc in scuare.arround:
if isinstance(sc.type, int) and sc.visible:
number = True
if not scuare.visible and not scuare.flagged and number:
scuare.high = True
print(workin, update)
self.show()
pygame.display.flip()
#Shows otherwise
if probable:
for scuare in self.grid:
if scuare.high:
scuare.checked = True
else:
scuare.checked = False
#makes groups for each section of probability
groups = []
for scuare in self.grid:
if scuare.checked:
groups.append(self.Group(scuare))
if scuare.high:
n = 0
for sc in scuare.arround:
if isinstance(sc.type, int) and sc.visible:
ammount = 0
for subsc in sc.arround:
if subsc.flagged:
ammount += 1
n += int(sc.type) - ammount
scuare.posiblemines = n
print("group ammount:", len(groups))
#make all mine arangements and count # of mines used for each
for g in groups:
for sc in self.grid:
sc.checked = True
counter = posibilities(g)
print("counter:", counter)
for sc, n in zip(g, counter):
sc.probab = n
def Group(self, scuare):#highlighted = not checked
scuare.checked = False
g = [scuare]
done = False
while not done:
done = True
for sc in g:
for subsc in sc.arround:
if subsc.checked:
done = False
subsc.checked = False
g.append(subsc)
return g
def show(self):#x
#shows all scuares in the grid
self.grid.draw(self.screen)
for sc in self.grid:
if sc.probab != 0:
text = LilFont.render(str(sc.probab),True, (0,0,0))
text_rect = text.get_rect(center=(sc.rect[0]+sc.size[0]/2,sc.rect[1]+sc.size[1]/2))
self.screen.blit(text, text_rect)
#grid divisory lines
for x in range(self.startpos[0],self.startpos[0]+760+1,self.size[0]):
pygame.draw.line(self.screen,(0,100,0),(x,self.startpos[1]),(x,self.startpos[1]+480))
for y in range(self.startpos[1],self.startpos[1]+480+1,self.size[1]):
pygame.draw.line(self.screen,(0,100,0),(self.startpos[0],y),(self.startpos[0]+760,y))
#show highlight
for sc in self.grid:
if sc.high:
pygame.draw.rect(self.screen,(0,0,0),pygame.Rect(sc.rect,sc.size),2)
return self.flags
class Start():
def __init__(self, screen, font):
self.screen = screen
self.font = font
self.state = "start"
self.playbutton = Button(screen,(63, 97, 1),(0,0,0),self.font,20,20,240,60)
self.exitbutton = Button(screen,(63, 97, 1),(0,0,0),self.font,540,20,240,60)
def events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.state = "quit"
elif event.type == pygame.MOUSEBUTTONDOWN:
mousepos = pygame.mouse.get_pos()
if self.playbutton.check_click(mousepos):
self.state = "playing"
if self.exitbutton.check_click(mousepos):
self.state = "quit"
def show(self):
#background
self.screen.fill((169, 237, 43))
#shows start and exit
self.playbutton.show("Play")
self.exitbutton.show("Exit")
pygame.display.flip()
def run(self):
while self.state == "start":
self.events()
self.show()
return self.state
class Game():#playing leave win loose
def __init__(self, screen, font, difficulty, multiplier):
self.screen = screen
self.font = font
self.state = "playing"
self.mines = difficulty*multiplier
self.flags = difficulty*multiplier
self.autobutton = Button(screen,(63, 97, 1),(0,0,0),self.font,20,20,240,60)
self.flagsbutton = Button(screen,(63, 97, 1),(168, 27, 27),self.font,280,20,240,60)
self.exitbutton = Button(screen,(63, 97, 1),(0,0,0),self.font,540,20,240,60)
self.grid = Grid(screen,self.mines,(38,24),(20,100),(20,20))
def events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.MOUSEBUTTONDOWN:
state = pygame.mouse.get_pressed()
mousepos = pygame.mouse.get_pos()
if state[0]:#left click
if self.exitbutton.check_click(mousepos):
self.state = "start"
if self.grid.started and self.flagsbutton.check_click(mousepos):
self.grid.revealall()
if self.grid.started and self.autobutton.check_click(mousepos):
self.grid.Auto()
if self.state == "playing":
if 20<=mousepos[0] and mousepos[0]<=(20+(38*20)) and 100<=mousepos[1] and mousepos[1]<=(100+(24*20)):#agregar valores predet
self.grid.leftclick(mousepos)
if state[2]:#right click
if self.grid.started and self.state == "playing":
self.flags = self.grid.rightclick(mousepos)
if self.state == "playing":
minesleft = self.grid.minesleft()
if minesleft == -1:
self.state = "lose"
elif minesleft == 0:
self.state = "win"
else:
self.state = "playing"
def show(self):
#background
self.screen.fill((169, 237, 43))
#shows grid
self.flags = self.grid.show()
#shows auto flags and exit
self.autobutton.show("Auto")
self.flagsbutton.show(f"Flags: {self.flags}")
self.exitbutton.show("Exit")
pygame.display.flip()
def run(self):
while self.state == "playing":
self.events()
self.show()
if self.state == "lose" or self.state == "win":
print(self.state)
pygame.time.delay(2000)
return "start"
def main():#start playing quit win loose
"""mixer.pre_init(44100, 16, 2, 4096)
mixer.init()"""
pygame.event.set_allowed(pygame.QUIT)
pygame.event.set_allowed(pygame.MOUSEBUTTONDOWN)
pygame.display.set_caption("Mine Sweeper + Auto")
pygame.font.init()
gamestate = "start"
while gamestate != "quit":
if gamestate == "start":
start = Start(Screen,Font)
gamestate = start.run()
if gamestate == "playing":
game = Game(Screen,Font,Difficulty,Multiplier)
gamestate = game.run()
pygame.quit()
if __name__ == "__main__":
main()