2 回答

TA貢獻1851條經驗 獲得超5個贊
這是一個解決方案。我實現了兩個函數,一個檢查 4 個單元格是否從左上角對齊,一個從右下角檢查。兩者都從第四行開始,因為在第四行之前沒有 4 個連續的單元格。
function checkDiagonally(color){
var numRows = $('table tr').length;
var numCols = $( "table tr:last td" ).length;
for (var j = 4; j < numCols ; j++)
{
for (var i = 1 ; i <= 4; i++)
{
var cell1 = $('tr:nth-child('+j+') td:nth-child('+i+')');
var cell2 = $('tr:nth-child('+(j-1)+') td:nth-child('+(i+1)+')');
var cell3 = $('tr:nth-child('+(j-2)+') td:nth-child('+(i+2)+')');
var cell4 = $('tr:nth-child('+(j-3)+') td:nth-child('+(i+3)+')');
if (cell1.find('div').css('background-color')===color &&
cell2.find('div').css('background-color')===color &&
cell3.find('div').css('background-color')===color &&
cell4.find('div').css('background-color')===color){
return true;
}
}
}
}
function checkDiagonallyRTL(color){
var numRows = $('table tr').length;
var numCols = $( "table tr:last td" ).length;
for (var j = 4; j < numCols ; j++)
{
for (var i = 7 ; i >= 4; i--)
{
var cell1 = $('tr:nth-child('+j+') td:nth-child('+i+')');
var cell2 = $('tr:nth-child('+(j-1)+') td:nth-child('+(i-1)+')');
var cell3 = $('tr:nth-child('+(j-2)+') td:nth-child('+(i-2)+')');
var cell4 = $('tr:nth-child('+(j-3)+') td:nth-child('+(i-3)+')');
if (cell1.find('div').css('background-color')===color &&
cell2.find('div').css('background-color')===color &&
cell3.find('div').css('background-color')===color &&
cell4.find('div').css('background-color')===color){
return true;
}
}
}
}

TA貢獻1805條經驗 獲得超10個贊
我們檢查該項目是否真的是藍色的。如果沒有,那么游戲就不會贏。否則,我們會檢查 4 個方向的項目。我們在所有四個方向上都有三個案例,具體取決于我們檢查的模式中項目的位置。
function check(item) {
if (!item.hasClass("blue")) return false;
var td = item.parent();
var tr = td.parent();
var tds = tr.find("td");
var tdIndex = tds.index(td);
var trParent = tr.parent();
var trs = trParent.find("tr");
var trIndex = trs.index(tr); //Initializing helper variables for the rows and columns
if (tds.length >= 3) {
if (tdIndex >= 2) {
if (td.prev().find(".circle.blue").length && td.prev().prev().find(".circle.blue").length) return true;
}
if (tdIndex < tds.length - 2) {
if (td.next().find(".circle.blue").length && td.next().next().find(".circle.blue").length) return true;
}
if ((tdIndex > 0) && (tdIndex < tds.length - 1)) {
if (td.prev().find(".circle.blue").length && td.next().find(".circle.blue").length) return true;
}
}
if (trs.length >= 3) {
if (trIndex >= 2) {
if ($(tr.prev().find("td")[tdIndex]).find(".circle.blue").length && $(tr.prev().prev().find("td")[tdIndex]).find(".circle.blue").length) return true;
}
if (trIndex < trs.length - 2) {
if ($(tr.next().find("td")[tdIndex]).find(".circle.blue").length && $(tr.next().next().find("td")[tdIndex]).find(".circle.blue").length) return true;
}
if ((trIndex > 0) && (trIndex < trs.length - 1)) {
if ($(tr.prev().find("td")[tdIndex]).find(".circle.blue").length && $(tr.next().find("td")[tdIndex]).find(".circle.blue").length) return true;
}
}
if ((trs.length >= 3) && (tds.length >= 3)) {
if ((trIndex >= 2) && (tdIndex >= 2)) {
if ($(tr.prev().find("td")[tdIndex - 1]).find(".circle.blue").length && $(tr.prev().prev().find("td")[tdIndex - 2]).find(".circle.blue").length) return true;
}
if ((trIndex >= 2) && (tdIndex < tds.length - 2)) {
if ($(tr.prev().find("td")[tdIndex + 1]).find(".circle.blue").length && $(tr.prev().prev().find("td")[tdIndex + 2]).find(".circle.blue").length) return true;
}
if ((trIndex < trs.length - 2) && (tdIndex >= 2)) {
if ($(tr.next().find("td")[tdIndex - 1]).find(".circle.blue").length && $(tr.next().next().find("td")[tdIndex - 2]).find(".circle.blue").length) return true;
}
if ((trIndex < trs.length - 2) && (tdIndex < tds.length - 2)) {
if ($(tr.next().find("td")[tdIndex + 1]).find(".circle.blue").length && $(tr.next().next().find("td")[tdIndex + 2]).find(".circle.blue").length) return true;
}
if ((trIndex > 0) && (trIndex < trs.length - 1) && (tdIndex > 0) && (tdIndex < tds.length - 1)) {
if (
($(tr.prev().find("td")[tdIndex - 1]).find(".circle.blue").length && $(tr.next().find("td")[tdIndex + 1]).find(".circle.blue").length) ||
($(tr.prev().find("td")[tdIndex + 1]).find(".circle.blue").length && $(tr.next().find("td")[tdIndex - 1]).find(".circle.blue").length)
) return true;
}
}
return false;
}
添加回答
舉報