2013-12-14 38 views
3

當前的邏輯對於3x3板來說工作正常,因爲它是靜態的。 如何將它轉換爲NxN邏輯?NxN板的TicTacToe獲勝邏輯

Win邏輯通過添加行和列方塊來工作。

/* 
* 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

+0

所以你'win'邏輯僅僅是一個靜態查找表。爲什麼不動態生成更大的N值表?似乎應該是可能的,但我沒有仔細考慮過。 – Matt

+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

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]