Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Would help to know what language you are using but essentially what you need is a loop that performs a look-around on each step.</p> <p>This is all psudocode so you will have to adapt to what ever language you are using.</p> <pre><code>// your array looks something like this but I will assume that all the numbers are characters Array myArray = [ [1,2,3,4,5,6,7,8] , [1,2,3,4,5,6,7,8] , [1,2,3,4,5,6,7,8] , [1,2,3,4,5,6,7,8] , [1,2,3,4,5,6,7,8] , [1,2,3,4,5,6,7,8] , [1,2,3,4,5,6,7,8] ,]; // set up a variable to store how many we need together to get a match int required = 3; // lets go with 3 // loop through the nested arrays for(int i = 0; i &lt; myArray.length; i++){ // loop through the elements in the nested array for(int j = 0; myArray[i].length; i++){ // get the character we are looking for char c = myArray[i][j]; // we will need to know where we are coming from first Point p = {x:j,y:i};// setting p.x = j and p.y = i // now lets perform the look-around (I will assume no diagonals) Array result = [p];// array with the original point result = lookAround(result, myArray); // if we got a result then you can proccess it in your game how you like and exit the loop. if(result != null){ // do game logic here and you can remove the points in the result array } } } </code></pre> <p>Now for the look-around method:</p> <pre><code>// this method returns an array of points if match is found or null otherwise. // a is our array, from is the last point checked or null if it's the first point, to is the point to check next, count is the current itteration or how many have been found Array lookAround( Array resultArray, Array gameArray){ // get char of first point from gameArray Char c = gameArray[resultArray[0].y][resultArray[0].x]; // this is the char we are looking for // grab last point in the array Point lastPoint = resultArray[resultArray.length - 1]; // try get second last point in array or null Point secondLastPoint = resultArray.length &lt;= 1 ? null : resultArray[resultArray.length - 2]; // now we find all the points we need to check Point up = { x:lastPoint.x , y:lastPoint.y - 1 ) Point down = { x:lastPoint.x , y:lastPoint.y + 1 ) Point left = { x:lastPoint.x - 1 , y:lastPoint.y ) Point right = { x:lastPoint.x + 1 , y:lastPoint.y ) Array pointsToCheck = [up,down,left,right]; foreach(Point p in pointsToCheck){ // check each of these points characters. // we have to check bounds first // the validatePoint method will help us if(validatePoint(p, secondLastPoint)){ // grab the character that we want to check now Char t = gameArray[p.y][p.x]; // now we finally check for the character match if( c == t ){ // if we are here then we have another match // lets make a copy of the result array and append the new point Array newResult = resultArray.copy(); newResult.push(p); // if we have enough points we can return the result as final. if( newResult.length &gt;= required){ return newResult; }else{ return lookAround( newResult, gameArray); } } } } // if the code reaches this point then we havn't been able to find a sequence for this point... lets return null. return null; } </code></pre> <p>This method is utility method to make sure our point is valid, i.e inside array bounds and also not the last point we checked... note that if we need to check more than 3 points we should pass and array of points (resultArray) and check them all instead of just last. // this method will check if the point is within the game array and also check that it is not the same as the last point that we have already checked.</p> <pre><code>Boolean validatePoint( Point p, Point last, Array gameArray){ //check array bounds if(p.x &lt; 0 || p.y &lt; 0 || p.x &gt;= gameArray[0].length || p.y &gt;= gameArray.length){ return false; } // check if the point is the same as the last one if(p.x == last.x &amp;&amp; p.y == last.y){ return false; } // if we got here then the point is valid return true; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload