Here we will construct a HT for web-based Html5+javascript to implement the Hanoi game.
Hanoi game rules and recursive algorithm analysis please refer to Http://en.wikipedia.org/wiki/Tower_of_Hanoi.
Knowing the rules and algorithms of Hanoi, we now start creating elements. Creating a chassis and 3 pillars with the HT for WEB (http://www.hightopo.com) existing 3D template is not a problem, the problem is to create several hollow discs. The first idea is: Create a cylinder, the upper and lower ends of the cylinder is hidden, set the width of the column to achieve the effect of the disk, after several attempts and consult the relevant API documentation, found that the cylinder is no thickness, the method is not feasible.
Later, the WebGL application (http://www.hightopo.com/blog/381.html) of the HT for Web custom 3D model was inspired by the formation of a disc in a rectangle on the XY plane, generated by the rotation of the y-axis one week, by consulting the relevant documentation, The most general decision to adopt HT. Default.createringmodel method to create a disk model, and then create a good model by referencing the Shape3d property when creating node.
In the logic realization, using the advanced post-out principle of the stack, the disk on the cylinder is controlled sequentially, ensuring that each moving disk is the smallest disc.
In the algorithm, using recursive algorithm, through the recursive algorithm, the relocation process step by step record down, and then use the principle of the heap to perform a step-by-step relocation work.
All code and run effects are as follows: http://v.youku.com/v_show/id_XODcwMTk4MDI4.html
var Barnum = 5,//disc number Cylinderheight = Barnum * 20 + 40,//cylinder Height Barrelminoradius = 50,//disc maximum outer radius Barreliradi US = 10,//disc inner radius Poorradius = 20,//disc outer radius Difference Barrelmaxoradius = Barrelminoradius + Barnum * Poorradius, Barrelhe ight = 20,//disc High barpadding = 20,//gap between cylinders Floorx = Barrelmaxoradius * 6 + barpadding * 4,//Chassis length floory = 20, Chassis High Floorz = 2 * Barrelmaxoradius + barpadding * 2,//Chassis width//Column Set positions = [{barrels: [], Position: [-(2*barrelmaxoradius + barpadding), CYLINDERHEIGHT/2 + 1, 0]},{barrels: [] , Position: [0, CYLINDERHEIGHT/2 + 1, 0]},{barrels: [], Position: [(2*barrelma Xoradius + barpadding), CYLINDERHEIGHT/2 + 1, 0]}], Runorder = [],//disk move sequence set//animation parameter params = { Delay:10, duration:500, easing:easing[' Easeboth ']};/** * Initialization program * */function init () {Datamodel = new HT. Datamodel (); G3d = new Ht.graph3d.Graph3dView (Datamodel); view = G3d.getview (); View.classname = ' main '; Document.body.appendChild (view); Window.addeventlistener (' Resize ', function (e) {g3d.invalidate (); }, False); G3d.seteye ([0, Cylinderheight * 2, Floorx * sin (2*pi/360*60)]); Initialize node initnodes (); Moveanimation ();} /** * Constructs game move queue * diskquantity: Number of Discs * Positiona: Starting point * positionb: Transit point * positionc: End point * */function Buildrunorder (diskquantity, Positiona, positionb, Positionc) {if (diskquantity = = 1) {Runorder.push ([Positiona, Positionc]); } else {Buildrunorder (diskQuantity-1, Positiona, Positionc, positionb); Buildrunorder (1, Positiona, positionb, Positionc); Buildrunorder (DiskQuantity-1, positionb, Positiona, Positionc); }}/** * Mobile Animation * Positiona: Starting point * positionc: End point * */function moveanimation (Positiona, Positionc) {if (!positiona) {var poses = Runorder.shift (); if (!poses) {setTimeout (reset, 500); }else{moveanimation (Positions[poses[0]], positions[poses[1]); }}else {var barrel = PositionA.barrels.pop (); var position = PositionC.cylinder.p3 (), Barpos = Barrel.getposition3d (); POSITION[1] = position[1] + floory + barrelheight * POSITIONC.BARRELS.LENGTH-CYLINDERHEIGHT/2; Setpolylinepoints (polyline, barpos, position); Params.action = function (V, t) {var length = G3d.getlinelength (polyline), offset = G3d.getline Offset (polyline, Length * v), point = offset.point, px = point.x, and py = point. Y, pz = point.z; BARREL.P3 (px, py, PZ); }; Params.finishfunc = function () {PositionC.barrels.push (barrel); var poses = Runorder.shift (); if (!poses) {moveanimation (); } else {moveanimation (positions[poses[0]], positions[poses[1]]); } }; Anim = ht. Default.startanim (params); }}/** * Reset Game * */function Reset () {if (positions[0].barrels.length = = 0) {positions[0].barrels = Positions[2].bar Rels } positions[2].barrels = []; for (var i = 0, len = positions[0].barrels.length; i < Len; i++) {var pos = positions[0].cylinder.p3 (); POS[1] = pos[1] + floory + i * BARRELHEIGHT-CYLINDERHEIGHT/2; POSITIONS[0].BARRELS[I].P3 (POS); } buildrunorder (Barnum, 0, 1, 2); SetTimeout (moveanimation, 500);} /** * Initialize node * */function initnodes () {//Chassis floor = CreateNode ([0, FLOORY/2, 0], [Floorx, Floory, Floorz]). S ({ ' Shape3d ': ' box ', ' 3d.movable ': false}); Create column for (var i = 0, len = 3; i < Len; i++) {Positions[i].cylinder = CreateNode (Positions[i].position, [20, Cylinderheight, [], floor). S ({' shape3d ': ' Cylinder ', ' shape3d.color ': ' #E5BB77 ', ' 3d. Movable ': false}); }//Create disc Createbarrels (Barnum, Positions[0].cylinder); Create disk run trajectory polyline = new ht. Polyline (); Polyline.setsegments ([1, 2, 4, 2]); POLYLINE.S ({' shape.background ': null, ' Shape.border.color ': ' Rgba (0,0,0,0) ', ' shape.border.gradient '. Color ': ' Rgba (0,0,0,0) ', ' shape.border.pattern ': [+], ' shape3d.resolution ': 50}); Datamodel.add (polyline);} /** * Set Route node * */function setpolylinepoints (polyline, from, to) {polyline.setpoints ([{x:from[0], y:from[2], E : From[1]}, {x:from[0], y:from[2], e:cylinderheight}, {x:from[0], y:from[2], E:cylinderheight + 60}, {x:to[0], y:to[2], E:cylinderheight +, {x:to[0], y:to[2], e:cylinderheight}, {x:to[0], y:t O[2], e:to[1]}]); return polyline;} /** * Create disc * Barnum: Disk number * Host: Adsorption node * */function Createbarrels (Barnum, host) {//disk initial x position var pos = host.p3 (); for (var i = barnum, j = 0; i > 0; I--, j + +) {Pos[1] = Barrelheight * j + floory; Positions[0].barrels.push (Createbarrel (POS, [1, Barrelheight, 1], Barrelminoradius + I*poorradius, BarrelIRadius, host ). S ({' Shape3d.color ': Randomcolor (), ' 3d.movable ': false})); }}/** * Create node * P3: Node location * S3: Node Size * Host: Adsorption node * */function createnode (P3, S3, host) {var node = new ht. Node (); NODE.P3 (p3); NODE.S3 (S3); Node.sethost (host); NODE.S ({' wf.visible ': ' Selected ', ' wf.color ': ' #FF6B10 ', ' wf.width ': 2, ' Wf.short ': true} ); Datamodel.add (node); return node;} /** * Create Hollow Cylinder * P3: Barrel position * S3: Barrel Size * Oradius: Barrel Outside diameter * Iradius: Barrel Inner Diameter * Host: Adsorption node * */function Createbarrel (P3, S3, Oradius, IRad IUS, host) {return CreateNode (P3, S3, host). S ({' Shape3d ': Ht. Default.createringmodel ([Oradius, 1, Oradius, 0, Iradius, 0, Iradius, 1, Oradius, 1], NULL, $, false, False, 70)});
HT for Web 3D Game Design-Hanoi (Towers of Hanoi)