TicTacToe赢得NxN板的逻辑

时间:2013-12-14 00:40:57

标签: javascript jquery algorithm

当前逻辑与3x3电路板工作正常,因为它是静态的。 如何将其转换为NxN逻辑?

通过添加行和列方块来赢取逻辑。

/*
 * To determine a win condition, each square is "tagged" from left
 * to right, top to bottom, with successive powers of 2.  Each cell
 * thus represents an individual bit in a 9-bit string, and a
 * player's squares at any given time can be represented as a
 * unique 9-bit value. A winner can thus be easily determined by
 * checking whether the player's current 9 bits have covered any
 * of the eight "three-in-a-row" combinations.
 *
 *     273                 84
 *        \               /
 *          1 |   2 |   4  = 7
 *       -----+-----+-----
 *          8 |  16 |  32  = 56
 *       -----+-----+-----
 *         64 | 128 | 256  = 448
 *       =================
 *         73   146   292
 *
 */
wins = [7, 56, 448, 73, 146, 292, 273, 84],

/*
 * Returns whether the given score is a winning score.
 */
win = function (score) {
    for (var i = 0; i < wins.length; i += 1) {
        if ((wins[i] & score) === wins[i]) {
            return true;
        }
    }
    return false;
},

我的小提琴是here

2 个答案:

答案 0 :(得分:2)

因此,要以编程方式执行此操作,您可以使用类来跟踪每个单元格所处的“设置”,即“row1”或“col1”:

i / j创建循环中:

cell.addClass('col' + j); // The cell is in column j
cell.addClass('row' + i); // The cell is in row i
if (i == j) {
    cell.addClass('dia0'); // The cell is in the down/right diagonal
}
if (j == SIZE - i - 1) {
    cell.addClass('dia1'); // The cell is in the up/right diagonal
}

然后,在win()中,单击最后一个单元格。对于单元格所属的每个类,检查包含X(或O)的类的单元格数是否等于表大小:

win = function (clicked) {
    // Get all of the classes this cell belongs to
    var memberOf = clicked[0].className.split(/\s+/);

    // Check elements with the same class, and see if they contain "turn", i.e. X or O
    for (var i=0; i<memberOf.length; i++) {
        var testClass = '.'+memberOf[i];
        // If the number of elements containing "turn" == SIZE,
        // we have a winning condition
        if( $('#tictactoe').find(testClass+':contains('+turn+')').length == SIZE) {
             return true;   
        }
    }

    return false;
},

JSFiddle Demo

答案 1 :(得分:0)

这匹配你的三个名单,我手动检查了两个。这当然是正确的,因为代码真的很简单,但你可能想要检查它:

var getWins = function(size) {
    var val = 1, cells = [], wins = [];
    for (var i = 0; i < size; i++) {
        cells[i] = [];
        for (var j = 0; j < size; j++) {
            cells[i][j] = val;
            val *= 2;
        }
    }
    var rowWins = [], colWins = [], mainDiagWin = 0, antiDiagWin = 0;
    for (i = 0; i < size; i++) {
        rowWins[i] = 0;
        colWins[i] = 0;
        mainDiagWin += cells[i][i];
        antiDiagWin += cells[i][size - i - 1];
        for (j = 0; j < size; j++) {
            rowWins[i] += cells[i][j];
            colWins[i] += cells[j][i];
        }
    }
    return rowWins.concat(colWins, mainDiagWin, antiDiagWin);
};

getWins(2);
//=> [3, 12, 5, 10, 9, 6]

getWins(3);
//=> [7, 56, 448, 73, 146, 292, 273, 84]

getWins(4)
//=> [15, 240, 3840, 61440, 4369, 8738, 17476, 34952, 33825, 4680]