Advertisement
Blairjay

Hanoi Script Mudlet

Jun 7th, 2022
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1.  
  2. Alias: ^hanoi (\d+) (\w+) (\w+)$
  3. hanoi(tonumber(matches[2]),matches[3],matches[4])
  4.  
  5.  
  6. Script:
  7. function hanoi(discNum, startPeg, endPeg)
  8. pegs = {["left"] = 1,["middle"] = 2,["right"] = 3}
  9. discs = {[1] = "ten",[2] = "twenty",[3] = "thirty",[4] = "forty",[5] = "fifty",[6] = "sixty"}
  10. currDisc = 1
  11. send(hanoiRec(discNum, startPeg, endPeg))
  12.  
  13. end
  14. function hanoiRec(discNum, startPeg, endPeg)
  15. if (discNum == 1) then
  16. return "get " .. discs[1] .." from " .. startPeg .. ";;put " .. discs[1] .. " on " .. endPeg .. ";;"
  17. else
  18. local helpPeg = 6 - pegs[startPeg] - pegs[endPeg]
  19. if helpPeg == 1 then helpPeg = "left" end
  20. if helpPeg == 2 then helpPeg = "middle" end
  21. if helpPeg == 3 then helpPeg = "right" end
  22. local solution1 = hanoiRec(discNum-1,startPeg,helpPeg)
  23. local innerStep = "get " .. discs[discNum] .." from " .. startPeg .. ";;put " .. discs[discNum] .. " on " .. endPeg .. ";;"
  24. local solution2 = hanoiRec(discNum-1,helpPeg,endPeg)
  25. final = solution1 .. innerStep .. solution2
  26. return(final)
  27. end
  28. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement