Advertisement
AdditionalPylons

hanoi.lua

May 25th, 2024
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.21 KB | None | 0 0
  1. print("Height: ")
  2. local w,h = term.getSize()
  3. local height = read()
  4. local blockh = h/height
  5. local scale = w/height/3
  6. local timescale = 0.5/(height/5+1)
  7. local blockcols = {colors.red,colors.orange,colors.yellow,colors.green,colors.cyan,colors.blue,colors.purple}
  8.  
  9. local blocks = {{},{},{}}
  10. for i=1,height do blocks[1][i] = height-i+1 end
  11.  
  12. local function draw_block(blockw,blocky,blockx)
  13.     local drawx = w/3*(blockx-0.5)
  14.     paintutils.drawBox(drawx-blockw/2*scale,
  15.                        h-(blockh*(blocky-1)),
  16.                        drawx+blockw/2*scale,
  17.                        h-blockh*blocky+1,
  18.                        blockcols[(blockw-1)%#blockcols+1])
  19. end
  20. local function move(a,b)
  21.     print(a,b)
  22.     table.insert(blocks[b],table.remove(blocks[a]))
  23.     term.setBackgroundColor(colors.black)
  24.     term.clear()
  25.     for i=1,3 do
  26.         for j=1,#blocks[i] do
  27.             draw_block(blocks[i][j],j,i)
  28.         end
  29.     end
  30.     sleep(timescale)
  31. end
  32. local function hanoi(h,a,b)
  33.     a = a or 1
  34.     b = b or 3
  35.     if h==1 then
  36.         move(a,b)
  37.         return
  38.     end
  39.     local c=6-a-b
  40.     hanoi(h-1,a,c)
  41.     move(a,b)
  42.     hanoi(h-1,c,b)
  43. end
  44.  
  45. term.setBackgroundColor(colors.black)
  46. hanoi(height)
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement