Page 1 of 2

Petite erreur pour génération de labyrinthe en python

Unread postPosted: 22 Apr 2022, 19:38
by Altaear
Hello j'ai un petit problème avec python sur calculatrice,
J'ai récupéré ce bout de code pour générer un labyrinthe mais il m'affiche quelques erreurs, est ce que quelqu'un sait ou j'ai pu me gourrer ?
Code: Select all
from ti_draw import *
from random import *

def vline(x,y,w):
  if w:
    x1=x*wl+9
    y1=y*wl+3
    draw_line(x1,y1,x1,y1+wl)

def hline(x,y,w):
  if w:
    x1=x*wl+9
    y1=y*wl+3
    draw_line(x1,y1,x1+wl,y1)

def cell(x,y):
  z=a[x+y*w]
  hline(x,y,z&1)
  vline(x+1,y,z&2 )
  hline(x,y+1,z&4)
  vline(x,y,z&8)

def disp():
  clear
  for x in range(w):
    for y in range(h):
      cell(x,y)

def nor(u):
  v=u-w
  if v>0:
    if a[v]==15:
      a[u]&=14
      a[v]&=11
      return v
    return u

def eas(u):
  v=u+1
  if v%w:
    if a[v]==15:
      a[u]&13
      a[v]&7
      return v
    return u

def sou(u):
  v=u+w
  if v<n:
    if a[v]==15:
      a[u]&=11
      a[v]&=14
      return v
    return u

def wes(u):
  v=u-1
  if u%w:
    if a[v]==15:
      a[u]&=7
      a[v]&=13
      return v
    return u

def move(u):
  for i in range(c):
    d=randrange(4)
    if d==0:
      u=nor(u)
    if d==1:
      u=eas(u)
    if d==2:
      u=sou(u)
    if d==3:
      u=wes(u)

# Initialize
w=25
h=17
n=w*h
c=n/4
wl=12
a=[15 for x in range(n)]
t=[x for x in range(n)]
a[0]=7
clear()
draw_text(125,110,"-thinking-")

while 1:
 
  # Shuffle the index
  for i in range(n):
    j=randrange(n)
    t[i],t[j]=t[j],t[i]
 
  #Check each cell in index order
  done=True
  for i in range(n):
    u=t[i]
    if a[u]==15:
      done=False
    else:
      move(u)

#Done if all cells connected
  if done:
    break

#Done
a[n-1]&=13
disp()


Voici mon erreur :
Code: Select all
File "<stdin>", line 2, in <module>
  File "C:\Users\arihe\AppData\Roaming\Texas I
nstruments\TI-Nspire CX CAS Student Softwar
e\python\doc2\maze.py", line 103, in <module>
  File "C:\Users\arihe\AppData\Roaming\Texas I
nstruments\TI-Nspire CX CAS Student Softwar
e\python\doc2\maze.py", line 71, in move
  File "C:\Users\arihe\AppData\Roaming\Texas I
nstruments\TI-Nspire CX CAS Student Softwar
e\python\doc2\maze.py", line 39, in eas
TypeError: unsupported types for __add__: 'Non
eType', 'int'

Re: Petit erreur pour génération de labyrinthe en python

Unread postPosted: 22 Apr 2022, 20:18
by critor
Bonjour.

De ce que je constate, tes fonctions opératoires (nor, eas, sou, wes) ont toutes leurs instructions return dans des blocs conditionnels, et ce sans aucune alternative (else).
Peut-être que le cas ne serait pas censé se produire, mais a priori je ne suis absolument pas surpris qu'elles puissent renvoyer None, ce qui fait alors échouer l'opération suivante comme indiqué par tes messages d'erreurs.

Re: Petit erreur pour génération de labyrinthe en python

Unread postPosted: 22 Apr 2022, 20:34
by critor
J'ai retrouvé le script original :
https://fr1lib.org/book/17194983/a0aa7c

Et je te confirme bien dans ta transcription une erreur d'indentation des return dans les fonctions opératoires.

Cela devrait être ça :
Code: Select all
def nor(u):
  v=u-w
  if v>0:
    if a[v]==15:
      a[u]&=14
      a[v]&=11
      return v
  return u

def eas(u):
  v=u+1
  if v%w:
    if a[v]==15:
      a[u]&=13
      a[v]&=7
      return v
  return u

def sou(u):
  v=u+w
  if v<n:
    if a[v]==15:
      a[u]&=11
      a[v]&=14
      return v
  return u

def wes(u):
  v=u-1
  if u%w:
    if a[v]==15:
      a[u]&=7
      a[v]&=13
      return v
  return u

Re: Petit erreur pour génération de labyrinthe en python

Unread postPosted: 22 Apr 2022, 20:43
by Altaear
En effet je n'ai plus d'erreur :)
Mais malheureusement la génération du labyrinthe bug un peu :/
Ca peut paraitre bête mais j'ai recopié ce code d'un livre pour essayer de le comprendre mais je ne connais pas les &= en python à quoi servent-ils ?

Re: Petit erreur pour génération de labyrinthe en python

Unread postPosted: 22 Apr 2022, 20:57
by critor
Je confirme que c'est long, mais que ça finit bien par marcher :
Image

&= est un raccourci pour réaliser un "et bit à bit" sur la variable concernée.
Par exemple, les 2 lignes suivantes sont équivalentes :
Code: Select all
a[u] &= 14
a[u] = a[u] & 14

Re: Petit erreur pour génération de labyrinthe en python

