import pygame, sys
from pygame.locals import *

pygame.init()
DISPLAYSURF = pygame.display.set_mode((1000, 600))
pygame.display.set_caption("Towers of Hanoi!")

black = pygame.Color(0,0,0)
white = pygame.Color(255,255,255)
red = pygame.Color(255,0,0)
purple = pygame.Color(255,0,255)
clock = pygame.time.Clock()

def main():
    
    # Get the specific input case for towers.
    #n = int(input("How many disks?\n"))
    #start = int(input("What is the start tower, 1, 2 or 3?\n"))
    #end = int(input("What is the end tower, 1, 2 or 3?\n"))
    n = 5
    start = 3
    end = 1
    #pygame.time.wait(2000)

    # Store three stacks.
    stacks = []
    for i in range(3):
        stacks.append([])

    # Put all items on the starting stack.
    for i in range(n,0,-1):
        stacks[start-1].append(i)

    # Do it.
    draw(n,stacks)
    towers(n, start, end, stacks)
    
    # Just to end the game.
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
                
# Runs a visual towers of hanoi with n disks from start to end, with the
# current contents (globally) stored in stacks.
def towers(n,start,end,stacks):

    # We have work to do.
    if n > 0:

        # This is handy...
        mid = 6 - start - end

        # Recurse with the top n-1 disks.
        towers(n-1,start,mid,stacks)

        # Pop off the top disk from stack start.
        disk = stacks[start-1].pop()

        # Put it on stack end.
        stacks[end-1].append(disk)

        #print("next move",n,start,end)
        #print(stacks[0])
        #print(stacks[1])
        #print(stacks[2])

        # Draw it.
        draw(n,stacks)

        # Move those n-1 disks to their final destination.
        towers(n-1,mid,end,stacks)

def draw(n,stacks):
      
    DISPLAYSURF.fill(red)

    # Three towers.
    pygame.draw.rect(DISPLAYSURF, purple, (295, 50, 10, 450), 0)
    pygame.draw.rect(DISPLAYSURF, purple, (495, 50, 10, 450), 0)
    pygame.draw.rect(DISPLAYSURF, purple, (695, 50, 10, 450), 0)

    # Draw each stack.
    for i in range(len(stacks)):

        # Go through each item (bottom to top) in stack i.
        for j in range(len(stacks[i])):

            # Get where I am drawing this disk.
            y = convertToY(n,j)
            x = convertToX(n,stacks[i][j],i)

            # Draw it.
            mycolor = white
            if stacks[i][j]%2 == 1:
                mycolor = black
            pygame.draw.ellipse(DISPLAYSURF, mycolor, (x, y, 40+20*stacks[i][j], 50), 0)

    pygame.display.update()
    pygame.time.wait(500)

def convertToY(n,index):
    return 450 - 50*index

def convertToX(n,diskno,col):
    mid = 300 + col*200
    return mid - 10*(diskno+2)

# Get it started.
main()
