LOGO
The Towers of Hanoi as a LOGO program.
;
; The Towers Of Hanoi
; LOGO
; Copyright (C) 1998 Amit Singh. All Rights Reserved.
; http://hanoi.kernelthread.com
;
; Tested under ucblogo 4.2
; Implementing a stack
make "STACK []
make "popped 0
make "esp 0
.macro while :while.cond :while.instr
if not run :while.cond [op []]
op se :while.instr (list "while :while.cond :while.instr)
end
bury "while
to _push :the.stack.name :the.item.value
make :the.stack.name fput :the.item.value thing :the.stack.name
make "esp :esp + 1
end
bury "_push
to _pop :the.stack.name
local "result
make "result first thing :the.stack.name
make :the.stack.name butfirst thing :the.stack.name
make "esp :esp - 1
make "popped :result
end
bury "_pop
to push :the.item.value
_push "STACK :the.item.value
end
bury "push
to pop
_pop "STACK
end
bury "pop
make "_sfTO 2
make "_sfFR 0
make "_sfUS 1
make "_sfNU 3
make "_sfEN 0
make "remain 0
to init
make "STACK []
make "popped 0
make "esp 0
make "_sfTO 2
make "_sfFR 0
make "_sfUS 1
make "_sfNU 3
make "_sfEN 0
make "remain 0
end
bury "init
TO moveit :from :to
(print "move :from "--> :to)
end
bury "moveit
TO hanoi :n
init
push :n
push 1
push 3
push 0
while [:esp > 0] [
pop
make "_sfEN :popped
pop
make "_sfTO :popped
pop
make "_sfFR :popped
pop
make "_sfNU :popped
make "remain 6 - :_sfFR - :_sfTO
ifelse not :_sfEN = 0 [
moveit :_sfFR :_sfTO
make "n_1 :_sfNU - 1
push :n_1
push :remain
push :_sfTO
push 0
] [
ifelse not :_sfNU = 1 [
push :_sfNU
push :_sfFR
push :_sfTO
push 1
make "n_1 :_sfNU - 1
push :n_1
push :_sfFR
push :remain
push 0
] [
moveit :_sfFR :_sfTO
]
]
]
end
bury "hanoi