How to traverse a two-dimensional array with a snake?

Hello everyone!
I have a square playing field created using a two-dimensional array of identical sprites (cells). I have little experience and I’ve been racking my brain for a long time about how I can go through the playing field from the cell the player touched to some final one (not important), but in order from left to right to the end of the row, going down to the next one, etc. That is, like a snake. The image will make it clearer.

1 Like

You can loop as many times as you need by checking if cell[row][column+1] exists, where row is the current row and column is the current column. If the cell doesn’t exist, then you reset column to 1 and increment row by 1. After incrementing row, check if the new row is larger than the grid, i.e. if row is larger than there are rows. If it is, then reset row to 1.

1 Like