[REBOL] =?iso-8859-1?Q?Towers_of_Hano=EF_-_just_for_fun?=
From: greggirwin::starband::net at: 18-Sep-2001 2:18
REBOL [
File: %hanoi.r
Author: "Gregg Irwin"
Date: 18-Sep-2001
Comments: {
This is modeled on an example in an old LISP book I have
(Understanding LISP) by Paul Gloess.
Call start routine to make it go (e.g. hanoi/start 3).
}
TBD: {
* Add visualization
}
]
hanoi: context [
towers: num-moves: none
start: func [
num-disks[integer!] "Number of disks to play with"
][
num-moves: 0
towers: reduce [_make-disks num-disks copy [] copy []]
print mold towers
_do-towers num-disks towers/1 towers/2 towers/3
print ["Number of moves required: " num-moves]
]
_do-towers: func [
num-disks[integer!] "Number of disks to move"
source[any-block!] "Source tower"
temp[any-block!] "Temporary holding tower"
dest[any-block!] "Destination tower"
][
if num-disks > 0 [
_do-towers (num-disks - 1) source dest temp
_move-disk source dest
_do-towers (num-disks - 1) temp source dest
]
]
_move-disk: func [
source[any-block!] "Source tower"
dest[any-block!] "Destination tower"
][
num-moves: add num-moves 1
append dest last source
clear back tail source
print mold towers
]
_make-disks: func [
num-disks [integer!] "Number of disks to put in a block"
/local i result
][
result: make block! num-disks
for i num-disks 1 -1 [
append result i
]
return result
]
]