labymini_2.py

Created by mathieu-croslacoste

Created on September 16, 2025

2.62 KB

compressed https://my.numworks.com/python/pardouin/labylowmini


f='#S$@E';e='$';d=zip;c=dict;b=range;V='#';K='E';J='.';D=int;n=-1
from kandinsky import fill_rect as W
from ion import keydown as G
from math import tan,cos,sin,pi,ceil as O
from random import choice as X
def M(wall):
 P=None;A=[[wall]*B for A in b(B)];A[1][1]=A[1][2]=A[1][3]=J;N=[(0,1,2,3),(0,1,3,2),(0,2,1,3),(0,2,3,1),(0,3,1,2),(0,3,2,1),(1,0,2,3),(1,0,3,2),(1,2,0,3),(1,2,3,0),(1,3,0,2),(1,3,2,0),(2,0,1,3),(2,0,3,1),(2,1,0,3),(2,1,3,0),(2,3,0,1),(2,3,1,0),(3,0,1,2),(3,0,2,1),(3,1,0,2),(3,1,2,0),(3,2,0,1),(3,2,1,0)];I=[(1,3,0)];E,H,M=P,P,0
 while I:
  F,G,L=I.pop()
  for O in X(N):
   C,D=[(F+2,G),(F-2,G),(F,G+2),(F,G-2)][O]
   if 0<=C<B and 0<=D<B and A[C][D]!='.':
    A[C][D]=A[(C+F)//2][(D+G)//2]='.'
    if L>M and(C in(1,B-2)or D in(1,B-2)):M,E,H=L,C,D
    I.append((C,D,L+1))
 A[1][0]='S'
 if E==1:A[0][H]=K
 elif E==B-2:A[B-1][H]=K
 elif H==1:A[E][0]=K
 else:A[E][B-1]=K
 return A
def Y(X,Y,angle):
 R=1;A=angle
 if A<=-180:A+=360
 elif A>180:A-=360
 K=float('inf')
 if A!=0 and A!=180:
  L=1/T(A);E=A<0;H=Y//100*100 if E else O(Y/100)*100;I=X+(H-Y)*L
  while R:
   F,G=D(H/100)-1*E,D(I/100)
   if not 0<=F<B or not 0<=G<B or C[F][G]!='.':break
   H+=100*n**E;I+=100*L*n**E
  K,M=S(X,Y,I,H),R
  if 0<=F<B and 0<=G<B:N=C[F][G]
 if A!=90 and A!=-90:
  P=T(A);E=A>90 or A<-90;I=X//100*100 if E else O(X/100)*100;H=Y+(I-X)*P
  while R:
   F,G=D(H/100),D(I/100)-1*E
   if not 0<=F<B or not 0<=G<B or C[F][G]!='.':break
   I+=100*n**E;H+=100*P*n**E
  Q=S(X,Y,I,H)
  if Q<K:
   K,M=Q,0
   if 0<=F<B and 0<=G<B:N=C[F][G]
 return K,M,N
def H():
 C=[(0,0,320,222,(135,206,235)),(0,111,320,111,(139,69,19))]
 for D in b(108):G=(D+1)//2*n**D+D%2*108-54;B=A+G*5/9;I,J,K=Y(E,F,B if B%45 else B+.05);H=round(14000/(I*N(A-B)));M,O,P=[Z,a][J][K];C.append((160+3*G,111-H,3,2*H,(M,O,P)))
 for(Q,R,S,T,U)in C:W(Q,R,S,T,U)
 del C
def P(s):
 global E,F,A,C,I
 if A%90:return
 B,G=E-s*100*U(A),F+s*100*N(A)
 if C[G//100][B//100]==J:E,F=B,G;H()
 elif C[G//100][B//100]==K:I+=1;C,E,F,A=M(V if I%5 else e),150,150,0;H()
def Q(s):
 global A;A+=s*45
 if A<=-180 or A>180:A-=s*360
 H()
def R(s,c):
 global E,F,A,C,I
 if A%90:return
 B,G=E+s*100*N(A),F+s*100*U(A)
 if C[D(G/100)][D(B/100)]==J:E,F=B,G;H()
 elif C[D(G/100)][D(B/100)]==K:I+=1;C,E,F,A=M(c if I%5 else e),150,150,0;H()
S=lambda X,Y,x,y:((X-x)**2+(Y-y)**2)**.5
T,N,U=lambda a:tan(a/180*pi),lambda a:cos(a/180*pi),lambda a:sin(a/180*pi)
B=15
C,E,F,A,I=M(V),150,150,0,1
Z=c(d(f,[(229,)*3,(255,0,0),(255,215,0),(72,118,255),(0,238,0)]))
a=c(d(f,[(179,)*3,(205,0,0),(205,173,0),(58,95,205),(0,205,0)]))
H()
while 1-G(5):
 if G(33):P(-1)
 if G(34):P(1)
 if G(0):Q(-1)
 if G(3):Q(1)
 if G(1):R(1,V)
 if G(2):R(-1,'@')

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.