Unread postPosted: 22 Apr 2022, 21:28
by Altaear
D'accord je comprend à peu près,
Mais malheureusement moi mon programme à décider que ça ne marcherait pas :sob:

Re: Petit erreur pour génération de labyrinthe en python

Unread postPosted: 22 Apr 2022, 21:37
by critor
Ouille...

Je te colle exactement ce que j'ai fait tourner ici :
Code: Select all
from ti_draw import *
from random import *

def vline(x,y,w):
  if w:
    x1=x*wl+9
    y1=y*wl+3
    draw_line(x1,y1,x1,y1+wl)

def hline(x,y,w):
  if w:
    x1=x*wl+9
    y1=y*wl+3
    draw_line(x1,y1,x1+wl,y1)

def cell(x,y):
  z=a[x+y*w]
  hline(x,y,z&1)
  vline(x+1,y,z&2 )
  hline(x,y+1,z&4)
  vline(x,y,z&8)

def disp():
  clear
  for x in range(w):
    for y in range(h):
      cell(x,y)

def nor(u):
  v=u-w
  if v>0:
    if a[v]==15:
      a[u]&=14
      a[v]&=11
      return v
  return u

def eas(u):
  v=u+1
  if v%w:
    if a[v]==15:
      a[u]&=13
      a[v]&=7
      return v
  return u

def sou(u):
  v=u+w
  if v<n:
    if a[v]==15:
      a[u]&=11
      a[v]&=14
      return v
  return u

def wes(u):
  v=u-1
  if u%w:
    if a[v]==15:
      a[u]&=7
      a[v]&=13
      return v
  return u

def move(u):
  for i in range(c):
    d=randrange(4)
    if d==0:
      u=nor(u)
    if d==1:
      u=eas(u)
    if d==2:
      u=sou(u)
    if d==3:
      u=wes(u)

# Initialize
w=25
h=17
n=w*h
c=n/4
wl=12
a=[15 for x in range(n)]
t=[x for x in range(n)]
a[0]=7
clear()
draw_text(125,110,"-thinking-")

while 1:

  # Shuffle the index
  for i in range(n):
    j=randrange(n)
    t[i],t[j]=t[j],t[i]

  #Check each cell in index order
  done=True
  for i in range(n):
    u=t[i]
    if a[u]==15:
      done=False
    else:
      move(u)

#Done if all cells connected
  if done:
    break

#Done
a[n-1]&=13
disp()

Re: Petit erreur pour génération de labyrinthe en python

Unread postPosted: 22 Apr 2022, 21:50
by Altaear
en effet il manquait un = après le & dans ma méthode eas merci pour la correction :)
Une dernière chose le thinking reste en plein milieu de l'écran comment puis-je le supprimer ?

Re: Petit erreur pour génération de labyrinthe en python

Unread postPosted: 22 Apr 2022, 22:05
by critor
J'ai pareil ici.

La fonction disp() est pourtant prévue pour commencer par tout effacer avec un appel clear().

Mais j'ai l'impression qu'il y a eu une autre erreur de copie dans ton script plus haut (ou dans la source que tu as utilisée), cet appel est mal écrit : clear au lieu de clear().

Voici le code corrigé de nouveau :
Code: Select all
from ti_draw import *
from random import *

def vline(x,y,w):
  if w:
    x1=x*wl+9
    y1=y*wl+3
    draw_line(x1,y1,x1,y1+wl)

def hline(x,y,w):
  if w:
    x1=x*wl+9
    y1=y*wl+3
    draw_line(x1,y1,x1+wl,y1)

def cell(x,y):
  z=a[x+y*w]
  hline(x,y,z&1)
  vline(x+1,y,z&2 )
  hline(x,y+1,z&4)
  vline(x,y,z&8)

def disp():
  clear
  for x in range(w):
    for y in range(h):
      cell(x,y)

def nor(u):
  v=u-w
  if v>0:
    if a[v]==15:
      a[u]&=14
      a[v]&=11
      return v
  return u

def eas(u):
  v=u+1
  if v%w:
    if a[v]==15:
      a[u]&=13
      a[v]&=7
      return v
  return u

def sou(u):
  v=u+w
  if v<n:
    if a[v]==15:
      a[u]&=11
      a[v]&=14
      return v
  return u

def wes(u):
  v=u-1
  if u%w:
    if a[v]==15:
      a[u]&=7
      a[v]&=13
      return v
  return u

def move(u):
  for i in range(c):
    d=randrange(4)
    if d==0:
      u=nor(u)
    if d==1:
      u=eas(u)
    if d==2:
      u=sou(u)
    if d==3:
      u=wes(u)

# Initialize
w=25
h=17
n=w*h
c=n/4
wl=12
a=[15 for x in range(n)]
t=[x for x in range(n)]
a[0]=7
clear()
draw_text(125,110,"-thinking-")

while 1:

  # Shuffle the index
  for i in range(n):
    j=randrange(n)
    t[i],t[j]=t[j],t[i]

  #Check each cell in index order
  done=True
  for i in range(n):
    u=t[i]
    if a[u]==15:
      done=False
    else:
      move(u)

#Done if all cells connected
  if done:
    break

#Done
a[n-1]&=13
disp()


Et ça marche : :)
Image

Re: Petit erreur pour génération de labyrinthe en python

Unread postPosted: 22 Apr 2022, 22:08
by Altaear
Merci beaucoup pour le temps pris :D