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

• December 26th, 2012, 06:49 PM
jtmnf
[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

Code :

```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; }```
• December 26th, 2012, 06:54 PM
Norm
Re: [Help]
Quote:

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?
• December 26th, 2012, 06:57 PM
jtmnf
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...
• December 26th, 2012, 06:58 PM
jtmnf
Re: [Help]
This situation:
Attachment 1652

The green rectangles...
• December 26th, 2012, 07:00 PM
Norm
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?
• December 26th, 2012, 07:02 PM
jtmnf
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!
• December 26th, 2012, 07:07 PM
Norm
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
• December 26th, 2012, 07:10 PM
jtmnf
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!
• December 26th, 2012, 07:14 PM
Norm
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.
• December 26th, 2012, 07:16 PM
jtmnf
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...
• December 26th, 2012, 07:21 PM
Norm
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.
• December 26th, 2012, 07:23 PM
jtmnf
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...
• December 26th, 2012, 07:39 PM
Norm
Re: [Help]
Quote:

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.
• December 26th, 2012, 07:43 PM
jtmnf
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...
• December 26th, 2012, 07:48 PM
Norm
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.
• December 26th, 2012, 07:52 PM
jtmnf
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...
• December 26th, 2012, 07:57 PM
Norm
Re: [Help]
Quote:

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.

Quote:

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?
• December 26th, 2012, 07:59 PM
jtmnf
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]

This is the "solution"... I know it!
• December 26th, 2012, 08:02 PM
Norm
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.
• December 26th, 2012, 08:47 PM
copeg
Re: [Help]
This thread has been cross posted here:

Java Programming Forums Cross Posting Rules

The Problems With Cross Posting

• December 26th, 2012, 09:49 PM
jtmnf
Re: [Help]
3 hours later...

Code :

``` 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!