#How to tell if DS Grid has same value a certain amount of times in a row?

1 messages · Page 1 of 1 (latest)

verbal grotto
#

How do I detect if a DS Grid has the same value 4 consecutive times in a row, both horizontally and vertically?

And if it does, I want to delete these 4 values (reset them back to 0).

I know using for loops is involved, having to loop through the grid multiple times, but I’m having a hard time figuring out how to actually accomplish this.

#

Basically, what I have right now is a game of Tetris, where it clears out one line at a time at the bottom, and I want it to be like Dr. Mario instead, where it clears out 4 in a row of the same block color anywhere in the grid, both horizontally and vertically.

tame quest
#
clear_row = []
clear_column = []
//Horizontal Check
for var(j = grid_height-1; j > 0; j--){ //go from bottom up, so you're not checking a bunch of empty rows and can quit faster
  for (var i = 0; i < grid_width-3; i++){ //we end s
   if grid[# i,j] != 0 //empty cell, idk how you store that
   and grid[# i,j] == grid[# i+1, j]
   and grid[# i,j] == grid[# i+2, j]
   and grid[# i,j] == grid[# i+3, j]{ //check 4 cells at a time
      array_push(clear_row,j)
      break;  
    }
  }
}
//Vertical Check
for (var i = 0; i < grid_width; i++){
  for (var j = grid_height-1; j > 2; j--){
     if grid[# i,j] != 0 
     and grid[# i,j] == grid[# i, j-1]
     and grid[# i,j] == grid[# i, j-2] 
     and grid[# i,j] == grid[# i, j-3]
        array_push(clear_column,j)
        break;
  }
}


if array_length(clear_row)>0 or array_length(clear_column)>0{
  //we found a row or column that can be cleared
}

idea is this, read through it and make sure you understand cuz there might be bugs

#

can't guarantee code runs correctly first try

#

since this code is running 4 for loops, it's not the lightest of operations.

it's not like stupidly heavy cuz your grids are relatively small, but
don't need to run it all the time. Just run it once when the gameboard changes aka right before you spawn a new block

verbal grotto
#

Thank you, I get the basic idea

#

Where do I put the code for clearing the row/column? Does it go inside another for loop?

tame quest
#

in here

clear_row and clear_column is the row/collumns that need clearing

#

i just modified the code

#

so that it can detect if multiple rows/columns are fulfilled

#

it does use an array, since it doesnt look like you're using GM1.4

verbal grotto
#

Now I just need to make it so that if a row/column of 4 is cleared, the blocks on top fall downwards

#

To prevent blocks from staying mid air

#

So do you know how I would check if a value in the DS_Grid is surrounded by 0 in all 4 cardinal directions? (up,down,left,right)

#

And if it is, then lower it down (basically adding gravity)

tame quest
#

same idea as finding a row of 4

#
for var(j = 0; j < grid_height-1; j++){ //go from bottom up, so you're not checking a bunch of empty rows and can quit faster
  for (var i = 0; i < grid_width; i++){
       if grid[# i,j]!=0{
          if ( (i-1 > 0 and grid[# i-1,j] = 0) or (i-1 <= 0) ) //being on the edge does not count as being supported
          and ( (i+1 < grid_width and grid[# i+1, j] = 0) or (i+1 >= grid_width) )
          and (j-1 > 0 and grid[# i,j-1] = 0 or (j-1 <= 0)
          and (j+1 < grid_height and grid[# i, j+1] = 0) //but if you're on the bottom edge, you are supported{
              grid[# i, j+1] = grid[# i, j]
              grid[# i, j] = 0
          }  
            
       }
    }
  }
}
#

the nice thing is that since we're scanning downwards
if the block falls 1, and is still unsupported, it'll be detected and fall again

#

also that will be the last piece of code i write for you. hopefully that's enough for you to get the idea and move forward with your own implementations