POV-Ray : Newsgroups : povray.animations : towers of hanoi : Re: towers of hanoi Server Time
9 Oct 2026 07:37:50 EDT (-0400)
  Re: towers of hanoi  
From: kurtzlepirate
Date: 17 Aug 2026 10:39:34
Message: <6a831d26@news.povray.org>
On 16/08/2026 13:48, jr wrote:
> hi,
> 
> koppi <jak### [at] gmailcom> wrote:
>> I solved the "towers of hanoi" puzzle using an iterative solver with an
>> explicit stack inside the bullet physics playground.
>> ... hanoi.lua
> 
> the POV-Ray parser can do recursion, any chance of your trying to make a SDL
> implementation ?
> 
> 
> regards, jr.
> 

yes of course.

writing the macro is really very simple. my old project about tower of 
hanoi:
// -----------------------------------------
// num	  : number of disc
// source : string
// dest	  : string
// helper : string
// -----------------------------------------
#macro tower_of_hanoi(num, source, dest, helper)
  #if ( num = 1 )
   Display(1,source,dest)
  #else	
   tower_of_hanoi(num - 1, source, helper, dest)
   Display(num,source,dest)
   tower_of_hanoi(num - 1, helper, dest, source)
  #end
#end


invoked in this way:
tower_of_hanoi(NumDisk, "1", "3", "2")


for NumDisk = 5 for exemple, this macro give:
(roman numerals refer to the discs, arabic numerals towers)

For 5 pieces, 31 moves are required.
Sequence :
001 : Move disk I from 1 to 3
002 : Move disk II from 1 to 2
003 : Move disk I from 3 to 2
004 : Move disk III from 1 to 3
005 : Move disk I from 2 to 1
006 : Move disk II from 2 to 3
007 : Move disk I from 1 to 3
008 : Move disk IV from 1 to 2
009 : Move disk I from 3 to 2
010 : Move disk II from 3 to 1
011 : Move disk I from 2 to 1
012 : Move disk III from 3 to 2
013 : Move disk I from 1 to 3
014 : Move disk II from 1 to 2
015 : Move disk I from 3 to 2
016 : Move disk V from 1 to 3
017 : Move disk I from 2 to 1
018 : Move disk II from 2 to 3
019 : Move disk I from 1 to 3
020 : Move disk III from 2 to 1
021 : Move disk I from 3 to 2
022 : Move disk II from 3 to 1
023 : Move disk I from 2 to 1
024 : Move disk IV from 2 to 3
025 : Move disk I from 1 to 3
026 : Move disk II from 1 to 2
027 : Move disk I from 3 to 2
028 : Move disk III from 1 to 3
029 : Move disk I from 2 to 1
030 : Move disk II from 2 to 3
031 : Move disk I from 1 to 3


... but from there on, it's difficult to implement the movement of the 
discs to turn it into an animation. I never really found a solution. The 
project fell through :(


If anyone has the heart...



-- 
klp
compagnie de la banquise


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.