The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

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?

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?

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.

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:

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.

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.