悔棋的实现

通过这个stack来实现,每走一步 push 棋盘,

/**
 * map stack is provide a stack to regret
 */



MAX_STEP = 4;// max step of stack save map
// you can change value to modify the stack capacity

var MapStack = cc.Class.extend({
    stack: null,//inner data structrue
    index: null,// cursor of stack
    ctor: function () {
        this.stack = new Array(MAX_STEP);
        this.index = 0;
        for (var i = 0; i < MAX_STEP; i++) this.stack[i] = 0;

    },
    
    /** clear the stack
     * @warning this will not remove data so,please use pop or 
         * push to operation this stack not use this.stack...
     * */
    clear: function () {
        this.index = 0;
    },
    
    /** push a member into stack
     * @param arg the member
     * @warning please use deep copy to push member
     */
    push: function (arg) {
        if (this.index == MAX_STEP) {// full
            for (var i = 0; i < MAX_STEP - 1; i++) {
                this.stack[i] = this.stack[i + 1];
            }
            this.index = MAX_STEP - 1;
        }
        
        this.stack[this.index] = arg;
        this.index++;
        cc.log("map stack push:" + arg);

    },
    /**pop a member and return the member which is pop
     * @return member which is pop
     */
    pop: function () {
        if (this.index > 0) {
            var ret = this.stack[this.index - 1];
            this.index--;
            return ret;
        }
        return null;
    },
    /**return the stack'stop member
     * @return the top member of the stack's top
     */
    top: function () {
        if (this.index > 0) return this.stack[this.index - 1];
        return null;
    }
});

 

转载于:https://my.oschina.net/kkkkkkkkkkkkk/blog/707175

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值