當前的邏輯對於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
所以你'win'邏輯僅僅是一個靜態查找表。爲什麼不動態生成更大的N值表?似乎應該是可能的,但我沒有仔細考慮過。 – Matt
只需生成查找表。算術相當簡單。順便提一下, –
好的部件。 –