board.js
changeset 114 1c3fdde0d481
parent 93 c2bf15c3b80b
child 115 9e01c6c0c679
equal deleted inserted replaced
113:fc058d27e829 114:1c3fdde0d481
    22 }
    22 }
    23 /** Set [i][j] element. */
    23 /** Set [i][j] element. */
    24 BoardArr.prototype.set = function(i, j, val) {
    24 BoardArr.prototype.set = function(i, j, val) {
    25     this.brd[4*i + j] = val;
    25     this.brd[4*i + j] = val;
    26 }
    26 }
       
    27 /** Compare boards. */
       
    28 BoardArr.prototype.equals = function(brd) {
       
    29     var from = this.brd, to = brd.brd;
       
    30     // More often corners differ, check it first.
       
    31     return from[0] === to[0] && from[3] === to[3] && from[12] === to[12] && from[15] === to[15]
       
    32         && from[1] === to[1] && from[2] === to[2]
       
    33         && from[4] === to[4] && from[5] === to[5] && from[6] === to[6] && from[7] === to[7]
       
    34         && from[8] === to[8] && from[9] === to[9] && from[10] === to[10] && from[11] === to[11]
       
    35         && from[13] === to[13] && from[14] === to[14];
       
    36 }
    27 /* Return and optionally fill 2d board.
    37 /* Return and optionally fill 2d board.
    28  * Doesn't designed to be efficient. */
    38  * Doesn't designed to be efficient. */
    29 BoardArr.prototype.exportTo = function(brd) {
    39 BoardArr.prototype.exportTo = function(brd) {
    30     brd = brd || [[],[],[],[]];
    40     brd = brd || [[],[],[],[]];
    31     for (var i = 0; i < 4; i++)
    41     for (var i = 0; i < 4; i++)
    36 BoardArr.prototype.copy = function(brd) {
    46 BoardArr.prototype.copy = function(brd) {
    37     brd = brd || new BoardArr();
    47     brd = brd || new BoardArr();
    38     for (var i = 0; i < 16; i++)
    48     for (var i = 0; i < 16; i++)
    39         brd.brd[i] = this.brd[i];
    49         brd.brd[i] = this.brd[i];
    40     return brd;
    50     return brd;
       
    51 }
       
    52 /** Number of free cell. */
       
    53 BoardArr.prototype.free = function() {
       
    54     var cnt = 0;
       
    55     for (var i = 0; i < 16; i++)
       
    56         if (this.brd[i] === 0)
       
    57             cnt++;
       
    58     return cnt;
       
    59 }
       
    60 BoardArr.prototype.score = function() {
       
    61     var score = 0;
       
    62     for (var i = 0; i < 16; i++) {
       
    63         var v = this.brd[i][j];
       
    64         if (v > 1)
       
    65             score += (v-1)*(1 << v);
       
    66     }
       
    67     return score;
       
    68 }
       
    69 BoardArr.prototype.max = function() {
       
    70     var max = 0;
       
    71     for (var i = 0; i < 4; i++) {
       
    72         for (var j = 0; j < 4; j++) {
       
    73             max = Math.max(max, this.brd[i][j]);
       
    74         }
       
    75     }
       
    76     return max;
    41 }
    77 }
    42 
    78 
    43 
    79 
    44 
    80 
    45 ////////////////////////////////////////////////////////////////
    81 ////////////////////////////////////////////////////////////////
    79         && x0[1] === y0[1] && x0[2] === y0[2]
   115         && x0[1] === y0[1] && x0[2] === y0[2]
    80         && x3[1] === y3[1] && x3[2] === y3[2]
   116         && x3[1] === y3[1] && x3[2] === y3[2]
    81         && x1[0] === y1[0] && x1[1] === y1[1] && x1[2] === y1[2] && x1[3] === y1[3]
   117         && x1[0] === y1[0] && x1[1] === y1[1] && x1[2] === y1[2] && x1[3] === y1[3]
    82         && x2[0] === y2[0] && x2[1] === y2[1] && x2[2] === y2[2] && x2[3] === y2[3];
   118         && x2[0] === y2[0] && x2[1] === y2[1] && x2[2] === y2[2] && x2[3] === y2[3];
    83 }
   119 }
       
   120 /** Compare boards. */
    84 BoardArr2d.prototype.equals = BoardArr2d.prototype.equals_unrolled;
   121 BoardArr2d.prototype.equals = BoardArr2d.prototype.equals_unrolled;
    85 
   122 
    86 /* Return and optionally fill 2d board. */
   123 /* Return and optionally fill 2d board. */
    87 BoardArr2d.prototype.exportTo = function(brd) {
   124 BoardArr2d.prototype.exportTo = function(brd) {
    88     brd = brd || [[],[],[],[]];
   125     brd = brd || [[],[],[],[]];
    96     for (var i = 0; i < 4; i++)
   133     for (var i = 0; i < 4; i++)
    97         for (var j = 0; j < 4; j++)
   134         for (var j = 0; j < 4; j++)
    98             brd.brd[i][j] = this.brd[i][j];
   135             brd.brd[i][j] = this.brd[i][j];
    99     return brd;
   136     return brd;
   100 }
   137 }
       
   138 /** Number of free cell. */
   101 BoardArr2d.prototype.free = function() {
   139 BoardArr2d.prototype.free = function() {
   102     var cnt = 0;
   140     var cnt = 0;
   103     for (var i = 0; i < 4; i++)
   141     for (var i = 0; i < 4; i++)
   104         for (var j = 0; j < 4; j++)
   142         for (var j = 0; j < 4; j++)
   105             if (this.brd[i][j] === 0)
   143             if (this.brd[i][j] === 0)
   109 BoardArr2d.prototype.score = function() {
   147 BoardArr2d.prototype.score = function() {
   110     var score = 0;
   148     var score = 0;
   111     for (var i = 0; i < 4; i++) {
   149     for (var i = 0; i < 4; i++) {
   112         for (var j = 0; j < 4; j++) {
   150         for (var j = 0; j < 4; j++) {
   113             var v = this.brd[i][j];
   151             var v = this.brd[i][j];
   114             if (v > 0)
   152             if (v > 1)
   115                 score += (v-1)*Math.pow(2, v);
   153                 score += (v-1)*(1 << v);
   116         }
   154         }
   117     }
   155     }
   118     return score;
   156     return score;
   119 }
   157 }
   120 BoardArr2d.prototype.max = function() {
   158 BoardArr2d.prototype.max = function() {
   762     for (var i = 0; i < 4; i++)
   800     for (var i = 0; i < 4; i++)
   763         for (var j = 0; j < 4; j++)
   801         for (var j = 0; j < 4; j++)
   764             brd[i][j] = this.brd[4*i + j];
   802             brd[i][j] = this.brd[4*i + j];
   765     return brd;
   803     return brd;
   766 }
   804 }
       
   805 /** Compare boards. */
   767 BoardObj.prototype.equals = function(brd) {
   806 BoardObj.prototype.equals = function(brd) {
   768     var self = this.brd;
   807     var self = this.brd;
   769     return self.aa == brd.aa && self.ad == brd.ad && self.da == brd.da && self.dd == brd.dd
   808     return self.aa == brd.aa && self.ad == brd.ad && self.da == brd.da && self.dd == brd.dd
   770         && self.ab == brd.ab && self.ac == brd.ac
   809         && self.ab == brd.ab && self.ac == brd.ac
   771         && self.ba == brd.ba && self.ca == brd.ca
   810         && self.ba == brd.ba && self.ca == brd.ca
   787     brd.ba = self.ba; brd.bb = self.bb; brd.bc = self.bc; brd.bd = self.bd;
   826     brd.ba = self.ba; brd.bb = self.bb; brd.bc = self.bc; brd.bd = self.bd;
   788     brd.ca = self.ca; brd.cb = self.cb; brd.cc = self.cc; brd.cd = self.cd;
   827     brd.ca = self.ca; brd.cb = self.cb; brd.cc = self.cc; brd.cd = self.cd;
   789     brd.da = self.da; brd.db = self.db; brd.dc = self.dc; brd.dd = self.dd;
   828     brd.da = self.da; brd.db = self.db; brd.dc = self.dc; brd.dd = self.dd;
   790     return brd;
   829     return brd;
   791 }
   830 }
       
   831 /** Number of free cell. */
   792 BoardObj.prototype.free = function() {
   832 BoardObj.prototype.free = function() {
   793     var cnt = 0;
   833     var cnt = 0;
   794     var brd = this.brd;
   834     var brd = this.brd;
   795     if (brd.aa === 0) cnt++; if (brd.ab === 0) cnt++; if (brd.ac === 0) cnt++; if (brd.ad === 0) cnt++;
   835     if (brd.aa === 0) cnt++; if (brd.ab === 0) cnt++; if (brd.ac === 0) cnt++; if (brd.ad === 0) cnt++;
   796     if (brd.ba === 0) cnt++; if (brd.bb === 0) cnt++; if (brd.bc === 0) cnt++; if (brd.bd === 0) cnt++;
   836     if (brd.ba === 0) cnt++; if (brd.bb === 0) cnt++; if (brd.bc === 0) cnt++; if (brd.bd === 0) cnt++;