[REBOL] "Towers of Hanoi" : Comments please
From: rgombert::essentiel::net at: 18-Feb-2001 21:05
This is one of my first scripts with REBOL. As i'm not sure of the "better"
way to achieve things, i would be happy to receive your comments about this
script, and how to code it better, or closer to the REBOL coding 'spirit'.
Title: "Hanoi game (learnig Rebol with...)"
Author: "Renaud GOMBERT"
The "Towers of Hanoi" Game.
Discs of different sizes stays on 3 sticks/stacks. Move one disc at a time
from one stack
to another. A move is possible only if the destination stick is empty or
its top disc is greater than the
the one you are moving. The goal is to move the initial stack from the
first stick to an other.
init: func [
"Initialisation of the hanoi block"
n [integer!] "Number of disc to use"
count: make integer! 0;
tmp: make block! 
hanoi: make block! 
for i 1 n 1 [tmp: append tmp i]
hanoi: reduce [tmp make block! 0 make block! 0]
first2: func [
"Return first item, otherwise 99999"
if (length? blk) = 0 [return 99999]
return first blk
move: func [
"Move one disc from one stick to another - does not care of game's rules"
/local temp tmp
temp: make block! []
tmp: first pick hanoi from
for i 1 3 1 [
poke temp i head either i = from [remove hanoi/:i] [either i = to [insert
head hanoi/:i tmp][hanoi/:i]]
count: count + 1
hanoi: copy temp
print [count ": move from" from "to" to " : " mold hanoi]
play: func [
"Play nb disc from one place to an other"
from [integer!] "Place to start from"
to [integer!] "Place to go to"
nb [integer!] "How many disc to move"
/local from_state to_state tmp n
tmp: pick [0 0 3 2 1] (from + to)
either ((length? pick hanoi from) > 0) [
either nb = 1 [
from_state: pick hanoi from
to_state: pick hanoi to
either (first2 from_state) < (first2 to_state)
move from to
move to from
play from to 2
play from tmp (nb - 1)
play from to 1
play tmp to (nb - 1)
print "Can't do that: nothing to move"
; init a game with 4 discs
;play the game
play 1 3 4