您的位置:首页 > Web前端 > JavaScript

古人的智慧:汉诺塔 函数的表达

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);
}
});



对拼图实现了底图的显示成功
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息