# Searching a 2d Array for lines of 3

• January 20th, 2014, 12:32 AM
Lurie1
Searching a 2d Array for lines of 3
At this point I have a tile based game. The tiles are stored in a 6 by 12 array labelled 1-10. I need to search the board to find if there are 3 or more in a row or column after the player makes a switch, btw switches can only happen on the x axis and only with other tiles. I have the event listener all set but to search the board but after an event happens all I have is about 50 lines of if statements after two for loops. Each statement compares the x and y to x+1 and x+2 or x1 and x-2 and so on.

I have searched google and do know how to search 2d arrays but only for individual pieces, is there a way to expedite and unclutter the process of searching for rows or columns of 3+ of the same tile??

Also as a second question a move is made the tiles in row or column that have 3+ consecutive tiles of the same value will disappear and the remaining tiles will move up. again i do this with lots of if statements first changing the tiles found in 3 ore more to 0 my null number then replacing them with the next tile in the row the is not 0. Is there a better way to do either of the above processes that does not involve the millions of if statements i am currently using?
• January 20th, 2014, 04:36 AM
GregBrannon
Re: Searching a 2d Array for lines of 3
Let me see if I understand:

There are 10 different tiles, labeled 1 through 10.
A 6-by-12 array (72 elements) is completely filled with the 10 different tiles.

Questions:
Are the tiles in the array objects of a Tile (or another) class or simply the labels "1" through "10"? What are tiles? What attributes do they have? If a class, please post the Tile class, or whatever it's called.

What's the proportion of each of the 10 tiles in the 72 elements? Approximately equal?

Explain this better: "switches can only happen on the x axis and only with other tiles" What else besides tiles is on the x-axis (in a row) that CAN'T be switched?

Explain this better: "the tiles in row or column that have 3+ consecutive tiles of the same value will disappear and the remaining tiles will move up" Move up from where? Where do the fill-in tiles come from when a column of 3+ are removed? What fills in behind the tiles that move in or up from the edges?
• January 20th, 2014, 10:36 AM
Lurie1
Re: Searching a 2d Array for lines of 3
The tiles are just the data points in the array I display them according the the number stored in the array 1-10. they are 45 by 45 pixel squares of varying color depending on the number in the array 1-10. they do not have any attributes and are just 1/72 of the playing board.

Yes as the board starts out each one is approximately = though the array is randomly populated so sometimes a specific tile is more common.

To clarify switching:

5 1 5 6 5 4 1 2 6 10 4 10
5 5 1 4 5 6 5 6 5 3 3 1
4 2 5 6 1 5 1 3 6 5 1 4
2 4 1 5 1 8 9 4 1 6 2 5
1 6 1 2 6 1 5 2 3 5 10 3
4 5 3 5 5 6 5 4 1 5 5 4

this is a board that was generated. a switch can only happen on the x axis ie. the 1 at point 1,2 can be switched with the 5 at 2,2 but not with anything else because of its location. the 5 at 2,2 can be switched with the 2 at 3,2 or the 1 at 1,2. so there are 5 possible switches that can occur out of 6 data points.

if a player makes a switch on the above board the logical one would be at 3,3 with the 5 and the 1, that makes both the 5's and the 1's are in a row of 3:

5 1 5 6 5 4 1 2 6 10 4 10
5 5 5 4 5 6 5 6 5 3 3 1
4 2 1 6 1 5 1 3 6 5 1 4
2 4 1 5 1 8 9 4 1 6 2 5
1 6 1 2 6 1 5 2 3 5 10 3
4 5 3 5 5 6 5 4 1 5 5 4

then the 5's and 1''s need to be deleted and the board needs to move up ie the row of 5's and 1's would be replaced with 0's my null piece:

5 1 5 6 5 4 1 2 6 10 4 10
4 5 6 5 6 5 3 3 1 0 0 0
4 2 6 1 5 1 3 6 5 1 4 0
2 4 5 1 8 9 4 1 6 2 5 0
1 6 2 6 1 5 2 3 5 10 3
4 5 3 5 5 6 5 4 1 5 5 4

nothing fills in behind them the object is to clear as many pieces on a board as possible no new piece is introduced after the initial population.
• January 20th, 2014, 12:40 PM
GregBrannon
Re: Searching a 2d Array for lines of 3
Thank you for explaining.

You can continue calling numerals 'tiles' in an array with large chains of 'if' logic to determine the state of the board. If that's working, maybe it's okay.

Or, you could be a bit more clever and use classes like LurielGame, Tile, and Board, each with appropriate attributes that define, and methods that monitor, the status of the game board and update the game's state with each move. I've done something similar, I can visualize how what I've done might be modified for your game, so I know it's possible and more efficient than miles of if statements.

The approach I'm suggesting is significantly different than your current design, requiring the use of OOP principles rather than pure brute force logic, so if you're not comfortable taking that on or you don't have the time to pursue a major shift in design, save it for another time.
• January 20th, 2014, 01:00 PM
Lurie1
Re: Searching a 2d Array for lines of 3
I am interested if you could explain more i would appreciate it.

I do understand the principles but not really how they could be applied in this instance. also how would that do anything but spread out the miles of if statements. Sorry for the short post but i am interested in this and thinking about it so kinda distracted.