ATTENTION: CE PROGRAMME EXIGE TOUTE LA MEMOIRE DE LA CALCULATRICE (donc supprimer tous les autres programmes python présents avant de l’injecter).
Le jeu du démineur. Pour lancer une nouvelle partie, init_game(29), où 29 est le nombre de mines au total (minimum conseillé de 29 mines). L’écran est divisé en 16 cases pour la largeur et 11 pour la hauteur, donc les valeurs doivent être comprises entre 0 et 15 pour x et entre 0 et 10 pour y. Afin de marcher sur une case, walk(x,y), walk(0,0) faisant marcher sur la case haut gauche. Pour placer/enlever un drapeau, flag(x,y)/unflag(x,y).
from math import * import random import kandinsky as k black=k.color(0,0,0) red=k.color(220,50,0) grey=k.color(200,200,200) white=k.color(255,255,255) #frame data def draw_mine(x,y): k.fill_rect(x+3,y+3,15,15,black),k.fill_rect(x+6,y+6,9,9,red) def draw_flag(cx,cy): x,y=20*cx,20*cy k.fill_rect(x+5,y+4,10,3,red),k.fill_rect(x+7,y+6,8,3,red),k.fill_rect(x+10,y+8,5,3,red),k.fill_rect(x+14,y+2,3,19,black) def draw_unvisited(x,y): k.fill_rect(x,y,21,21,grey),k.fill_rect(x+3,y+3,15,15,white) def draw_empty(x,y): k.fill_rect(x,y,21,21,black),k.fill_rect(x+1,y+1,19,19,white) #number of surrounding mines def ismine(x,y): if x<0 or y<0 or y>10 or x>15: return 0 if abs(screen[y][x])%10==9 : return 1 if screen[y][x]!=9: return 0 def surround(x,y): return (ismine(x+1,y)+ismine(x+1,y-1)+ismine(x+1,y+1)+ismine(x,y+1)+ismine(x,y-1)+ismine(x-1,y+1)+ismine(x-1,y)+ismine(x-1,y-1)) def draw_surround(x,y): col=int((-255/7)*surround(x,y)+(2040/7)) k.draw_string("%s"%surround(x,y),20*x+5,20*y+2,k.color(0,col,0)) #init a new game def init_game(mines): global screen screen=[] for loop in range(11): screen.append([10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]) val=[] for loop in range(11): val.append([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]) for loop in range(mines): ry=random.randint(0,len(val)-1) rx=random.choice(val[ry]) val[ry].remove(rx) screen[ry][rx]=9 for cx in range(16): for cy in range(11): if screen[cy][cx]==10: screen[cy][cx]=surround(cx,cy) if surround(cx,cy)==0: screen[cy][cx]=10 #render the actual matrix def display(): for cx in range(16): for cy in range(11): if screen[cy][cx]>=1: draw_unvisited(cx*20,cy*20) if screen[cy][cx]==-10: draw_empty(cx*20,cy*20) if screen[cy][cx]==-9: draw_mine(cx*20,cy*20) if screen[cy][cx]<0 and screen[cy][cx]>-9: draw_surround(cx,cy) if screen[cy][cx]<-10: draw_flag(cx,cy) #flag a case def flag(x,y): screen[y][x]=-10-abs(screen[y][x]) display() #unflag a case def unflag(x,y): if screen[y][x]<-10: screen[y][x]=(abs(screen[y][x]))-10 walk(x,y) display() #walk on a case def walk(x,y): if screen[y][x]==9: print("perdu") screen[y][x]=-9 if screen[y][x]>0 and screen[y][x]<9: screen[y][x]=-screen[y][x] if screen[y][x]==10: lane(x,y) display() #invert the sign of an element def abso(x,y): if y>=0 and y<=10 and x<=15 and x>=0: screen[y][x]=-abs(screen[y][x]) #recursive algorithm whenever walking on an empty case def lane(x,y): if screen[y][x]==10: abso(x,y) if y+1<=10: if screen[y+1][x]==10: lane(x,y+1) if y-1>=0: if screen[y-1][x]==10: lane(x,y-1) if x+1<=15: if screen[y][x+1]==10: lane(x+1,y) if x-1>=0: if screen[y][x-1]==10: lane(x-1,y) abso(x+1,y),abso(x,y+1),abso(x-1,y),abso(x,y-1)