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 21 of 21

Thread: [Help][SOLVED] - Catch sequence of 3 or more pieces of same color [BEJEWELED GAME]

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    11
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default [Help][SOLVED] - Catch sequence of 3 or more pieces of same color [BEJEWELED GAME]

    I need help... I'm doing Bejeweled in JAVA, but I need help to overpass a problem...

    When I start the board for the first time, I cannot have 3 or more pieces of the same color, but my function "verifica_inicio" it's not doing the work that it should have :S

    Can anyone help me?

    pos_x/pos_y is the position in the matrix
    fig_aleat is the random piece that is randomized at the moment

    public int verifica_inicio(int fig_aleat,int pos_x,int pos_y){
            if(pos_x<2 && pos_y<2){
                    return 1;
            }
     
            if(pos_x<2){
                    if((fig_aleat!=matriz[pos_x][pos_y-1] || fig_aleat!=matriz[pos_x][pos_y-2])){
                            return 1;
                    }
            }
     
            if (pos_y<2){
                    if(fig_aleat!=matriz[pos_x-1][pos_y] || fig_aleat!=matriz[pos_x-2][pos_y]){
                            return 1;
                    }
            }
     
            if(fig_aleat!=matriz[pos_x-1][pos_y] || fig_aleat!=matriz[pos_x-2][pos_y]){
                if((fig_aleat!=matriz[pos_x][pos_y-1] || fig_aleat!=matriz[pos_x][pos_y-2])){
                        return 1;
                }
            }
     
            return 0;
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: [Help]

    it's not doing the work that it should have
    Can you describe what the code should be doing and the steps it should take to do it?

    Why is this test done: pos_x<2?

    How are the colors of the pieces determined?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    jtmnf (December 26th, 2012)

  4. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    11
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: [Help]

    The code should see if there are 3 or more pieces in consecutive positions...
    The "aleat_fig" as the values 0 to 5 (6 pieces)!

    For example, I cannot have this combination:

    # EDIT -> it's nxn, not nxm, sorry :S #
    0 1 4 4 2 4
    0 3 5 4 5 3
    0 4 4 2 4 5
    2 3 4 1 3 5
    1 3 1 4 4 3
    4 5 2 3 4 5

    Because of the three 0's...

  5. #4
    Junior Member
    Join Date
    Dec 2012
    Posts
    11
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: [Help]

    This situation:
    Sem Título.jpg

    The green rectangles...

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: [Help]

    Does the code have to look through all rows and columns in a 5x6 grid looking for any sequence of 3 numbers in a row or column (not diagonal) with the same value?
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    jtmnf (December 26th, 2012)

  8. #6
    Junior Member
    Join Date
    Dec 2012
    Posts
    11
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: [Help]

    I messed up... It's a nxn grid, not nxm, sorry... But yes! If 3 or more numbers are the same, I have to change the 1st appearence of that number to other!

  9. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: [Help]

    The posted method has three arguments passed to it. What are they supposed to be used for?
    It returns a 0 or a 1. What do they indicate?

    A thought: check the rows one by one for 3 in a row and then the columns one by one for 3 in a row
    If you don't understand my answer, don't ignore it, ask a question.

  10. The Following User Says Thank You to Norm For This Useful Post:

    jtmnf (December 26th, 2012)

  11. #8
    Junior Member
    Join Date
    Dec 2012
    Posts
    11
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: [Help]

    pos_x/pos_y is the position in the matrix
    fig_aleat is the random piece that is randomized at the moment

    When returns 1, it indicates that there's no 3 or more pieces (numbers) in sequence!

  12. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: [Help]

    How is that part of the problem of searching through an nxn grid looking for 3 in a row?
    First look at the rows then look at the columns for 3 in a row.
    If you don't understand my answer, don't ignore it, ask a question.

  13. The Following User Says Thank You to Norm For This Useful Post:

    jtmnf (December 26th, 2012)

  14. #10
    Junior Member
    Join Date
    Dec 2012
    Posts
    11
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: [Help]

    I already do that, but I appears that at the end, there is, once more, a sequence of 3 or more pieces of the same color... :S I cannot do anything more, I exhausted...

  15. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: [Help]

    Take it one step at a time.
    How would the code look at the items in a row to see if there are three in a row?
    Use a piece of paper, write some numbers in a row and work through the logic using arrows drawn below the numbers for indexes as you move to the right in the row and compare the numbers.
    If you don't understand my answer, don't ignore it, ask a question.

  16. The Following User Says Thank You to Norm For This Useful Post:

    jtmnf (December 26th, 2012)

  17. #12
    Junior Member
    Join Date
    Dec 2012
    Posts
    11
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: [Help]

    I already do that, I swear... I don't understand what's missing :S I already have Array out of bounds a bunch of times...

  18. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: [Help]

    I already do that,
    I don't see how the posted method looks at the contents of a row and tests if there are 3 identical elements in sequence. It would need a loop to do that.

    Can you describe the logic to look at a row of numbers and check if there are 3 in a row the same?

    Take the very simple case of a row with three numbers. How would you check if they are the same?
    Use two indexes and a counter.
    If you don't understand my answer, don't ignore it, ask a question.

  19. The Following User Says Thank You to Norm For This Useful Post:

    jtmnf (December 26th, 2012)

  20. #14
    Junior Member
    Join Date
    Dec 2012
    Posts
    11
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: [Help]

    matriz[pos_x][pos_y] == matriz[pos_x][pos_y-1] && matriz[pos_x][pos_y] == matriz[pos_x][pos_y-2]
    matriz[pos_x][pos_y] == matriz[pos_x][pos_y+1] && matriz[pos_x][pos_y] == matriz[pos_x][pos_y+2]
    matriz[pos_x][pos_y] == matriz[pos_x+1][pos_y] && matriz[pos_x][pos_y] == matriz[pos_x+2][pos_y]
    matriz[pos_x][pos_y] == matriz[pos_x-1][pos_y] && matriz[pos_x][pos_y] == matriz[pos_x-2][pos_y]

    These are all the conditions... If one of those occurs, I need to change the matriz[pos_x][pos_y] to another number...

    But I don't know how :S I tried... And tried...

  21. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: [Help]

    Can you describe the logic for testing? Don't write any code until you have the logic.

    My question was for searching one row. That would be a one dimensional array. The code you posted used a two dimensional array and looked at numbers on more than one row.
    In the code you posted, where is the counter and where are the two indexes?
    The counter counts the number of same numbers found so far.
    One index points to the first number in the sequence of numbers.
    The other index moves to the right so the next number can be checked for a match to the one at the first index.
    If you don't understand my answer, don't ignore it, ask a question.

  22. The Following User Says Thank You to Norm For This Useful Post:

    jtmnf (December 26th, 2012)

  23. #16
    Junior Member
    Join Date
    Dec 2012
    Posts
    11
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: [Help]

    There aren't counters...

    My thought: search in the rows... Search for a sequence of 3 or more numbers... If it find it, change the first number to another... Now, it returns to search in the rows (from the beginning)... If its ok, go to the columns... Do the same!

    I tried to do this more than once... I have no results...

  24. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: [Help]

    There aren't counters...
    My thought: search in the rows... Search for a sequence of 3
    A counter is how you know there are 3 in a row. You count them as you see them:
    first there is one, then if the next number matches the first there are two
    and then if the next number matches the first there are three and the search is done. The first index points to the first of the three numbers.
    When the numbers don't match, the first index is set to that number, the count is set to 1 and the search starts over.

    Search for a sequence of 3
    This is the problem: How to find the sequence of 3. What are the steps the program must do to find that sequence of 3?
    If you don't understand my answer, don't ignore it, ask a question.

  25. The Following User Says Thank You to Norm For This Useful Post:

    jtmnf (December 26th, 2012)

  26. #18
    Junior Member
    Join Date
    Dec 2012
    Posts
    11
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: [Help]

    I can answer with this:

    matriz[pos_x][pos_y] == matriz[pos_x][pos_y-1] && matriz[pos_x][pos_y] == matriz[pos_x][pos_y-2]
    matriz[pos_x][pos_y] == matriz[pos_x][pos_y+1] && matriz[pos_x][pos_y] == matriz[pos_x][pos_y+2]
    matriz[pos_x][pos_y] == matriz[pos_x+1][pos_y] && matriz[pos_x][pos_y] == matriz[pos_x+2][pos_y]
    matriz[pos_x][pos_y] == matriz[pos_x-1][pos_y] && matriz[pos_x][pos_y] == matriz[pos_x-2][pos_y]

    This is the "solution"... I know it!

  27. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: [Help]

    Try working on a solution for a single row, not a 2 dimensional array. When that works, then you can easily modify the logic to look at all the rows and the columns.

    I gave you most of the logic in post #17.


    Are we working on the same problem? I'm trying to find 3 in a row anywhere in the array and do not need pos_x and pos_y passed to the method that is doing the search.
    If you don't understand my answer, don't ignore it, ask a question.

  28. The Following User Says Thank You to Norm For This Useful Post:

    jtmnf (December 26th, 2012)

  29. #20
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: [Help]

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/67041-help.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  30. The Following 2 Users Say Thank You to copeg For This Useful Post:

    jtmnf (December 26th, 2012), Norm (December 26th, 2012)

  31. #21
    Junior Member
    Join Date
    Dec 2012
    Posts
    11
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: [Help]

    3 hours later...

      public void verifica_inicio(int[][] matriz, int n){
    		  	int i;
    			int j;
    			int temp;
    			int temp_2;
    			int temp_3 = n;
    			Random rand = new Random();
     
    			for(i = 0;i<n;i++){
    				for(j = 0;j<n;j++){
    					if (i < n-2){
    						System.out.print(i +" + "+ j+"\n");
    						if (matriz[i][j] == matriz[i+1][j] && matriz[i][j] == matriz[i+2][j]){
    							temp = matriz[i][j];
    							do{
    								temp_2=rand.nextInt(6);
    							}while(temp_2 == temp);
    							matriz[i][j] = temp_2;
    							try{
    								verifica_inicio(matriz,temp_3);
    							} catch (ArrayIndexOutOfBoundsException e){
    								System.out.println("ERROR: Array Index Out Of Bounds -> (" + i +" ; "+ j+")");
    							}
    						}	
    					}
     
    					if (j < n-2){
    						if (matriz[i][j] == matriz[i][j+1] && matriz[i][j] == matriz[i][j+2]){
    							temp = matriz[i][j];
    							do{
    								temp_2=rand.nextInt(6);
    							}while(temp_2 == temp);
    							matriz[i][j] = temp_2;
    							try{
    								verifica_inicio(matriz,temp_3);
    							} catch (ArrayIndexOutOfBoundsException e){
    								System.out.println("ERROR: Array Index Out Of Bounds -> (" + i +" ; "+ j+")");
    							}
    						}	
    					}
    				}
    			}
    		}


    Hard work, but I did it


    Thank you!