minesweeper.py

Created by maelstudio

Created on October 18, 2023

5.66 KB

Touches

  • Déplacer le curseur: FLÈCHES

  • Creuser: OK

  • Placer/Retirer un drapeau: DEL


Règles du jeu

Le Démineur est un jeu de réflexion dont le but est de localiser des mines cachées dans une grille, avec pour seule indication le nombre de mines dans les zones adjacentes.

Avant de lancer une partie, sélectionnez le nombre de mines que vous voulez avec les flèches gauche/droite et appuyez sur OK pour confirmer.

En creusant des cases, vous révèlerez des cases portant des nombres ou des cases vides. Un nombre indique le nombre de mines adjacentes (même diagonalement), et une case vide signifie qu’il n’y a aucune mine adjacente.

Si vous pensez qu’une case contienne une mine, placez-y un drapeau. Les drapeaux vous aideront à tenir compte des emplacements des mines.

Si vous avez placez le bon nombre de drapeaux autour d’une case portant un nombre, vous pouvez appuyer sur OK sur la case pour creuser toutes les autres cases adjacentes . Cela vous fera gagner du temps! Mais si vous avez placé vos drapeaux sur les mauvaises cases, cette manipulation les fera exploser.

Vous gagnez la partie en ayant creusé toutes les cases ne contenant pas de mines. Essayez d’obtenir le meilleur temps!


Ceci est une version optimisée du code, pour prendre le moins de place possible sur la calculatrice. Vous trouverez la version complète ici.


