How To Show The Steps of an Iteration in A3
i did actionscript 3 version of old algorithm made in c language solve the “tower of hanoi” puzzle.
the problem knew how show intermediary steps in c language don’t know how in actionscript 3.
the below .as file code uses 3 disks of successive bigger diameters ( , different colors ) created movieclips in flash must move first peg third one, through appropriate moves, in such way end piled in same crescent order in first peg.
the way code written shows beginning disks position , end result not intermediary steps.
i want code being capable show successive disks positions on pegs in each step of process, each time right-click mouse.
how ?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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 content of disk positions , coordinates of positions
for( var d = 0; d<disks_num; d++)
{
//adds array of position x , 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 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 empty space on top of origin peg
while(pegdiskcontent[origin][i] == null && i< disks_num-1) i++;
// looks empty space on top of destination peg
//while(pegdiskcontent[destination][j] == null && j< disks_num-1) j++;
while(pegdiskcontent[destination][j] == null && j< disks_num) j++;
// gets real position on the top of destination peg
j--;
// tranferes disk origin peg destination peg
disk[ parseint( pegdiskcontent[origin][i],10) ].x = pegdiskpos[destination][j][0];
disk[ parseint( pegdiskcontent[origin][i],10) ].y = pegdiskpos[destination][j][1];
pegdiskcontent[destination][j] = pegdiskcontent[origin][i] ;
pegdiskcontent[origin][i] = null;
}
}
}
More discussions in ActionScript 3
adobe
Comments
Post a Comment