hanoi.py

Created by pikube

Created on November 14, 2019

1.81 KB

ATTENTION: CE PROGRAMME EXIGE TOUTE LA MEMOIRE DE LA CALCULATRICE (donc supprimer tous les autres programmes python présents avant de l’injecter).

Jeu de hanoi: empiler tous les anneaux (du plus petit au plus grand) sur le pylône à droite. Lancez le jeu avec play(). Pour sélectionner un anneau, tapez 4 (pylône gauche), 5 (pylône milieu) ou 6 (pylône droite) puis EXE. Pour choisir la destination de l’anneau sélectioné, tapez à nouveau 4, 5 ou 6 et EXE. Si vous souhaitez ajouter un anneau, ajouter une valeur à pilar1 (cette liste contient les anneaux et leur taille, croissante)

Pour une résolution automatique, tapez hanoi(). Merçi à Smoofy pour cette partie du script.


from math import *
import kandinsky as k
import time

pilar1=[1,2,3,4,5]
pilar2=[]
pilar3=[]
pilars=[pilar1,pilar2,pilar3]

def fill(a,b,c,d,e):
  k.fill_rect(a,b,c,d,e)

def draw_pilar(x,y):
  fill(x+40,y,20,20*5+20,(200,200,200))
  fill(x,y+20*5+20,100,20,(200,200,200))

def draw_ring(x,y,size):
  if size>0:
    fill(x-10*size,y,20*size+10,5,(size*50,size*88,size*64))  

def draw_scene():
  fill(0,0,320,222,(255,255,255))
  for cx in range(3):
    draw_pilar(cx*100+10,90)
    k.draw_string("%s"%cx,cx*100+55,207,(0,0,0),(200,200,200))
    for cy in range(len(pilars[cx])):
      draw_ring(cx*100+55,200-10*cy,pilars[cx][len(pilars[cx])-cy-1])

def arrow(x):
  x+=40
  fill(x,40,15,20,(255,0,0))
  for loop in range(10):
    fill(x-3+loop,60+loop,21-2*loop,1,(255,0,0))

l=len(pilar1)

def move(ic,oc):
  #oc et ic des int
  if type(oc)!=str and type(ic)!=str and len(pilars[ic])!=0:
      #aucun anneau dans la column output
      if pilars[oc]==[]:
        pilars[oc].insert(0,pilars[ic].pop(0))
      else:
        #anneau dans la column output
        if (pilars[ic][0])<=(pilars[oc][0]):
          pilars[oc].insert(0,pilars[ic].pop(0))

def play():
  draw_scene()
  while len(pilars[2])<l:
    i1=input()
    oc,ic="50","50"
    if i1=="4":
      ic=0
      arrow(12)
    if i1=="5":
      ic=1
      arrow(112)
    if i1=="6":
      arrow(212)
      ic=2
    i2=input()
    if i2=="4":
      oc=0
    if i2=="5":
      oc=1
    if i2=="6":
      oc=2
    move(ic,oc)
    draw_scene()
  k.draw_string("Bravo!",125,50)
  
def solve(n=len(pilar1),d=0,i=1,a=2):
  draw_scene() 
  if n==1:
    move(d,a)
    time.sleep(0.5)
    draw_scene()
  else:
    solve(n-1,d,a,i)
    move(d,a)
    time.sleep(0.5)
    draw_scene()
    solve(n-1,i,d,a)
def hanoi():
  solve()
  k.draw_string("mouvements:%s"%(2**(len(pilar3))-1),160-60,50)

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.