Results 1 to 4 of 4
  1. #1
    prog_phil is offline Member
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default Checking a row for sequential matches

    Hello all,

    Firstly, thank you for these great forums; Google has often led me here when I'm searching for a solution.

    I'm playing around trying to make a very primitive game similar to bejewelled, but have become stuck when trying to find matches that are greater than 3. To clarify, my code works when there are 3 sequential matches, but not more. So if there are 4 matches in a row/column, the first 3 are taken. This isn't so much a case of "fix my code for me" but rather some guidance with the logic.

    Java Code:
    private Chain getLinks(ArrayList<Gem> gems) { //this contains a single row/column of gems 
    	Chain c = new Chain(); //class that holds the length, start and end of a sequence
    	c.setLen(1);
    	
    	for (int i = 0; i < gems.size() - 1; i++) {
    		Gem cur = gems.get(i);
    		Gem next = gems.get(i + 1);
    		if (cur == next) { 
    			c.increment(); // (just length++ in the chain class)
    			if ((c.getLen() >= 3))  { // *** PROBLEM HERE, I IMAGINE ***
    				c.end(i + 1);
    				break; // have to break, otherwise programme gets stuck/crashes
    			}	
    		} else { //chain broken, start again
    			c.setStart(i + 1);
    			c.setLen(1);
    		}
    	}//end for
    	return c;
    }
    Where I've marked the code is obviously where the issue lies, but I've spent the past two hours trying to think of a different way that will allow me access to more than 3 gems. I know I'm over thinking it so hopefully one of you smart chaps can smack me around the face and point me in the right direction.

    thank you for you time.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,801
    Rep Power
    25

    Default Re: Checking a row for sequential matches

    Normally the equals() method is used to compare objects. == is used with object references to see if they refer to the same object, not to compare the contents of two different objects.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    prog_phil is offline Member
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default Re: Checking a row for sequential matches

    Quote Originally Posted by Norm View Post
    Normally the equals() method is used to compare objects. == is used with object references to see if they refer to the same object, not to compare the contents of two different objects.
    You're absolutely right. However, I still have the same issue of not being able to check more than 3 gems in a row/column. Say the gems are these ascii characters &$$$$"%^, my current algorithm only matches the first three &***$"%^, instead of &****"%^ (The stars in this instance represent gems removed).

    Any tips on how I can adjust my algorithm in order to match the desired functionality would be appreciated.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,801
    Rep Power
    25

    Default Re: Checking a row for sequential matches

    Can you post a small, complete program that compiles, executes and shows the problem?

    The posted code stops the search based on a count. Is there a better condition to use for stopping the count? Like: next item not = last item
    Last edited by Norm; 01-02-2013 at 09:49 PM.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. JFileChooser - sequential files treatment
    By althair in forum AWT / Swing
    Replies: 2
    Last Post: 01-03-2010, 04:55 PM
  2. Realize default value/sequential number in reporting tool
    By freezea in forum Reviews / Advertising
    Replies: 0
    Last Post: 04-21-2009, 08:08 AM
  3. Realize default value/sequential number in reporting tool
    By freezea in forum Reviews / Advertising
    Replies: 0
    Last Post: 04-13-2009, 05:25 PM
  4. Replies: 4
    Last Post: 12-04-2008, 12:07 AM
  5. Problem with Sequential File and Arrays
    By rhivka in forum New To Java
    Replies: 8
    Last Post: 07-30-2007, 10:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •