Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Searching a 2d Array for lines of 3

  1. #1
    Member
    Join Date
    Jan 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default 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?

  3. #3
    Member
    Join Date
    Jan 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default 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.

  5. #5
    Member
    Join Date
    Jan 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. Searching for object variables inside a 1-D array?
    By Scorks in forum Collections and Generics
    Replies: 2
    Last Post: September 6th, 2013, 11:36 PM
  2. [SOLVED] Help with searching an array for a String (name) using a for loop
    By seph in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 23rd, 2013, 09:01 PM
  3. Searching and displaying results in a multi-dimensional array.
    By wfalcon2012 in forum Collections and Generics
    Replies: 2
    Last Post: February 18th, 2012, 08:06 PM
  4. Your input with written code (Sorting and searching within array)
    By GalBenH in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 11th, 2012, 09:19 AM

Tags for this Thread