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

今天学习了除拼图代码的编写,中午还完成了逻辑题的推测,下午就是玩了个 汉诺塔  并用代码写出来
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);
    }
});

     后 

对拼图实现了底图的显示成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值