# Minesweeper - April 2020
# Arthur Jacquin (arthur@jacquin.xyz)
# https://github.com/arthur-jacquin/numworks-games
''' Codes | Mine | Not a mine |
| Covered | 9 | 0 to 8 (nb of adjacent mines) |
| Uncovered | 19 | 10 to 18 (nb of adjacent mines + 10) |
| Flagged | 19 | 20 to 28 (nb of adjacent mines + 20) |
'''# Modules
fromionimportkeydownfromkandinskyimportset_pixel,draw_string,fill_rectfromrandomimportchoice# Parameters, tools
mines=20Xdomain,Ydomain=range(16),range(10)# Grid dimensions
prox=[[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]# Colors
imp=(0,0,0)# Title, cursor, content
grey=(70,70,70)# Text, borders
light=(220,220,220)# Unknown boxes
none=(255,255,255)# Empty boxes, background
curs=(0,0,255)# Cursor
numb=((0,0,255),(0,127,0),(255,0,0),(0,0,127),(127,0,0),(0,127,127),(0,0,0),(127,127,127))# Numbers
defwait(buttons=range(53)):# Wait for keypress
whileTrue:foriinbuttons:ifkeydown(i):whilekeydown(i):passreturnidefmine(x,y,b,a):# Draw a mine
fill_rect(x+2+4,y+2+4,9,9,b)fill_rect(x+4,y+6+4,13,1,b)fill_rect(x+6+4,y+4,1,13,b)fill_rect(x+4+4,y+4+4,2,2,(255,255,255))forpin[[9,2],[10,3],[10,9],[9,10],[3,10],[2,9],[2,3],[3,2]]:set_pixel(x+p[0]+4,y+p[1]+4,a)defcase(X,Y):# Draw a box
value=MAT[Y][X]x,y=20*X,20*Y+22fill_rect(x,y,20,20,grey)# Borders
ifvalue<10:# Unknown
fill_rect(x+1,y+1,18,18,light)elifvalue==10:# Empty
fill_rect(x+1,y+1,18,18,none)elifvalueinrange(11,19):# Number
fill_rect(x+1,y+1,18,18,none)draw_string(str(value-10),x+5,y+1,numb[value-11])elifvalue==19:# Uncovered mine
fill_rect(x+1,y+1,18,18,(255,0,0))mine(x,y,imp,(255,0,0))elifvalue>19:# Flag
fill_rect(x+1,y+1,18,18,light)fill_rect(x+10,y+8,2,6,imp)fill_rect(x+6,y+14,8,2,imp)fill_rect(x+6,y+4,6,4,(255,0,0))ifcursor==[X,Y]:# Cursor
fill_rect(x,y,20,3,curs)fill_rect(x,y,3,20,curs)fill_rect(x,y+17,20,3,curs)fill_rect(x+17,y,3,20,curs)defsearch(old):# Search around for empty boxes
new=[]foriinold:forpinprox:tX,tY=i[0]+p[0],i[1]+p[1]iftXinXdomainandtYinYdomain:ifMAT[tY][tX]==0:new.append([tX,tY])ifnot(MAT[tY][tX]inrange(10,19)):MAT[tY][tX]=(MAT[tY][tX])%10+10case(tX,tY)ifnew!=[]:search(new)whileTrue:# Initialisation
compteur=mines# Displayed discovered mines count
decouv=0# Real count
cursor=[8,5]# Cursor coordinates
MAT=[[0]*len(Xdomain)]*len(Ydomain)# Empty matrix
# Interface drawing
draw_string("MINESWEEPER",105,2,imp)draw_string((""+str(compteur)+"/"+(str(mines)+"")[:2])[-5:],265,2,grey)mine(245,0,grey,none)fill_rect(0,21,320,1,grey)forYinYdomain:forXinXdomain:case(X,Y)# Main loop
whileTrue:X,Y=cursorresult=wait([0,1,2,3,4,17])# Get keypress
ifresult<4:# Cursor movement
ifresult==0andcursor[0]-1inXdomain:cursor[0]-=1elifresult==1andcursor[1]-1inYdomain:cursor[1]-=1elifresult==2andcursor[1]+1inYdomain:cursor[1]+=1elifresult==3andcursor[0]+1inXdomain:cursor[0]+=1case(X,Y)case(cursor[0],cursor[1])elifresult==4:# Mine clearance
ifMAT==[[0]*len(Xdomain)]*len(Ydomain):# Matrix generation
bombes=[]foriinrange(mines):x,y=choice(Xdomain),choice(Ydomain)while (x,y)inbombesor(x,y)==(X,Y):x,y=choice(Xdomain),choice(Ydomain)bombes.append((x,y))MAT=[]foryinYdomain:column=[]forxinXdomain:if (x,y)inbombes:column.append(9)else:column.append(sum([1*((x+t[0],y+t[1])inbombes)fortinprox]))MAT.append(column)ifMAT[Y][X]>19:compteur+=1draw_string((""+str(compteur))[-2:],265,2,grey)ifMAT[Y][X]inrange(11,19):# Automated mine clearance
whileTrue:flags=0fortinprox:ifX+t[0]inXdomainandY+t[1]inYdomain:ifMAT[Y+t[1]][X+t[0]]==9:breakelifMAT[Y+t[1]][X+t[0]]==29:flags+=1ifMAT[Y][X]-10==flags:fortinprox:ifX+t[0]inXdomainandY+t[1]inYdomainandMAT[Y+t[1]][X+t[0]]<9:MAT[Y+t[1]][X+t[0]]=(MAT[Y+t[1]][X+t[0]])%10+10case(X+t[0],Y+t[1])ifMAT[Y+t[1]][X+t[0]]==10:search([[X+t[0],Y+t[1]]])breakMAT[Y][X]=(MAT[Y][X])%10+10case(X,Y)ifMAT[Y][X]==10:search([[X,Y]])# Automated search
elifMAT[Y][X]==19:# Lose
forxinXdomain:foryinYdomain:if (MAT[y][x])%10==9:# Reveal the mines
MAT[y][x]=19case(x,y)elifMAT[y][x]>19:# Strike misplaced flags
foriinrange(18):fill_rect(20*x+i,20*y+23+i,3,1,imp)fill_rect(20*x+17-i,20*y+23+i,3,1,imp)fill_rect(0,0,320,21,none)draw_string("YOU LOST !",110,2,(255,0,0))breakelifresult==17:# (Un)Flagging
ifMAT[Y][X]<20:sens=1else:sens=-1compteur-=sensif (MAT[Y][X])%10==9:decouv+=sensMAT[Y][X]=(MAT[Y][X])%10+20*(sens==1)draw_string((""+str(compteur))[-2:],265,2,grey)case(X,Y)ifcompteur==0anddecouv==mines:# Win
forxinXdomain:foryinYdomain:ifMAT[y][x]<9:MAT[y][x]=(MAT[y][x])%10+10case(x,y)fill_rect(0,0,320,21,none)draw_string("YOU WON !",115,2,(0,153,0))cursor=[42,120]case(X,Y)breakwhilenot(keydown(4)):pass
During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:
Ensure the proper functioning of the site (essential cookies); and
Track your browsing to send you personalized communications if you have created a professional account on the site and can be contacted (audience measurement cookies).
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.