A=True
from time import*
from math import floor,sqrt
from random import randint as n
from kandinsky import*
from kandinsky import draw_string as g
from kandinsky import fill_rect as f
from ion import keydown as k
gCS=[(142,204,57),(167,217,72)]
dCS=[(215,184,153),(229,194,159)]
cC=0,0,0
mBGC=255,0,0
mCS=[(0,0,0),(255,255,255)]
fCS=[(255,0,0),(220,0,0)]
tmCS=[(255,255,255),(255,190,0),(221,160,17),(0,0,0)]
tC=255,255,255
bC=73,117,43
nCS=[(255,255,255),(25,118,210),(56,142,60),(255,0,0),(0,0,132),(132,0,0),(123,31,162),(132,0,132),(0,0,0)]
fb='\n\n\n_____11\n_____1100\n_____110000\n_____11000000\n_____1100000000\n_____110000000000\n_____1100000000\n_____11000000\n_____110000\n_____1100\n_____11\n_____11\n_____11\n____1111'
mb='\n\n\n_________00\n_________00\n_____0_000000_0\n______00000000\n_____0011000000\n_____0011000000\n___00000000000000\n___00000000000000\n_____0000000000\n_____0000000000\n____0_00000000_0\n_______000000\n_________00\n_________00'
xb='00________________00\n_00______________00\n__00____________00\n___00__________00\n____00________00\n_____00______00\n______00____00\n_______00__00\n________0000\n_________00\n_________00\n________0000\n_______00__00\n______00____00\n_____00______00\n____00________00\n___00__________00\n__00____________00\n_00______________00\n00________________00'
tb='\n_______11111\n________222\n_______11111\n_____111222111\n____11222222211\n___1122000002211\n___1220003000221\n__112000030030211\n__122000030300221\n__122000033000221\n__122000030000221\n__112000000000211\n___1220000000221\n___1122000002211\n____11222222211\n_____111222111\n_______11111'
mc=20
sqz=20
sW=320
sH=200
gW=sW//sqz
gH=sH//sqz
ms=[]
d=[]
fd=[]
c=[gW//2,gH//2]
origin=[0,0]
stm=monotonic()
lm=monotonic()
def dgsq(xy,C,fill=A,t=1):
  x=xy[0]*sqz;y=xy[1]*sqz+(222-sH)
  if fill:f(x,y,sqz,sqz,C)
  else:dr(x,y,sqz,sqz,C,t)
def dr(x,y,w,h,C,t=1):f(x,y,w,t,C);f(x,y,t,h,C);f(x,y+h,w,-t,C);f(x+w,y,-t,h,C)
def db(b,xy,Cs):
  b=b.split('\n')
  for(y,row)in enumerate(b):
    for(x,pixel)in enumerate(row):
      if pixel!='_':set_pixel(xy[0]*sqz+x,xy[1]*sqz+(222-sH)+y,Cs[int(pixel)])
def a(xy,list):
  n=0
  for yo in[-1,0,1]:
    for xo in[-1,0,1]:
      if[xo,yo]==[0,0]:continue
      if[xy[0]+xo,xy[1]+yo]in list:n+=1
  return n
def j(xy):return sqrt((origin[0]-xy[0])**2+(origin[1]-xy[1])**2)
def m(xy):
  if xy in fd:fd.pop(fd.index(xy));dfcer()
  d.append(xy.copy())
  if a(xy,ms)>0:dcl(xy);dc();return
  hd=[];hd.append(xy.copy());h(xy,hd);origin[0]=xy[0];origin[1]=xy[1];hd.sort(key=j)
  for p in hd:
    dcl(p)
    if p==c:dc()
  dfcer()
def h(xy,hd):
  for yo in[-1,0,1]:
    for xo in[-1,0,1]:
      p=[xy[0]+xo,xy[1]+yo]
      if[xo,yo]==[0,0]or p in d or p[0]>gW-1 or p[0]<0 or p[1]>gH-1 or p[1]<0:continue
      if p in fd:fd.pop(fd.index(p))
      d.append(p.copy());hd.append(p.copy())
      if a(p,ms)==0:h(p,hd)
def dcl(xy):
  Ci=int(xy[0]%2==0 and xy[1]%2==0 or xy[0]%2!=0 and xy[1]%2!=0)
  if xy in d:
    dgsq(xy,dCS[Ci],A);n=a(xy,ms)
    if n>0:g(str(n),xy[0]*sqz+5,222-sH+xy[1]*sqz+1,nCS[n],dCS[Ci])
    return
  dgsq(xy,gCS[Ci],A)
  if xy in fd:db(fb,xy,fCS)
def dc():dgsq(c,cC,False,2)
def dg():
  for y in range(gH):
    for x in range(gW):dcl([x,y])
def dfcer():f(260,0,60,222-sH,bC);g(str(mc-len(fd))+'/'+str(mc),260,3,tC,bC)
def gtm():return'{:.1f}'.format(monotonic()-stm)
def dtm():f(50,0,40,222-sH,bC);g(str(gtm()),42,3,tC,bC)
def v(dd):
  f(105,0,120,222-sH,bC);g('YOU LOST !',110,3,tC,bC);origin[0]=c[0];origin[1]=c[1];fd.sort(key=j);ms.sort(key=j)
  for xy in fd:
    if not xy in ms:db(xb,xy,[(0,0,0)])
  for xy in dd:
    dgsq(xy,mBGC)
    if xy==c:dc()
  for xy in ms:
    if not xy in fd:db(mb,xy,mCS)
def pms():
  for i in range(mc):
    p=[n(0,gW-1),n(0,gH-1)]
    while p in ms or abs(p[0]-c[0])<=1 and abs(p[1]-c[1])<=1:p=[n(0,gW-1),n(0,gH-1)]
    ms.append(p)
def ch(xy):
  dd=[]
  for yo in[-1,0,1]:
    for xo in[-1,0,1]:
      p=[xy[0]+xo,xy[1]+yo]
      if[xo,yo]==[0,0]or p in d or p in fd or p[0]>gW-1 or p[0]<0 or p[1]>gH-1 or p[1]<0:continue
      if p in ms:dd.append(p);continue
      m(p)
  return dd
f(0,0,320,222-sH,bC)
g('Select mines count:',5,3,tC,bC)
g(str(mc),215,3,tC,bC)
g('OK →',275,3,tC,bC)
dg()
lch=monotonic()
while k(4)or k(52):0
while not(k(4)or k(52)):
  if floor(monotonic()*2)%2==0:f(200,0,10,222-sH,bC);f(240,0,10,222-sH,bC)
  else:g('<',200,3,tC,bC);g('>',240,3,tC,bC)
  if monotonic()-lch<.15:continue
  if k(0)or k(3):
    if k(0)and mc>15:mc-=1
    if k(3)and mc<99:mc+=1
    f(215,0,25,222-sH,bC);g(str(mc),215,3,tC,bC);lch=monotonic()
while A:
  f(0,0,320,222-sH,bC);db(fb,[12,-1],fCS);db(tb,[1,-1],tmCS);ms=[];d=[];fd=[];c=[gW//2,gH//2];stm=monotonic();dtm();dfcer();dg()
  for i in range(11):g('MINESWEEPER'[:i+1],105,3,tC,bC);sleep(.05)
  dc();stm=monotonic();ltm=gtm();ff=A
  while A:
    if gtm()!=ltm:dtm();ltm=gtm()
    if monotonic()-lm>.15:
      p=None
      for ky in[3,0,1,2]:
        if k(ky):p=ky;break
      if not p==None:
        lc=c.copy()
        if p==3:
          c[0]+=1
          if c[0]>gW-1:c[0]=0
        elif p==0:
          c[0]-=1
          if c[0]<0:c[0]=gW-1
        elif p==2:
          c[1]+=1
          if c[1]>gH-1:c[1]=0
        elif p==1:
          c[1]-=1
          if c[1]<0:c[1]=gH-1
        dc();dcl(lc);lm=monotonic()
    if(k(4)or k(52))and not c in fd:
      if ff:pms();ff=False
      if c in ms:v([c]);break
      if c in d and a(c,fd)==a(c,ms):
        dd=ch(c)
        if dd:v(dd);break
      elif not c in d:m(c)
      if len(d)+mc==gW*gH:f(105,0,120,222-sH,bC);g('YOU WON !',115,3,tC,bC);break
      while k(4)or k(52):0
    if k(17):
      if c in fd:fd.pop(fd.index(c))
      elif not c in d:fd.append(c.copy())
      dcl(c);dc();dfcer()
      while k(17):0
  while k(4)or k(52):0
  while not(k(4)or k(52)):0

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.