古人的智慧:汉诺塔 函数的表达
2016-07-21 19:21
531 查看
今天学习了除拼图代码的编写,中午还完成了逻辑题的推测,下午就是玩了个汉诺塔 并用代码写出来
后
对拼图实现了底图的显示成功
var HelloWorldLayer = cc.Layer.extend({ _s: null, _t: null, _e: null, _n: null, _lockPos:null, _tempNum:null, ctor:function () { this._super(); this._init(); }, _init:function(){ this._s = []; this._t = []; this._e = []; this._n = 10; this._index = 0; this._lockPos = 1; for(var i=this._n; i>0; i--) this._s.push(i); this._checkFirst(); }, /* * 开始检查 * */ _check: function(){ var a = this._s[this._s.length-1]; var b = this._t[this._t.length-1]; var c = this._e[this._e.length-1]; this._lockPos = this._getPos(a,b,c); this._changePos(a,b,c); cc.log("次数---", ++this._index); cc.log("a---", this._s.toString()); cc.log("b---", this._t.toString()); cc.log("c---", this._e.toString()); if(this._e.length < this._n) this._check(); }, /* * 获取位置 * */ _getPos:function(a,b,c){ if(this._lockPos == 1){ if(b == undefined) return 3; else if(c == undefined) return 2; else return (b<c)?2:3; } else if(this._lockPos == 2){ if(a == undefined) return 3; else if(c == undefined) return 1; else return (a<c)?1:3; } else if(this._lockPos == 3){ if(a == undefined) return 2; else if(b == undefined) return 1; else return (a<b)?1:2; } }, /* * 获取位置 1表示返回前面一个值 (b,a,c) * */ _getV: function(x,y,z){ if(x%2 == 0){ if(y==undefined){ if(z%2 != 0 && x < z) return 2; return 1; }else{ if(y%2 != 0 && x < y) return 1; return 2; } }else{ if(y==undefined){ if(z%2 == 0 && x < z) return 2; return 1; }else{ if(y%2 == 0 && x < y) return 1; return 2; } } }, /* * * */ _changePos:function(a,b,c){ if(this._lockPos == 1){ if(this._getV(a,b,c) == 1){ this._t.push(this._s.pop()); this._lockPos = 2; } else{ this._e.push(this._s.pop()); this._lockPos = 3; } } else if(this._lockPos == 2){ if(this._getV(b,a,c) == 1){ this._s.push(this._t.pop()) this._lockPos = 1; }else{ this._e.push(this._t.pop()); this._lockPos = 3; } } else if(this._lockPos == 3){ if(this._getV(c,a,b) == 1){ this._s.push(this._e.pop()) this._lockPos = 1; }else { this._t.push(this._e.pop()); this._lockPos = 2; } } }, /* * 第一次 * */ _checkFirst: function(){ if(this._n%2 == 0) { this._t.push(this._s.pop()); this._lockPos = 2; } else{ this._e.push(this._s.pop()); this._lockPos = 3; } this._check(); } }); var HelloWorldScene = cc.Scene.extend({ onEnter:function () { this._super(); var layer = new HelloWorldLayer(); this.addChild(layer); } });
后
对拼图实现了底图的显示成功
相关文章推荐
- JQuery1——基础($对象,选择器,对象转换)
- Android学习笔记(二九):嵌入浏览器
- Android java 与 javascript互访(相互调用)的方法例子
- JavaScript演示排序算法
- javascript实现10进制转为N进制数
- 最后一次说说闭包
- Ajax
- 2019年开发人员应该学习的8个JavaScript框架
- HTML中的script标签研究
- 对一个分号引发的错误研究
- 异步流程控制:7 行代码学会 co 模块
- ES6 走马观花(ECMAScript2015 新特性)
- JavaScript拆分字符串时产生空字符的原因
- Canvas 在高清屏下绘制图片变模糊的解决方法
- Redux系列02:一个炒鸡简单的react+redux例子
- JavaScript 各种遍历方式详解