I was bored after work, so I came up with the idea of using js to implement algorithm demonstration. I used js to implement selection and sorting a long time ago, but the source program could not be found!
The Recursive Algorithm of qingta:
Void move (int n, char a, char B, char c) {If (n = 1) printf ("\ t % C-> % C \ n",, c); // when N has only one, it moves directly from A to C else {move (n-1, A, C, B ); // The n-1 part is to be moved from A to B printf through C ("\ t % C-> % C \ n", a, c); move (n-1, B,, c); // change B to start disk after N-1 moves, and move B to C through a, which is hard to understand }}
In the above recursive algorithm, the printf function can be replaced by our JS animation processing function. I was inspired by the piata algorithm demo written by others: the animation effect was first executed after the algorithm was executed.
[Demo address]
JS address of the tower of Hanoi algorithm: [hnt. js]
In the init () method, we can see that
// Initialize hnt. init = function (OPT) {$ ("# block" ).html (''); hnt. movie = []; hnt. options = $. extend (hnt. options, OPT); hnt. a. num = hnt. options. n; hnt. b. num = 0; hnt. c. num = 0; hnt. initblock (hnt. options. n); hnt. move (hnt. options. n, "A", "B", "C"); hnt. start ();}
The first half is to use the variable for reinitialization.
Second half:
Initblock (); // initialize the plate
Move (); // The execution of the pie chart algorithm is only an execution algorithm. In this process, the path of the plate movement is saved, but no animation is executed.
Start (); // execute the animation effect.
========================================================== ====================
Later: Now, I just used js to simply implement the demonstration process of the tower of Hanoi. Next I plan to optimize the process and the interface, and I hope I can learn something else. For example, HTML5 DND, audio, woker, localstorage, and jquerymobile frameworks