因此,要以编程方式执行此 *** 作,可以使用类来跟踪每个单元格所在的“设置”,即“ row1”或“ col1”:
在
i/
j创建循环中:
cell.addClass('col' + j); // The cell is in column jcell.addClass('row' + i); // The cell is in row iif (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演示
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)