亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Jquery:檢查單元格是否水平,垂直或對角對齊

Jquery:檢查單元格是否水平,垂直或對角對齊

海綿寶寶撒 2022-05-26 16:59:50
我有一個 6x7 表,當我單擊特定單元格時,我需要檢查:- 同一行上的4 個連續單元格或 - 同一列上的4 個連續單元格或 - 同一對角線上的 4 個連續單元格是否具有屬性類藍色的。我試了一下,似乎水平和垂直檢查工作正常。但不是對角線。有任何想法嗎?$(".circle").click(function() {  var colindex = $(this).closest('td').index() + 1;  var rowindex = $(this).closest('tr').index() + 1;  if(checkHorizontally() || checkVertically() || checkDiagonally()){      console.log("Blue wins");  }  function checkHorizontally(){    var sum;    for(i=6;i>0;i--){      for(j=1;j<=7;j++){        var cell = $('tr:nth-child('+i+') td:nth-child('+j+')');        if (cell.find('div').css('background-color')==='rgb(0, 0, 255)'){           sum+=1;        }        else{           sum=0;        }        if(sum>=4){          console.log("blue wins horizontally");          return true;        }      }      sum=0;    }  }function checkVertically(){  var sum;  for(i=1;i<=7;i++){    for(j=1;j<=6;j++){      var cell = $('tr:nth-child('+j+') td:nth-child('+i+')');      if (cell.find('div').css('background-color')==='rgb(0, 0, 255)'){        sum+=1;      }      else{        sum=0;      }    if(sum>=4){      console.log("blue wins vertically");      return true;    }  }  sum=0;  }}function checkDiagonally(){  var sum;  for(k=1;k<=7;k++){      for(var y=1, x=k; x<7 ; y++,x++){      var cell = $('tr:nth-child('+y+') td:nth-child('+x+')');      if (cell.find('div').css('background-color')==='rgb(0, 0, 255)'){        sum+=1;      }      else{        sum=0;      }    if(sum>=4){      console.log("blue wins diagonally");      return true;    }  }  sum=0;  }}    });
查看完整描述

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;

          }

        }

    }

  }


查看完整回答
反對 回復 2022-05-26
?
holdtom

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;

}


查看完整回答
反對 回復 2022-05-26
  • 2 回答
  • 0 關注
  • 133 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號