Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.66 KB | None | 0 0
  1. import random
  2.  
  3. width = 10
  4. height = 10
  5.  
  6. start_pos = (0,0)
  7. liste_visites = [start_pos]
  8. liste_historique = [start_pos]
  9.  
  10. def next_step():
  11.     global liste_visites
  12.     global liste_historique
  13.     global actual_pos
  14.  
  15.     direction = [1,1,1,1] #liste de boléen pour [up,right,down,left]
  16.  
  17.     for i in liste_visites: #
  18.         if i[0] == actual_pos[0] and i[1] == actual_pos[1]-1:
  19.             direction[0] = 0
  20.  
  21.         if i[0] == actual_pos[0]+1 and i[1] == actual_pos[1]:
  22.             direction[1] = 0
  23.  
  24.         if i[0] == actual_pos[0] and i[1] == actual_pos[1]+1:
  25.             direction[2] = 0
  26.  
  27.         if i[0] == actual_pos[0]-1 and i[1] == actual_pos[1]-1:
  28.             direction[3] = 0
  29.  
  30.         if actual_pos[1] <= 0 :
  31.             direction[0] = 0
  32.  
  33.         if actual_pos[0] >= width:
  34.             direction[1] = 0
  35.  
  36.         if actual_pos[1] >= height:
  37.             direction[2] = 0
  38.  
  39.         if actual_pos[0] <= 0:
  40.             direction[3] = 0
  41.  
  42.  
  43.     liste_dir_possibles = []
  44.     for i in range(0,4):
  45.         if direction[i]==1:
  46.             liste_dir_possibles.append(i)
  47.    
  48.     a = 0
  49.     for i in direction:
  50.         a += i
  51.  
  52.     if a>0:
  53.         if a>1:
  54.             rand = random.randomint(0,a-1)
  55.         else:
  56.             rand = 0
  57.  
  58.         liste_historique.append(actual_pos)
  59.         liste_visites.append(actual_pos)
  60.  
  61.         if list_dir_possibles[rand]==0:
  62.             actual_pos = (actual_pos[0],actual_pos[1]-1)
  63.  
  64.         if list_dir_possibles[rand]==1:
  65.             actual_pos = (actual_pos[0]+1,actual_pos[1])
  66.  
  67.         if list_dir_possibles[rand]==2:
  68.             actual_pos = (actual_pos[0],actual_pos[1]+1)
  69.  
  70.         if list_dir_possibles[rand]==3:
  71.             actual_pos = (actual_pos[0]-1,actual_pos[1])
  72.  
  73.     else:
  74.         liste_historique.pop(len(liste_historique)-1) #supprime le dernier element de la liste
  75.         actual_pos = liste_historique[len(liste_historique)-1] #actual_pos prend la derniere valeur de l'historique
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement