2011-05-12 130 views
0
if(isEmptySquare(this)){ 

     adjacents = adjacentSquares(this); 

     $(adjacents).each(function(){ 

      uncoverSquare(this); 

        //if(isEmptySquare(this)){ 

      //adjacents = adjacentSquares(this); 

      //$(adjacents).each(function(){ 

       //uncoverSquare(this); 

     //});     
    //} 

     });     
    } 

function adjacentSquares(square){ 

      var thisRow = $(square).parent().parent().children().index($(square).parent()); 
      var thisCol = $(square).parent().children().index($(square)); 
      var prevRow = (thisRow-1); 
      var nextRow = (thisRow+1); 

      if(thisCol == 0){sliceFrom = 0;} else { sliceFrom = (thisCol-1);} 

      var above = $('tr:eq('+prevRow+')').children('td').slice((sliceFrom),(thisCol+2)); 
      var below = $('tr:eq('+nextRow+')').children('td').slice((sliceFrom),(thisCol+2)); 
      var aboveBelow = $.merge(above,below); 
      var prevNext = $.merge(($(square).next('td')),($(square).prev('td'))); 
      var adjacents = $.merge(aboveBelow,prevNext); 

      return adjacents; 

     } 

function isEmptySquare(square){ 

     if($(square).filter(function(){return !/[0-9]/.test($(square).text());}).not(":contains('x')").length>0){ 

      return true; 

     } else { 

      return false; 
     } 
    } 

我該如何開始爲使用jQuery的表格單元格做洪水填充算法?jQuery洪水填充算法

當前用戶單擊表格單元格時,如果它是空的,則會找到其相鄰單元格。我發現讓它工作的一種方法是重複每個相鄰方塊的代碼(我已經評論了這部分代碼),然後重複代碼,重複代碼.....雖然它變得沒有反應。

我試圖用jQuery做一個minsweeper遊戲,有人向我提到了一個洪水填充算法,儘管我不確定如何實現這個。

回答