board.js
author Oleksandr Gavenko <gavenkoa@gmail.com>
Sun, 07 Sep 2014 00:33:47 +0300
changeset 11 2264f7a3f7e0
parent 9 961eff57a23f
child 12 a9a44cfc3e08
permissions -rw-r--r--
Swap score parameters.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     1
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     2
////////////////////////////////////////////////////////////////
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     3
// Board as linear array.
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     4
////////////////////////////////////////////////////////////////
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     5
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     6
/* Create board on linear array.
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     7
 * Extract data from 'brd' (which is 2d array) if present. */
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     8
function BoardArr(brd) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     9
    this.brd = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    10
    if (brd) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    11
        for (var i = 0; i < 4; i++)
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    12
            for (var j = 0; j < 4; j++)
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    13
                this.brd[4*i + j] = brd[i][j];
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    14
    }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    15
}
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    16
/* Doesn't designed to be efficient. */
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    17
BoardArr.prototype.get = function(i, j) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    18
    return this.brd[4*i + j];
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    19
}
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    20
/* Doesn't designed to be efficient. */
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    21
BoardArr.prototype.set = function(i, j, val) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    22
    this.brd[4*i + j] = val;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    23
}
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    24
/* Return and optionally fill 2d board.
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    25
 * Doesn't designed to be efficient. */
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    26
BoardArr.prototype.exportTo = function(brd) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    27
    brd = brd || [[],[],[],[]];
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    28
    for (var i = 0; i < 4; i++)
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    29
        for (var j = 0; j < 4; j++)
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    30
            brd[i][j] = this.brd[4*i + j];
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    31
    return brd;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    32
}
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    33
BoardArr.prototype.copy = function(brd) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    34
    brd = brd || new BoardArr();
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    35
    for (var i = 0; i < 16; i++)
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    36
        brd.brd[i] = this.brd[i];
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    37
    return brd;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    38
}
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    39
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    40

eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    41
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    42
////////////////////////////////////////////////////////////////
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    43
// Board as 2d array.
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    44
////////////////////////////////////////////////////////////////
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    45
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    46
/* Create board on 2d array.
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    47
 * Extract data from brd (which is 2d array) if present. */
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    48
function BoardArr2d(brd) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    49
    this.brd = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]];
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    50
    if (brd) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    51
        for (var i = 0; i < 4; i++)
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    52
            for (var j = 0; j < 4; j++)
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    53
                this.brd[i][j] = brd[i][j];
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    54
    }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    55
}
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    56
BoardArr.prototype.get = function(i, j) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    57
    return this.brd[i][j];
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    58
}
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    59
BoardArr.prototype.set = function(i, j, val) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    60
    this.brd[i][j] = val;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    61
}
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    62
/* Return and optionally fill 2d board. */
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    63
BoardArr.prototype.exportTo = function(brd) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    64
    brd = brd || [[],[],[],[]];
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    65
    for (var i = 0; i < 4; i++)
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    66
        for (var j = 0; j < 4; j++)
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    67
            brd[i][j] = this.brd[i][j];
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    68
    return brd;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    69
}
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    70
BoardArr2d.prototype.copy = function(brd) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    71
    brd = brd || new BoardArr2d();
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    72
    for (var i = 0; i < 4; i++)
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    73
        for (var j = 0; j < 4; j++)
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    74
            brd.brd[i][j] = this.brd[i][j];
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    75
    return brd;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    76
}
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    77
9
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
    78
BoardArr2d.prototype.canRight = function() {
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
    79
    for (var i = 0; i < 4; i++) {
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
    80
        var f0 = this.brd[i][0], f1 = this.brd[i][1], f2 = this.brd[i][2], f3 = this.brd[i][3];
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
    81
        if (f2 > 0 && (f2 === f3 || f3 === 0))
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
    82
            return true;
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
    83
        if (f1 > 0 && (f1 === f2 || f2 === 0))
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
    84
            return true;
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
    85
        if (f0 > 0 && (f0 === f1 || f1 === 0))
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
    86
            return true;
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
    87
    }
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
    88
    return false;
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
    89
}
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
    90
BoardArr2d.prototype.canLeft = function() {
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
    91
    for (var i = 0; i < 4; i++) {
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
    92
        var f0 = this.brd[i][0], f1 = this.brd[i][1], f2 = this.brd[i][2], f3 = this.brd[i][3];
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
    93
        if (f1 > 0 && (f1 === f0 || f0 === 0))
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
    94
            return true;
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
    95
        if (f2 > 0 && (f2 === f1 || f1 === 0))
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
    96
            return true;
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
    97
        if (f3 > 0 && (f3 === f2 || f2 === 0))
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
    98
            return true;
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
    99
    }
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
   100
    return false;
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
   101
}
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
   102
BoardArr2d.prototype.canUp = function() {
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
   103
    for (var j = 0; j < 4; j++) {
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
   104
        var f0 = this.brd[0][j], f1 = this.brd[1][j], f2 = this.brd[2][j], f3 = this.brd[3][j];
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
   105
        if (f1 > 0 && (f1 === f0 || f0 === 0))
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
   106
            return true;
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
   107
        if (f2 > 0 && (f2 === f1 || f1 === 0))
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
   108
            return true;
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
   109
        if (f3 > 0 && (f3 === f2 || f2 === 0))
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
   110
            return true;
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
   111
    }
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
   112
    return false;
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
   113
}
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
   114
BoardArr2d.prototype.canDown = function() {
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
   115
    for (var j = 0; j < 4; j++) {
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
   116
        var f0 = this.brd[0][j], f1 = this.brd[1][j], f2 = this.brd[2][j], f3 = this.brd[3][j];
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
   117
        if (f2 > 0 && (f2 === f3 || f3 === 0))
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
   118
            return true;
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
   119
        if (f1 > 0 && (f1 === f2 || f2 === 0))
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
   120
            return true;
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
   121
        if (f0 > 0 && (f0 === f1 || f1 === 0))
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
   122
            return true;
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
   123
    }
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
   124
    return false;
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
   125
}
961eff57a23f Add BoardArr2d.prototype.can* functions. Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 6
diff changeset
   126
6
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   127
BoardArr2d.prototype.shiftLeft_unrolled = function(brd) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   128
    var from = this.brd, to = brd.brd;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   129
    for (var i = 3; i >= 0; i--) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   130
        var f0 = from[i][0], f1 = from[i][1], f2 = from[i][2], f3 = from[i][3];
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   131
        if (f3 === 0) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   132
            to[i][0] = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   133
            if (f2 === 0) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   134
                to[i][1] = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   135
                if (f1 === 0) {      // a 0 0 0
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   136
                    to[i][2] = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   137
                    to[i][3] = f0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   138
                } else {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   139
                    if (f0 === f1) {   // a a 0 0
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   140
                        to[i][2] = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   141
                        to[i][3] = f0 + 1;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   142
                    } else {           // a b 0 0
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   143
                        to[i][2] = f0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   144
                        to[i][3] = f1;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   145
                    }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   146
                }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   147
            } else {               // f2 !== 0
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   148
                if (f1 === 0) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   149
                    to[i][1] = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   150
                    if (f0 === f2) {   // a 0 a 0
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   151
                        to[i][3] = f2 + 1;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   152
                        to[i][2] = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   153
                    } else {           // a 0 b 0
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   154
                        to[i][3] = f2;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   155
                        to[i][2] = f0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   156
                    }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   157
                } else {             // f1 !== 0
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   158
                    if (f1 === f2) {   // a b b 0
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   159
                        to[i][3] = f2 + 1;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   160
                        to[i][2] = f0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   161
                        to[i][1] = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   162
                    } else {           // f1 !== f2
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   163
                        to[i][3] = f2;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   164
                        if (f0 === f1) { // a a b 0
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   165
                            to[i][2] = f1 + 1;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   166
                            to[i][1] = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   167
                        } else {         // a b c 0
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   168
                            to[i][2] = f1;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   169
                            to[i][1] = f0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   170
                        }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   171
                    }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   172
                }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   173
            }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   174
        } else {                 // f3 !== 0
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   175
            if (f2 === f3) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   176
                to[i][3] = f2 + 1;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   177
                to[i][0] = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   178
                if (f1 === 0) { // a 0 b b
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   179
                    to[i][2] = f0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   180
                    to[i][1] = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   181
                } else {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   182
                    if (f0 === f1) { // a a b b
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   183
                        to[i][2] = f1 + 1;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   184
                        to[i][1] = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   185
                    } else { // a b c c
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   186
                        to[i][2] = f1;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   187
                        to[i][1] = f0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   188
                    }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   189
                }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   190
            } else { // f2 !== f3
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   191
                to[i][3] = f3;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   192
                if (f2 === 0) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   193
                    to[i][0] = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   194
                    if (f1 === 0) { // a 0 0 b
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   195
                        to[i][1] = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   196
                        to[i][2] = f0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   197
                    } else {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   198
                        if (f0 === f1) { // a a 0 b
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   199
                            to[i][1] = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   200
                            to[i][2] = f1 + 1;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   201
                        } else { // a b 0 c
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   202
                            to[i][1] = f0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   203
                            to[i][2] = f1;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   204
                        }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   205
                    }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   206
                } else { // f2 !== 0 && f2 !== f3
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   207
                    if (f1 === 0) { // a 0 b c
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   208
                        to[i][0] = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   209
                        to[i][1] = f0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   210
                        to[i][2] = f2;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   211
                    } else {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   212
                        if (f0 === f1) { // a a b c
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   213
                            to[i][0] = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   214
                            to[i][1] = f1 + 1;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   215
                            to[i][2] = f2;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   216
                        } else { // a b c d
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   217
                            to[i][0] = f0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   218
                            to[i][1] = f1;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   219
                            to[i][2] = f2;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   220
                        }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   221
                    }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   222
                }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   223
            }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   224
        }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   225
    }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   226
}
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   227
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   228
BoardArr2d.prototype.shiftLeft_mostly_unrolled = function(brd) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   229
    var from = this.brd, to = brd.brd;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   230
    for (var i = 3; i >= 0; i--) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   231
        var f0 = from[i][0], f1 = from[i][1], f2 = from[i][2], f3 = from[i][3];
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   232
        if (f3 === 0) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   233
            if (f2 === 0) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   234
                if (f1 === 0) { // a 0 0 0
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   235
                    f3 = f0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   236
                    f2 = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   237
                } else {  // a b 0 0
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   238
                    f3 = f1;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   239
                    f2 = f0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   240
                }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   241
                f1 = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   242
            } else { // f2 !== 0 && f3 === 0
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   243
                if (f1 === 0) { // a 0 b 0
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   244
                    f3 = f2;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   245
                    f2 = f0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   246
                } else { // a b c 0
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   247
                    f3 = f2;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   248
                    f2 = f1;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   249
                    f1 = f0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   250
                }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   251
            }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   252
            f0 = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   253
        } else { // f3 !== 0
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   254
            if (f2 === 0) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   255
                if (f1 === 0) { // a 0 0 b
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   256
                    f2 = f0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   257
                } else {  // a b 0 c
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   258
                    f2 = f1;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   259
                    f1 = f0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   260
                }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   261
                f0 = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   262
            } else { // f2 !== 0 && f3 !== 0
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   263
                if (f1 === 0) { // a 0 b c
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   264
                    f1 = f0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   265
                    f0 = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   266
                } // else: a b c d
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   267
            }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   268
        }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   269
        if (f2 === 0) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   270
            to[i][0] = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   271
            to[i][1] = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   272
            to[i][2] = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   273
            to[i][3] = f3;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   274
            continue;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   275
        }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   276
        if (f1 === 0) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   277
            to[i][0] = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   278
            to[i][1] = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   279
            if (f2 === f3) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   280
                to[i][2] = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   281
                to[i][3] = f3 + 1;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   282
            } else {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   283
                to[i][2] = f2;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   284
                to[i][3] = f3;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   285
            }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   286
            continue;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   287
        }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   288
        if (f2 === f3) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   289
            to[i][0] = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   290
            to[i][3] = f3 + 1;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   291
            if (f0 === f1) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   292
                to[i][1] = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   293
                to[i][2] = f1 + 1;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   294
            } else {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   295
                to[i][1] = f0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   296
                to[i][2] = f1;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   297
            }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   298
        } else {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   299
            to[i][3] = f3;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   300
            if (f1 === f2) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   301
                to[i][0] = 0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   302
                to[i][1] = f0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   303
                to[i][2] = f2 + 1;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   304
            } else {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   305
                to[i][0] = f0;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   306
                to[i][1] = f1;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   307
                to[i][2] = f2;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   308
            }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   309
        }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   310
    }
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   311
}
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   312
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   313

eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   314
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   315
////////////////////////////////////////////////////////////////
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   316
// Board as properties.
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   317
////////////////////////////////////////////////////////////////
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   318
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   319
/* Create board as properties of object.
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   320
 * Extract data from 'brd' (which is 2d array) if present. */
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   321
function BoardObj(brd) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   322
    if (brd)
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   323
        this.brd = { aa: brd[0][0], ab: brd[0][1], ac: brd[0][2], ad: brd[0][3],
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   324
                     ba: brd[1][0], bb: brd[1][1], bc: brd[1][2], bd: brd[1][3],
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   325
                     ca: brd[2][0], cb: brd[2][1], cc: brd[2][2], cd: brd[2][3],
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   326
                     da: brd[3][0], db: brd[3][1], dc: brd[3][2], dd: brd[3][3] };
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   327
    else
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   328
        this.brd = { aa: 0, ab: 0, ac: 0, ad: 0,
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   329
                     ba: 0, bb: 0, bc: 0, bd: 0,
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   330
                     ca: 0, cb: 0, cc: 0, cd: 0,
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   331
                     da: 0, db: 0, dc: 0, dd: 0 };
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   332
}
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   333
BoardObj.arrMap = [["aa", "ab", "ac", "ad"], ["ba", "bb", "bc", "bd"], ["ca", "cb", "cc", "cd"], ["da", "db", "dc", "dd"]];
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   334
/* Doesn't designed to be efficient. */
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   335
BoardObj.prototype.get = function(i, j) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   336
    return this.brd[BoardObj.arrMap[i][j]];
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   337
}
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   338
/* Doesn't designed to be efficient. */
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   339
BoardObj.prototype.set = function(i, j, val) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   340
    this.brd[BoardObj.arrMap[i][j]] = val;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   341
}
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   342
/* Return and optionally fill 2d board.
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   343
 * Doesn't designed to be efficient. */
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   344
BoardObj.prototype.exportTo = function(brd) {
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   345
    brd = brd || [[],[],[],[]];
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   346
    for (var i = 0; i < 4; i++)
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   347
        for (var j = 0; j < 4; j++)
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   348
            brd[i][j] = this.brd[4*i + j];
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   349
    return brd;
eb31d2025a1d Initial implementation of board back-ends.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   350
}