Initial implementation of board back-ends.
authorOleksandr Gavenko <gavenkoa@gmail.com>
Sat, 06 Sep 2014 22:57:36 +0300
changeset 6 eb31d2025a1d
parent 5 f1dd66c68f4e
child 7 d8106c8c0481
Initial implementation of board back-ends.
board.js
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/board.js	Sat Sep 06 22:57:36 2014 +0300
@@ -0,0 +1,304 @@
+
+////////////////////////////////////////////////////////////////
+// Board as linear array.
+////////////////////////////////////////////////////////////////
+
+/* Create board on linear array.
+ * Extract data from 'brd' (which is 2d array) if present. */
+function BoardArr(brd) {
+    this.brd = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
+    if (brd) {
+        for (var i = 0; i < 4; i++)
+            for (var j = 0; j < 4; j++)
+                this.brd[4*i + j] = brd[i][j];
+    }
+}
+/* Doesn't designed to be efficient. */
+BoardArr.prototype.get = function(i, j) {
+    return this.brd[4*i + j];
+}
+/* Doesn't designed to be efficient. */
+BoardArr.prototype.set = function(i, j, val) {
+    this.brd[4*i + j] = val;
+}
+/* Return and optionally fill 2d board.
+ * Doesn't designed to be efficient. */
+BoardArr.prototype.exportTo = function(brd) {
+    brd = brd || [[],[],[],[]];
+    for (var i = 0; i < 4; i++)
+        for (var j = 0; j < 4; j++)
+            brd[i][j] = this.brd[4*i + j];
+    return brd;
+}
+BoardArr.prototype.copy = function(brd) {
+    brd = brd || new BoardArr();
+    for (var i = 0; i < 16; i++)
+        brd.brd[i] = this.brd[i];
+    return brd;
+}
+BoardArr.prototype.show = function(fn) {
+BoardArr.prototype.shiftLeft = function(brd) {
+}
+
+
+
+////////////////////////////////////////////////////////////////
+// Board as 2d array.
+////////////////////////////////////////////////////////////////
+
+/* Create board on 2d array.
+ * Extract data from brd (which is 2d array) if present. */
+function BoardArr2d(brd) {
+    this.brd = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]];
+    if (brd) {
+        for (var i = 0; i < 4; i++)
+            for (var j = 0; j < 4; j++)
+                this.brd[i][j] = brd[i][j];
+    }
+}
+BoardArr.prototype.get = function(i, j) {
+    return this.brd[i][j];
+}
+BoardArr.prototype.set = function(i, j, val) {
+    this.brd[i][j] = val;
+}
+/* Return and optionally fill 2d board. */
+BoardArr.prototype.exportTo = function(brd) {
+    brd = brd || [[],[],[],[]];
+    for (var i = 0; i < 4; i++)
+        for (var j = 0; j < 4; j++)
+            brd[i][j] = this.brd[i][j];
+    return brd;
+}
+BoardArr2d.prototype.copy = function(brd) {
+    brd = brd || new BoardArr2d();
+    for (var i = 0; i < 4; i++)
+        for (var j = 0; j < 4; j++)
+            brd.brd[i][j] = this.brd[i][j];
+    return brd;
+}
+
+BoardArr2d.prototype.shiftLeft_unrolled = function(brd) {
+    var from = this.brd, to = brd.brd;
+    for (var i = 3; i >= 0; i--) {
+        var f0 = from[i][0], f1 = from[i][1], f2 = from[i][2], f3 = from[i][3];
+        if (f3 === 0) {
+            to[i][0] = 0;
+            if (f2 === 0) {
+                to[i][1] = 0;
+                if (f1 === 0) {      // a 0 0 0
+                    to[i][2] = 0;
+                    to[i][3] = f0;
+                } else {
+                    if (f0 === f1) {   // a a 0 0
+                        to[i][2] = 0;
+                        to[i][3] = f0 + 1;
+                    } else {           // a b 0 0
+                        to[i][2] = f0;
+                        to[i][3] = f1;
+                    }
+                }
+            } else {               // f2 !== 0
+                if (f1 === 0) {
+                    to[i][1] = 0;
+                    if (f0 === f2) {   // a 0 a 0
+                        to[i][3] = f2 + 1;
+                        to[i][2] = 0;
+                    } else {           // a 0 b 0
+                        to[i][3] = f2;
+                        to[i][2] = f0;
+                    }
+                } else {             // f1 !== 0
+                    if (f1 === f2) {   // a b b 0
+                        to[i][3] = f2 + 1;
+                        to[i][2] = f0;
+                        to[i][1] = 0;
+                    } else {           // f1 !== f2
+                        to[i][3] = f2;
+                        if (f0 === f1) { // a a b 0
+                            to[i][2] = f1 + 1;
+                            to[i][1] = 0;
+                        } else {         // a b c 0
+                            to[i][2] = f1;
+                            to[i][1] = f0;
+                        }
+                    }
+                }
+            }
+        } else {                 // f3 !== 0
+            if (f2 === f3) {
+                to[i][3] = f2 + 1;
+                to[i][0] = 0;
+                if (f1 === 0) { // a 0 b b
+                    to[i][2] = f0;
+                    to[i][1] = 0;
+                } else {
+                    if (f0 === f1) { // a a b b
+                        to[i][2] = f1 + 1;
+                        to[i][1] = 0;
+                    } else { // a b c c
+                        to[i][2] = f1;
+                        to[i][1] = f0;
+                    }
+                }
+            } else { // f2 !== f3
+                to[i][3] = f3;
+                if (f2 === 0) {
+                    to[i][0] = 0;
+                    if (f1 === 0) { // a 0 0 b
+                        to[i][1] = 0;
+                        to[i][2] = f0;
+                    } else {
+                        if (f0 === f1) { // a a 0 b
+                            to[i][1] = 0;
+                            to[i][2] = f1 + 1;
+                        } else { // a b 0 c
+                            to[i][1] = f0;
+                            to[i][2] = f1;
+                        }
+                    }
+                } else { // f2 !== 0 && f2 !== f3
+                    if (f1 === 0) { // a 0 b c
+                        to[i][0] = 0;
+                        to[i][1] = f0;
+                        to[i][2] = f2;
+                    } else {
+                        if (f0 === f1) { // a a b c
+                            to[i][0] = 0;
+                            to[i][1] = f1 + 1;
+                            to[i][2] = f2;
+                        } else { // a b c d
+                            to[i][0] = f0;
+                            to[i][1] = f1;
+                            to[i][2] = f2;
+                        }
+                    }
+                }
+            }
+        }
+    }
+}
+
+BoardArr2d.prototype.shiftLeft_mostly_unrolled = function(brd) {
+    var from = this.brd, to = brd.brd;
+    for (var i = 3; i >= 0; i--) {
+        var f0 = from[i][0], f1 = from[i][1], f2 = from[i][2], f3 = from[i][3];
+        if (f3 === 0) {
+            if (f2 === 0) {
+                if (f1 === 0) { // a 0 0 0
+                    f3 = f0;
+                    f2 = 0;
+                } else {  // a b 0 0
+                    f3 = f1;
+                    f2 = f0;
+                }
+                f1 = 0;
+            } else { // f2 !== 0 && f3 === 0
+                if (f1 === 0) { // a 0 b 0
+                    f3 = f2;
+                    f2 = f0;
+                } else { // a b c 0
+                    f3 = f2;
+                    f2 = f1;
+                    f1 = f0;
+                }
+            }
+            f0 = 0;
+        } else { // f3 !== 0
+            if (f2 === 0) {
+                if (f1 === 0) { // a 0 0 b
+                    f2 = f0;
+                } else {  // a b 0 c
+                    f2 = f1;
+                    f1 = f0;
+                }
+                f0 = 0;
+            } else { // f2 !== 0 && f3 !== 0
+                if (f1 === 0) { // a 0 b c
+                    f1 = f0;
+                    f0 = 0;
+                } // else: a b c d
+            }
+        }
+        if (f2 === 0) {
+            to[i][0] = 0;
+            to[i][1] = 0;
+            to[i][2] = 0;
+            to[i][3] = f3;
+            continue;
+        }
+        if (f1 === 0) {
+            to[i][0] = 0;
+            to[i][1] = 0;
+            if (f2 === f3) {
+                to[i][2] = 0;
+                to[i][3] = f3 + 1;
+            } else {
+                to[i][2] = f2;
+                to[i][3] = f3;
+            }
+            continue;
+        }
+        if (f2 === f3) {
+            to[i][0] = 0;
+            to[i][3] = f3 + 1;
+            if (f0 === f1) {
+                to[i][1] = 0;
+                to[i][2] = f1 + 1;
+            } else {
+                to[i][1] = f0;
+                to[i][2] = f1;
+            }
+        } else {
+            to[i][3] = f3;
+            if (f1 === f2) {
+                to[i][0] = 0;
+                to[i][1] = f0;
+                to[i][2] = f2 + 1;
+            } else {
+                to[i][0] = f0;
+                to[i][1] = f1;
+                to[i][2] = f2;
+            }
+        }
+    }
+}
+
+
+
+////////////////////////////////////////////////////////////////
+// Board as properties.
+////////////////////////////////////////////////////////////////
+
+/* Create board as properties of object.
+ * Extract data from 'brd' (which is 2d array) if present. */
+function BoardObj(brd) {
+    if (brd)
+        this.brd = { aa: brd[0][0], ab: brd[0][1], ac: brd[0][2], ad: brd[0][3],
+                     ba: brd[1][0], bb: brd[1][1], bc: brd[1][2], bd: brd[1][3],
+                     ca: brd[2][0], cb: brd[2][1], cc: brd[2][2], cd: brd[2][3],
+                     da: brd[3][0], db: brd[3][1], dc: brd[3][2], dd: brd[3][3] };
+    else
+        this.brd = { aa: 0, ab: 0, ac: 0, ad: 0,
+                     ba: 0, bb: 0, bc: 0, bd: 0,
+                     ca: 0, cb: 0, cc: 0, cd: 0,
+                     da: 0, db: 0, dc: 0, dd: 0 };
+}
+BoardObj.arrMap = [["aa", "ab", "ac", "ad"], ["ba", "bb", "bc", "bd"], ["ca", "cb", "cc", "cd"], ["da", "db", "dc", "dd"]];
+/* Doesn't designed to be efficient. */
+BoardObj.prototype.get = function(i, j) {
+    return this.brd[BoardObj.arrMap[i][j]];
+}
+/* Doesn't designed to be efficient. */
+BoardObj.prototype.set = function(i, j, val) {
+    this.brd[BoardObj.arrMap[i][j]] = val;
+}
+/* Return and optionally fill 2d board.
+ * Doesn't designed to be efficient. */
+BoardObj.prototype.exportTo = function(brd) {
+    brd = brd || [[],[],[],[]];
+    for (var i = 0; i < 4; i++)
+        for (var j = 0; j < 4; j++)
+            brd[i][j] = this.brd[4*i + j];
+    return brd;
+}