fromkandinskyimportset_pixelaspoly_set_pixel,fill_rectfromionimportkeydownaspoly_test_key,KEY_UP,KEY_DOWN,KEY_LEFT,KEY_RIGHT,KEY_EXEasKEY_ENTER,KEY_BACKasKEY_ESCscreen_w,screen_h=320,222defwait_key():while1:forkin(KEY_UP,KEY_DOWN,KEY_LEFT,KEY_RIGHT,KEY_ENTER,KEY_ESC):ifpoly_test_key(k):returnkSCALE=min(screen_w//128,screen_h//64)YELLOW,RED,BLUE,GREEN,ALL=0,1,2,3,4VOID,WALL,SPIKES,MONSTER,TRAP,EXIT=100,101,102,107,112,117GO_UP,GO_RIGHT,GO_DOWN,GO_LEFT,ATTACK,QUIT=10,11,12,13,14,15NEW_GAME,TRAP_APPEARED,SPIKES_APPEARED=20,21,22JAUNE,ROUGE,BLEU,VERT,TOUS=0,1,2,3,4VIDE,MUR,PICS,MONSTRE,PIEGE,SORTIE=100,101,102,107,112,117ALLER_HAUT,ALLER_DROITE,ALLER_BAS,ALLER_GAUCHE,ATTAQUER,QUITTER=10,11,12,13,14,15NOUVELLE_PARTIE,PIEGE_APPARU,PICS_APPARUS=20,21,22defri(rs,a,b):rs[0]=(rs[0]*214013+2531011)%4294967296r=(rs[0]//65536)&0x7fffreturnr%(b-a)+a### Rendering
defpx(x,y,c):forsxinrange(SCALE):forsyinrange(SCALE):poly_set_pixel(x*SCALE+sx,y*SCALE+sy,c)colors=[(77,71,63),(210,217,217),(241,233,103),(234,96,88),(97,156,236),(111,226,126),(178,126,206)]s=0x002a2a55005454aa00m=0x00005ae7bd18000000t=0x00182c4a5234180000e=0xf080e0c080e0c0f000f=0x0f0107030107030f00p=0x386565396dd7ff7d00tiles=[0,0xff818181818181ff01,s+2,s+3,s+4,s+5,s+6,m+2,m+3,m+4,m+5,m+6,t+2,t+3,t+4,t+5,t+6,f+2,e+3,f+4,e+5]sprites=[p+2,p+3,p+4,p+5]defrender_image(tile,x0,y0):fg,bg=colors[tile&0xff],colors[0]tile>>=8iftile:foryinrange(8):forxinrange(8):px(x0*8+7-x,y0*8+7-y,fgiftile&1elsebg)tile>>=1else:fill_rect(x0*8*SCALE,y0*8*SCALE,8*SCALE,8*SCALE,bg)defrender_game(board,players,old_players,update):forxyinupdate:ifxy==-1:foryinrange(8):forxinrange(16):render_image(tiles[board[16*y+x]-VOID],x,y)else:render_image(tiles[board[xy]-VOID],xy%16,xy//16)forxyinold_players:render_image(tiles[board[xy]-VOID],xy%16,xy//16)foriinrange(4):render_image(sprites[i],players[i]%16,players[i]//16)### API for submissions
definput_action():interactive_actions={KEY_UP:GO_UP,KEY_RIGHT:GO_RIGHT,KEY_DOWN:GO_DOWN,KEY_LEFT:GO_LEFT,KEY_ENTER:ATTACK,KEY_ESC:QUIT}i=-1whilenotiininteractive_actions.keys():i=wait_key()returninteractive_actions.get(i)defis_a(obj,kind):ifkind==VOIDorkind==WALL:returnobj==kindreturnkind<=obj<=kind+4defaffects(obj,player):return (is_a(obj,SPIKES)oris_a(obj,MONSTER)oris_a(obj,TRAP))andobj!=SPIKES+playerandobj!=MONSTER+playerandobj!=TRAP+playerpathfind_d=[-16,+16,-1,+1,GO_UP,GO_DOWN,GO_LEFT,GO_RIGHT]defpathfind(board,start,end):n=len(board)parent=[-1]*ndirections=[-1]*nqueue=[start]parent[start]=startcell=-1whilequeue:cell=queue.pop(0)ifcell==end:breakforiinrange(4):d=cell+pathfind_d[i]ifboard[d]!=WALLandparent[d]<0:parent[d]=celldirections[d]=pathfind_d[i+4]queue.append(d)ifcell==end:path=[]whilecell!=start:path=[directions[cell]]+pathcell=parent[cell]returnpathdemander_action,est_un,affecte,calculer_chemin=input_action,is_a,affects,pathfind### Board generator
defufc():return[iforiinrange(128)]defufr(uf,i):ifuf[i]==i:returnirep=ufr(uf,uf[i])uf[i]=repreturnrepdefufm(uf,i,j):uf[ufr(uf,i)]=ufr(uf,j)defgb(rs):board=[WALL]*128pp=[17,30,97,110]pe=[47,80,95,32]foriinrange(4):board[pp[i]]=VOIDboard[pe[i]]=EXIT+iuf=[iforiinrange(128)]whileany(ufr(uf,pp[i])!=ufr(uf,pe[i])foriinrange(4)):i=ri(rs,1,7)*16+ri(rs,1,15)ifboard[i]==WALLandany(board[n]!=VOIDfornin(i-16,i+16,i-1,i+1)):board[i]=VOIDfornin(i-16,i+16,i-1,i+1):ifboard[n]!=WALL:ufm(uf,i,n)foriinrange(128):ifboard[i]==VOIDandri(rs,0,16)<2:board[i]=[MONSTER,MONSTER,SPIKES,TRAP][ri(rs,0,4)]+ri(rs,0,5)returnboard### Game logic
defplay_board(rs,turn_function,blind):board=gb(rs)players=[17,30,97,110]old_players=[]ev=[(-1,-1,NEW_GAME,-1)]update=[-1]turns=0penalty=0whileTrue:ifnotblind:render_game(board,players,old_players,update)action=turn_function(board,players,ev)ev=[]update=[]old_players=players[:]ifactionisNone:continueifaction==QUIT:returnNoneturns+=1traps_activated=0forpinrange(4):coord=players[p]ifcoord<0:continueifaction==ATTACK:fordirectioninrange(4):dx=(direction==1)-(direction==3)dy=(direction==2)-(direction==0)i=coord+dx+16*dydest=board[i]ifis_a(dest,MONSTER):board[i]=VOIDupdate.append(i)elifis_a(dest,TRAP)andaffects(dest,p):traps_activated+=1board[i]=VOIDupdate.append(i)else:dx=(action==GO_RIGHT)-(action==GO_LEFT)dy=(action==GO_DOWN)-(action==GO_UP)i=coord+dx+16*dydest=board[i]ifdest==EXIT+p:players[p]=-1continueifdest==WALLoris_a(dest,EXIT):continueplayers[p]=iifnotaffects(dest,p):continueifis_a(dest,SPIKES):penalty+=10elifis_a(dest,MONSTER):penalty+=10board[i]=VOIDupdate.append(i)elifis_a(dest,TRAP):traps_activated+=1board[i]=VOIDupdate.append(i)whiletraps_activated>0:effect=ri(rs,0,3)ifeffect==0:penalty+=10x=ri(rs,1,15)y=ri(rs,1,7)ifboard[y*16+x]==VOIDandy*16+xnotinplayers:board[y*16+x]=(TRAPifeffect==1elseSPIKES)+ri(rs,0,4)ev.append((x,y,TRAP_APPEAREDifeffect==1elseSPIKES_APPEARED,p))update.append(y*16+x)traps_activated-=1ifall(p<0forpinplayers):score=150-penalty-turnsprint("Bravo! "+str(turns)+"T "+str(penalty)+"D -> "+str(score))returnscoredefplay_game(turn_function,blind=False,seed=0xc0ffee,maxgames=100):rs=[seed]games=0score=0whilegames!=maxgames:print("#"+str(games)+": "+str(rs[0]))board_score=play_board(rs,turn_function,blind)ifboard_scoreisNone:print("")print("Quit!")returnelse:score+=max(board_score,0)games+=1print("Games solved:",games)print("Score:",score)deftour_ask(plateau,joueurs,evenements):# Demander une action avec input()
returndemander_action()deftour_path(plateau,joueurs,evenements):globaljoueur_courant,chemin# Réinitialisation en début de partie
for (x,y,ev,joueur)inevenements:ifev==NOUVELLE_PARTIE:joueur_courant=0chemin=[]# Si le joueur est arrivé à sa destination, on passe au suivant.
# Il arrive que plusieurs joueurs sortent en un seul tour, si ça se produit
# on continue de passer au joueur suivant (il en reste forcément un qui
# n'est pas sorti sinon la partie serait finie).
whilejoueurs[joueur_courant]==-1:joueur_courant+=1chemin=[]# Chemin du joueur actuel vers sa sortie
ifchemin==[]:case_sortie=plateau.index(SORTIE+joueur_courant)chemin=calculer_chemin(plateau,joueurs[joueur_courant],case_sortie)# Prochaine étape
mouvement=chemin[0]chemin=chemin[1:]returnmouvement# Ordre des joueurs à sortir
ordre_de_sortie=[0,2,1,3]# Position du joueur qu'on veut sortir dans ordre_de_sortie
joueur_courant_id=0# Chemin pour le sortir
chemin=[]deftour_greedy(plateau,joueurs,evenements):globaljoueur_courant_id,cheminfor (x,y,ev,joueur)inevenements:ifev==NOUVELLE_PARTIE:joueur_courant_id=0chemin=[]# Si le joueur est arrivé à sa destination, on passe au suivant
whilejoueurs[ordre_de_sortie[joueur_courant_id]]==-1:joueur_courant_id+=1chemin=[]joueur_courant=ordre_de_sortie[joueur_courant_id]# Chemin du joueur actuel vers sa sortie
ifchemin==[]:case_sortie=plateau.index(SORTIE+joueur_courant)chemin=calculer_chemin(plateau,joueurs[joueur_courant],case_sortie)# S'il y a des monstres autour mais pas de piège, attaquer
monstres_autour=Falsepieges_autour=Falseforjoueurinjoueurs:# On ne compte pas les joueurs qui ont déjà sortis
ifjoueur!=-1:ifest_un(plateau[joueur-1],MONSTRE)or \
est_un(plateau[joueur+1],MONSTRE)or \
est_un(plateau[joueur-16],MONSTRE)or \
est_un(plateau[joueur+16],MONSTRE):monstres_autour=Trueifest_un(plateau[joueur-1],PIEGE)or \
est_un(plateau[joueur+1],PIEGE)or \
est_un(plateau[joueur-16],PIEGE)or \
est_un(plateau[joueur+16],PIEGE):pieges_autour=True# if monstres_autour and not pieges_autour:
# return ATTAQUER
# Prochaine étape
mouvement=chemin[0]chemin=chemin[1:]returnmouvementprint("play_game(tour_ask, blind=False)")print("play_game(tour_path, blind=True)")print("play_game(tour_greedy, blind=True)")
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.