CAAT 游戏 sumon 算法

猜下一个数字的算法

setGuessNumber : function ()
        {
            var a = [], b, c;
            for (b = 0; b < this.rows; b++) {
                for (c = 0; c < this.columns; c++) {
                    this.data[b][c].removed || a.push(this.data[b][c]);  //是可见的数字,注意||的用法
                 }
            }
            if (a.length > 1)  //打乱数字,避免数字相邻选择
             {
                for (b = 0; b < a.length; b++) {
                    c = Math.random() * a.length >> 0;
                    var d = a[b];
                    a[b] = a[c];
                    a[c] = d 
                }
                c = 0;
            }
            b = this.brickIncrementByDifficulty[this.difficulty];   //  difficulty=0,b=5;
              d = this.level - 1;  //level1就是 d=0
             d >= this.gameMode.number_policy.length && (d = this.gameMode.number_policy.length - 1);   //如果通过了最后一关,就是停到最后一关
             d = this.gameMode.number_policy[d];    //d=10
            var e = d + b, f = 0;      //e=15
             if (a.length == 1) {   //对于最后一个数,猜的数就是它
                 c = a[0].value;
            }
            else if (a.length == 2) {   //对于只剩下两个数,猜的数就是两个的和,
                 c = a[0].value + a[1].value;
            }
            else
            {
                for (b = 0; b < a.length; b++)
                {
                    if (c + a[b].value <= e) {
                        c += a[b].value, f++;
                    }
                    else if (c > d) {
                        break;
                    }
                    f == 1 && (c = a[0].value + a[1].value);
                }
            }
            this.guessNumber = c;
            this.fireEvent("context", "guessnumber", this);
            this.setMultipliers()
        },

   brickIncrementByDifficulty : [5, 10],是难度增加的值

 


 number_policy : [10, 10, 10, 10, 10, 15, 15, 15, 15, 20, 25, 30, 35,
            40, 45, 50],

 

选择了数字的时候的算法

        selectionChanged : function (a)
        {
            var b;
            for (b = 0; b < this.selectedList.length; b++)
            {
                if (this.selectedList[b] == a) {
                    this.selectedList.splice(b, 1);
                    this.fireEvent("brick", "selection", a);
                    return
                }
                var c = 0;
                for (b = 0; b < this.selectedList.length; b++) {
                    c += this.selectedList[b].value;
                }
                c += a.value;
                if (c > this.guessNumber)   //数字超过,需要重新选择
                {
                    a.selected = !1;
                    a = this.selectedList.slice(0);
                    for (b = 0; b < this.selectedList.length; b++) {
                        this.selectedList[b].selected = !1;
                    }
                    this.selectedList = [];
                    this.fireEvent("brick", "selectionoverflow", a)
                }
                else if (c == this.guessNumber)    //等于猜的数字,就是过关
                {
                    this.selectedList.push(a);
                    a = this.selectedList.slice(0);
                    for (b = 0; b < this.selectedList.length; b++) {
                        this.selectedList[b].selected = !1, this.selectedList[b].removed = !0;
                    }
                    if (this.gameMode.rearrange_on_remove)
                    {
                        for (b = 0; b < this.selectedList.length; b++)
                        {
                            c = this.selectedList[b].column;
                            for (var d = this.selectedList[b].row; d > 0; d--)
                            {
                                var e = this.data[d - 1][c], f = this.data[d][c], g = e;
                                this.data[d - 1][c] = this.data[d][c];
                                this.data[d][c] = g;
                                g = e.row;
                                e.row = f.row;
                                f.row = g;
                                this.fireEvent("brick", "rearranged", {
                                    fromRow : e.row - 1, toRow : e.row, column : c
                                })
                            }
                        }
                        this.selectedList = [];
                    }
                    this.fireEvent("brick", "selection-cleared", a);
                    this.score += this.multiplier * (a.length + 1) * (this.difficulty == 0 ? 10 : 20) * a.length;
                    this.fireEvent("context", "score", this);
                    for (b = 0; b < this.rows; b++)
                    {
                        for (a = 0; a < this.columns; a++)
                        {
                            if (!this.data[b][a].removed) {
                                this.setGuessNumber();
                                return
                            }
                            this.setStatus(this.ST_LEVEL_RESULT) ;;
                        }
                    }
                }
                else
                {
                    this.selectedList.push(a), this.fireEvent("brick", "selection", a), this.setMultipliers();
                }
            }
        },

 游戏模式

 


var HN = HN || {};
(function ()
{
    HN.GameModes =
    {
        classic :
        {
            fixed_table_size :!0, rearrange_on_remove :!0,

         rows_initial : 8,

         columns_initial : 8,
    rows_max : 8,
            columns_max : 8, time_policy :- 500, minTurnTime : 12E3,
    number_policy : [10, 10, 10, 15,
            15, 15, 20, 20, 25, 30, 35, 40, 45, 50],
    name : "classic"
        },
        progressive :
        {
            fixed_table_size :!1, rearrange_on_remove :!0, rows_initial : 3, columns_initial : 3, rows_max : 8,
            columns_max : 8, time_policy : 0,
    number_policy : [10, 10, 10, 10, 10, 15, 15, 15, 15, 20,
            25, 30, 35, 40, 45, 50], name : "progressive"
        },
        respawn :
        {
            fixed_table_size :!0, rearrange_on_remove :!0, respawn :!0, respawn_time : 22E3, rows_initial : 8,
            columns_initial : 8,
    rows_max : 8,
    columns_max : 8,
    time_policy :- 1E3,
    initial_map : [[0,
            0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0,
            0, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 1, 1, 0], [1,
            1, 1, 1, 1, 1, 1, 1]],
    number_policy : [10, 10, 10, 10, 10, 15, 15, 15, 15, 20, 25, 30, 35,
            40, 45, 50],
    name : "progressive"
        }
    }
})();

 

你可能感兴趣的:(CAAT 游戏 sumon 算法)