Question

   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; 
        }
    }

How would I begin to go about doing a flood fill algorithm for table cells using jQuery?

Currently when a user clicks on a table cell, if it is empty then its adjacent cells are uncovered. One way I found of getting it to work was to repeat the code for each of the adjacent squares (I have commented this part of the code out), and then repeat the code, repeat the code.....although it became unresponsive.

I'm trying to make a minsweeper game with jQuery, and somebody has mentioned to me a flood fill algorithm, although I am unsure how to implement this.

Was it helpful?

Solution

Just incase anyone stumbles across this. i found the answer i was looking for here http://www.htmlgoodies.com/primers/jsp/article.php/3622321/Javascript-Basics-Part-12.htm

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top