Skip to content

Instantly share code, notes, and snippets.

@Kaki-In
Created December 26, 2023 18:12
Show Gist options
  • Save Kaki-In/16ec5e14534074e76a8c8022786cf3dd to your computer and use it in GitHub Desktop.
Save Kaki-In/16ec5e14534074e76a8c8022786cf3dd to your computer and use it in GitHub Desktop.
MineSweeper
from kandinsky import *
from random import *
from ion import *
from time import *
class Mine():
def __init__(self,mode=0):
self.time=0
self.mode=mode
self.game=[[0 for i in range([9,15,30][mode])] for i in range([9,13,16][mode])]
self.last=[[20 for i in i] for i in self.game]
for i in range([10,40,99][self.mode]):
while True:
x=randrange(len(self.game[0]))
y=randrange(len(self.game))
if self.game[y][x]!=2 and not (x<=1 and y<=1):
self.game[y][x]=2
break
def mineAround(self,x,y):
m=0
if x:
if y:
if self.game[y-1][x-1]%4>=2:m+=1
if self.game[y][x-1]%4>=2:m+=1
if y<len(self.game)-1:
if self.game[y+1][x-1]%4>=2:m+=1
if y:
if self.game[y-1][x]%4>=2:m+=1
if y<len(self.game)-1:
if self.game[y+1][x]%4>=2:m+=1
if x<len(self.game[0])-1:
if y:
if self.game[y-1][x+1]%4>=2:m+=1
if self.game[y][x+1]%4>=2:m+=1
if y<len(self.game)-1:
if self.game[y+1][x+1]%4>=2:m+=1
return m
def show(self,ask=True):
for x in range(len(self.game[0])):
for y in range(len(self.game)):
if self.game[y][x]==self.last[y][x] and ask:continue
fill_rect(x*10,y*10,10,10,(255,255,255))
fill_rect(x*10,y*10,1,10,(0,0,0))
fill_rect(x*10,y*10,10,1,(0,0,0))
if self.game[y][x]%2:
if self.game[y][x]==3:fill_rect(x*10+1,y*10+1,9,9,(255,0,0))
if self.mineAround(x,y):
for i in range(self.mineAround(x,y)):
fill_rect(x*10+(i%3)*3+1,y*10+(i//3)*3+1,2,2,(255,0,0))
elif self.game[y][x]!=3:fill_rect(x*10+1,y*10+1,9,9,(0,200,0))
if self.game[y][x]>=4:
fill_rect(x*10+1,y*10+1,9,9,(255,180,50))
self.last[y][x]=self.game[y][x]
def main(self,tipa):
x=0
y=0
lastx=0
lasty=0
fill_rect(0,0,320,222,(0,0,0))
a=True
while not True in [3 in [j%4 for j in i] for i in self.game]:
if keydown(0):
a=False
while not a:
if x:
x-=1
elif y:
x=len(self.game[0])-1
y-=1
else:
x=len(self.game[0])-1
y=len(self.game)-1
if self.game[y][x]%2==0:a=True
if keydown(1):
a=False
while not a:
if y:
y-=1
elif x:
y=len(self.game)-1
x-=1
else:
x=len(self.game[0])-1
y=len(self.game)-1
if self.game[y][x]%2==0:a=True
if keydown(2):
a=False
while not a:
if y<len(self.game)-1:
y+=1
elif x<len(self.game[0])-1:
y=0
x+=1
else:
x=0
y=0
if self.game[y][x]%2==0:a=True
if keydown(3):
a=False
while not a:
if x<len(self.game[0])-1:
x+=1
elif y<len(self.game)-1:
x=0
y+=1
else:
x=0
y=0
if self.game[y][x]%2==0:a=True
if keydown(4) and not self.game[y][x]%2:
self.game[y][x]=self.game[y][x]%4+1
a=False
while not a:
if x<len(self.game[0])-1:
x+=1
elif y<len(self.game)-1:
x=0
y+=1
else:
x=0
y=0
if self.game[y][x]%2==0:a=True
if keydown(17) and (not self.game[y][x]%2) and (sum([sum([int(j>=4) for j in i]) for i in self.game])<[10,40,99][self.mode] or self.game[y][x]>=4):
if self.game[y][x]//4:self.game[y][x]-=4
else:self.game[y][x]+=4
a=False
while not a:
if x<len(self.game[0])-1:
x+=1
elif y<len(self.game)-1:
x=0
y+=1
else:
x=0
y=0
if self.game[y][x]%2==0:a=True
if sum([i.count(6) for i in self.game])==[10,40,99][self.mode]:return True
if a:
fill_rect(lastx*10+1,lasty*10+1,9,9,(255,255,255))
self.last[lasty][lastx]=20
lastx=x
lasty=y
self.show()
fill_rect(x*10+1,y*10+1,9,9,(0,0,255))
sleep(0.2)
a=False
draw_string("Drapeaux:"+str(sum([sum([int(j>=4) for j in i]) for i in self.game]))+"/"+str([10,40,99][self.mode])+" ",0,204,(255,180,50),(0,0,0))
draw_string(str(int(monotonic()-tipa)+self.time)+"s",0,186,(255,180,50),(0,0,0))
return False
def play(self):
tipa=monotonic()
result=None
try:result=self.main(tipa)
except KeyboardInterrupt:input("Jeu en pause (press OK)")
finally:
self.last=[[20 for i in i] for i in self.game]
self.time+=int(monotonic()-tipa)
if result!=None:
if result:
fill_rect(0,0,320,222,(0,100,0))
fill_rect(0,0,len(self.game[0])*10+1,len(self.game)*10+1,(0,0,0))
draw_string(str(self.time)+"s",0,186,(255,255,255),(0,100,0))
else:
fill_rect(0,0,320,222,(255,0,0))
fill_rect(0,0,len(self.game[0])*10+1,len(self.game)*10+1,(0,0,0))
for i in self.game:
for j in range(len(i)):
if i[j] in [2,6]:i[j]=3
draw_string(str(self.time)+"s",0,186,(0,0,0),(255,0,0))
self.show(False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment