board.js
changeset 14 9b49e710f5a7
parent 12 a9a44cfc3e08
child 20 ab294e8db00c
equal deleted inserted replaced
13:b3bfa9d1b537 14:9b49e710f5a7
    57     return this.brd[i][j];
    57     return this.brd[i][j];
    58 }
    58 }
    59 BoardArr2d.prototype.set = function(i, j, val) {
    59 BoardArr2d.prototype.set = function(i, j, val) {
    60     this.brd[i][j] = val;
    60     this.brd[i][j] = val;
    61 }
    61 }
       
    62 BoardArr2d.prototype.equals = function(brd) {
       
    63     for (var i = 0; i < 4; i++)
       
    64         for (var j = 0; j < 4; j++)
       
    65             if (this.brd[i][j] !== brd.brd[i][j])
       
    66                 return false;
       
    67     return true;
       
    68 }
    62 /* Return and optionally fill 2d board. */
    69 /* Return and optionally fill 2d board. */
    63 BoardArr2d.prototype.exportTo = function(brd) {
    70 BoardArr2d.prototype.exportTo = function(brd) {
    64     brd = brd || [[],[],[],[]];
    71     brd = brd || [[],[],[],[]];
    65     for (var i = 0; i < 4; i++)
    72     for (var i = 0; i < 4; i++)
    66         for (var j = 0; j < 4; j++)
    73         for (var j = 0; j < 4; j++)
    82             if (v > 0)
    89             if (v > 0)
    83                 score += (v-1)*Math.pow(2, v);
    90                 score += (v-1)*Math.pow(2, v);
    84         }
    91         }
    85     }
    92     }
    86     return score;
    93     return score;
       
    94 }
       
    95 BoardArr2d.prototype.max = function() {
       
    96     var max = 0;
       
    97     for (var i = 0; i < 4; i++) {
       
    98         for (var j = 0; j < 4; j++) {
       
    99             max = Math.max(max, this.brd[i][j]);
       
   100         }
       
   101     }
       
   102     return max;
    87 }
   103 }
    88 
   104 
    89 BoardArr2d.prototype.canRight = function() {
   105 BoardArr2d.prototype.canRight = function() {
    90     for (var i = 0; i < 4; i++) {
   106     for (var i = 0; i < 4; i++) {
    91         var f0 = this.brd[i][0], f1 = this.brd[i][1], f2 = this.brd[i][2], f3 = this.brd[i][3];
   107         var f0 = this.brd[i][0], f1 = this.brd[i][1], f2 = this.brd[i][2], f3 = this.brd[i][3];