[COLOR=black]I did an ActionScript 3 version of my old algorithm made in c language to solve the “Tower of Hanoi” puzzle.[/COLOR]
[COLOR=black]The problem is that I knew how to show the intermediary steps in c language but I don’t know how to do that in ActionScript 3.[/COLOR]
[COLOR=black]The below .as file code uses just three disks of successively bigger diameters ( and different colors ) created as MovieClips in Flash which must move from the first peg to the third one, through appropriate moves, in such way that they end up piled up in the same increasing order as they were in the first peg.[/COLOR]
[COLOR=black]The way the code is written just shows the beginning disks position and the end result but not the intermediary steps.[/COLOR]
[COLOR=black]I want this code being capable to show the successive disks positions on the pegs in each step of the process, each time I click the mouse.[/COLOR]
[COLOR=black]How to do that ?[/COLOR]
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.text.TextField;
import flash.utils.*;
public class TheTowerOfHanoi extends MovieClip
{
private var pegs_num:uint = 3;
private var disks_num:uint = 3;
private var PegNum:uint;
private var PegDiskPos = new Array();
private var PegDiskContent = new Array();
private var disk:Array = new Array();
public function TheTowerOfHanoi()
{
for ( var p = 0; p<pegs_num; p++)
{
PegDiskPos[p] = new Array( disks_num);
PegDiskContent[p] = new Array( disks_num);
// Set the Content of all disk positions and the coordinates of the positions
for( var d = 0; d<disks_num; d++)
{
//Adds array of the position x and y coordinates
PegDiskPos[p][d] = new Array(150+ p*120, 200+d*20);
if( p == 0 )
{
PegDiskContent[p][d] = "" + d;
//~~~~~~~~~
}
else
PegDiskContent[p][d] = null;
//PegDiskContent[p][d] = "";
}
}
disk[0] = new DiskA();
disk[1] = new DiskB();
disk[2] = new DiskC();
//Put disks in the initial position
disk[0].x = PegDiskPos[0][0][0]; disk[0].y = PegDiskPos[0][0][1];
disk[1].x = PegDiskPos[0][0][0]; disk[1].y = PegDiskPos[0][1][1];
disk[2].x = PegDiskPos[0][0][0]; disk[2].y = PegDiskPos[0][2][1];
addChild( disk[0]);
addChild( disk[1]);
addChild( disk[2]);
btn.addEventListener(MouseEvent.CLICK, ShowNextStep);
}
public function ShowNextStep(event:MouseEvent):void
{
RearrangeDisksInThePegs( disks_num, 0,1,2)
}
public function RearrangeDisksInThePegs( LastProcessedDisk:int, originPeg:uint, dormentPeg:uint, destinationPeg:uint)
{
if( LastProcessedDisk == 1 )
{
MoveDisk( originPeg, destinationPeg);
return;
}
else
{
RearrangeDisksInThePegs( LastProcessedDisk-1, originPeg, destinationPeg, dormentPeg );
MoveDisk( originPeg, destinationPeg);
RearrangeDisksInThePegs( LastProcessedDisk-1, dormentPeg, originPeg, destinationPeg );
}
}
private function MoveDisk( origin:uint, destination:uint)
{
var i:uint = 0;
var j:uint = 0;
// looks for an empty space on the top of the origin peg
while(PegDiskContent[origin]* == null && i< disks_num-1) i++;
// looks for an empty space on the top of the destination peg
//while(PegDiskContent[destination][j] == null && j< disks_num-1) j++;
while(PegDiskContent[destination][j] == null && j< disks_num) j++;
// gets the real position on the top of the destination peg
j--;
// tranferes Disk from the origin Peg to the destination Peg
disk[ parseInt( PegDiskContent[origin]*,10) ].x = PegDiskPos[destination][j][0];
disk[ parseInt( PegDiskContent[origin]*,10) ].y = PegDiskPos[destination][j][1];
PegDiskContent[destination][j] = PegDiskContent[origin]* ;
PegDiskContent[origin]* = null;
}
}
}