hanoi.py

Created by elnix91

Created on April 18, 2025

3.67 KB

Towers of Hanoi


from ion import keydown as key
from kandinsky import fill_rect as rect ,draw_string as txt
from time import sleep


# Main variables
WOOD = (150,75,0)
RINGS = [0,0,0]
SELECT = (255,0,0)
SKY = (0,0,0)
left_key = 0
right_key = 3
selection_key = 52


# Drawing ring
def draw_ring(pile_draw,pile_acted,id,fly,c):
    if c == 0:
        c = SKY
    else:
        c=(0,0,255-PILES[pile_acted][id]*20)

    long=PILES[pile_acted][id]*5
    y = 193
    if fly:y = 30

    rect(75+80*pile_draw,y-15*len(PILES[pile_acted][:id]),-long,14,c)
    rect(85+80*pile_draw,y-15*len(PILES[pile_acted][:id]),long,14,c)


# Drawing arrow of selected pile
def draw_select(pile,c):
    if c == 1:
        c = SELECT
        c1 = 1
    else:
        c = SKY
        c1 = 0
    rect(75+80*pile,15,10,10,c)
    if len(PILES[3]) == 1: 
        draw_ring(pile,3,0,1,c1)


# Menu
def menu():
    nb_rings = input("Rings number: ")
    return int(nb_rings)



# Main loop
while 1:

    # Game variables
    PILES = [[],[],[],[]]
    pile_selected = 0
    old_pile_selected = 1
    selector_moved = True
    allow_change_pile_selected = True
    allow_select_ring = True

    

    # Ask number of rings to the player
    nb_rings = menu()
    for i in range(nb_rings,0,-1):
        PILES[0].append(i)


    # Draw pillars and ground
    rect(0,0,320,222,SKY)
    rect(0,208,320,15,WOOD)
    for i in range(3):
        rect(75+80*i,208,10,-nb_rings*15-10,WOOD)


    # Draw rings
    for i in range(nb_rings):
        draw_ring(0,0,i,0,1)

    while key(selection_key) == 1:1

    # Game loop
    while 1:

        # Pile selection system
        if key(right_key) + key(left_key) == 0:
            allow_change_pile_selected = True
            selector_moved = False
        if (allow_change_pile_selected == True) and (key(right_key) + key(left_key) > 0):
            pile_selected = (pile_selected + key(right_key)-key(left_key))%3
            allow_change_pile_selected = False
            selector_moved = True



        # Pile moving system
        if key(selection_key) == 0:
            allow_select_ring = True
        

        if (key(selection_key) == True) and (allow_select_ring == True):
            allow_select_ring = False

            # If selection is empty
            if (len(PILES[3]) == 0) and (len(PILES[pile_selected]) > 0):

                PILES[3].append(PILES[pile_selected][-1])
                draw_ring(pile_selected,pile_selected,-1,0,0)
                draw_ring(pile_selected,3,0,1,1)
                PILES[pile_selected].pop()

            # If selection have 1 item
            elif (len(PILES[3]) == 1):

                allow_put_ring_in_pile = False

                if len(PILES[pile_selected])>0:
                    if PILES[pile_selected][-1]>PILES[3][0]:
                        allow_put_ring_in_pile = True
                else:
                    allow_put_ring_in_pile = True
                
                if allow_put_ring_in_pile == True:

                    PILES[pile_selected].append(PILES[3][0])
                    draw_ring(pile_selected,pile_selected,-1,0,1)
                    draw_ring(pile_selected,3,0,1,0)
                    PILES[3].pop()


        # Draw things
        if selector_moved:
            draw_select(old_pile_selected,0)
            old_pile_selected = pile_selected
            draw_select(pile_selected,1)


        # Test if victory
        print(nb_rings)
        print(PILES)
        if (len(PILES[1])==nb_rings) or (len(PILES[2])==nb_rings):
            rect(142,99,36,24,(0,0,255))
            txt("GG!",145,102,(0,0,255),SKY)
            sleep(1)
            while key(selection_key) == 1:1
            while not key(selection_key) == 1:1
            break

        sleep(0.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.