1 --[[ Towers of hanoi - Lua 5.1 ]]--
3 local leftStack, centerStack, rightStack = {name = 'Left'}, {name = 'Center'}, {name = 'Right'}
5 local visualize = function(...)
9 print(string.format("%s:\t%s", z[i].name, table.concat(z[i], ' ')))
14 local move = function(a, b)
15 print( a.name, '->', b.name )
16 table.insert( b, table.remove(a) )
22 hanoi = function(n, orig, pivot, dest)
39 hanoi(n-2, orig, pivot, dest)
41 hanoi(n-2, dest, orig, pivot)
43 hanoi(n-1, pivot, orig, dest)
46 io.write "Value of N: "
47 local n = tonumber( io.read "*l", 10 )
49 if type(n) == 'number' then
52 io.stderr "Value given is not a number.\n"
57 leftStack[ #leftStack+1 ] = i
60 local hanoiInteractive = coroutine.wrap(hanoi)
64 print('#'..counter..': ')
66 visualize( leftStack, centerStack, rightStack )
68 until hanoiInteractive(n, leftStack, centerStack, rightStack) ~= true