user3020461
user3020461

Reputation:

How to check for horizontal sets of 3 in 2D array?

I'm currently working on a project for a class, and I've been stuck here for the longest time. What I basically have to do, is check for horizontal sets of 3 identical char characters in a 2D 8x8 array. If there is a set of 3 (for example & & &), it should remove them all, and drop items from above down, and fill the empty slots on the very top row of the array with random characters from a a separate array that I have made. The main thing I don't understand is how to check for identical sets of 3 characters horizontally in this 2D array.

Any help would be much appreciated guys.

Upvotes: 2

Views: 2711

Answers (3)

Goran
Goran

Reputation: 675

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.

This is all psudocode so you will have to adapt to what ever language you are using.

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

Now for the look-around method:

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

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.

Boolean validatePoint( Point p, Point last, Array gameArray){

    //check array bounds
    if(p.x < 0 || p.y < 0 || p.x >= gameArray[0].length || p.y >= gameArray.length){
        return false;
    }

    // check if the point is the same as the last one
    if(p.x == last.x && p.y == last.y){
        return false;
    }

    // if we got here then the point is valid
    return true;

}

Upvotes: 0

rmunshi
rmunshi

Reputation: 26

Assume variable i represent the index of the particular row, eg. for third row, index is 2 and for fifth row, index is 4. So for your matrix[8][8],

for (int j = 0; j < 8; j++) {
if (matrix[i][j-1] == matrix[i][j] && matrix[i][j] == matrix[i][j+1]) {
            // do your stuff
        }
}

Update your i to second row index and check again.

Upvotes: 0

aga
aga

Reputation: 29416

Basically, you need to loop through every row in your matrix, from the element with index equal to 1 to the previous to the last element and check if the current element is equal to its previous and next elements:

for (int i = 0; i < matrix.length; i++) {
    for (int j = 1; j < matrix[i].length - 1; j++) {
        if (matrix[i][j-1] == matrix[i][j] && matrix[i][j] == matrix[i][j+1]) {
            // do something here
        }
    }
}

Upvotes: 2

Related Questions