require("draw")
fullscreen(1)
draw.onbuffer()
--wait for exe
exe=false
--draw tower
drawtower=true
b={{0},{0},{0}}
z={0,0,0}
function drawline(c,r,l)
y = 80*c-40
x = 80-1.5*n+3*r
if l>0 then
draw.line(x,y-2*l,x,y+2*l)
draw.line(x-1,y-2*l,x-1,y+2*l)
else
draw.line(x,y-14,x,y+14)
draw.line(x-1,y+14,x-1,y-14)
draw.line(x,180,x,50)
end
end
function board()
draw.clear()
draw.line(80+1.5*n,40,80-1.5*n,40) draw.line(80+1.5*n,120,80-1.5*n,120) draw.line(80+1.5*n,200,80-1.5*n,200)
draw.line(78-1.5*n,0,78-1.5*n,260,1,3)
for i=1,3 do for j=1,z[i] do drawline(i,j,b[i][j]) end end
draw.update()
end
function cw(p) return p%3+1 end
function nm(p) if p==1 then return("left ") else if p==2 then return("middle") end end return("right ") end
function coed(n) n=n/2 local c=0 while n>0 do if n%2==1 then c=c+1 end n=n/4 end return(c) end
function main()
clear()
pos=1 n=tonumber(input("How many disks?")) z[1]=n for i=1,n do b[1][i],b[2][i],b[3][i]=n-i+1,0,0 end lastMove=2^n-1
clear()
f,t=1,1
showgraph()
for m=1,lastMove do
printf(m..":")
if m%2==1 then
f,t=pos,cw(pos)
print(nm(f).." to "..nm(t)..".")
pos=t
else
if coed(m)%2==1 then
f,t=cw(cw(pos)),cw(pos)
print(nm(f).." to "..nm(t)..".")
else
t,f=cw(cw(pos)),cw(pos)
print(nm(f).." to "..nm(t)..".") end end
if drawtower then
b[t][z[t]+1]=b[f][z[f]]
b[f][z[f]]=0 z[f]=z[f]-1 z[t]=z[t]+1
board()
if exe then waitkey(K_EXE) else for i=1,10000 do end end
end
end
end
main()
Tower Of Hanoi Solution
Started by
vanhoa
, Nov 06 2006 01:39 AM
3 replies to this topic
#1
Posted 06 November 2006 - 01:39 AM
#2
Posted 10 November 2006 - 01:31 PM
if you wanna wait for exe you just have to type
I wanted to see the code but it's too hard to read :s could you fix that ?
waitkey(K_EXE)
I wanted to see the code but it's too hard to read :s could you fix that ?
#3
Posted 10 November 2006 - 02:49 PM
It is more easy to you to change it in the top of the file, only change exe=1 or exe=false (or nil).
#4
Posted 13 November 2006 - 12:38 PM
k I didn't noticed
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users



