Matching algorithm almost success

I was working on my game as an inspiration from the game “Candy Crush”. I did a check method to find all the 3 or more matches on the rows and column. I thought I did it but I saw a match that sit at the end of the row or column was not added to the matches that it found. Someone could help? I’m stuck forever ???

This is the algorithm I used to check all matches:

private void check() {
		Map<Candy,ArrayList<Tile>> matches = new HashMap<>();
		ArrayList<Tile> match = new ArrayList<>();

		// Find all horizontal matches
		for(int i=0;i<row;i++) {
			int j=0;
			match.clear();
			while(j < 8) {
				Tile current = tiles[i][j];
				Candy candy = current.getCandy();
				
				if(match.size() == 0 || match.get(0).getCandy().equals(candy)) {
					match.add(current);
				}else if(!match.get(0).getCandy().getType().equals(candy.getType())) {
					if(match.size() >= 3) {
						matches.put(candy, match);
						System.out.println(match);
					}
					match.clear();
					match.add(current);
				}
				j++;
			}
                        // And I thought this could solve that problem but it's not
			if(j == 7 && match.size() >= 3) {
				match.add(tiles[i][j]);
				matches.put(tiles[i][j].getCandy(), match);
				//System.out.println(match);
			}
		}

		// Find all vertical matches
		for(int j=0;j<col;j++) {
			int i=0;
			match.clear();
			while(i < 8) {
				Tile current = tiles[i][j];
				Candy candy = current.getCandy();
				
				if(match.size() == 0 || match.get(0).getCandy().equals(candy)) {
					match.add(current);
				}else if(!match.get(0).getCandy().getType().equals(candy.getType())) {
					if(match.size() >= 3) {
						matches.put(candy, match);
						//System.out.println(match);
					}
					match.clear();
					match.add(current);
				}
				i++;
			}
                        // Here too, but for the vertical matches
			if(i == 7 && match.size() >= 3) {
				matches.put(tiles[i][j].getCandy(), match);
				//System.out.println(match);
			}
		}
	}

You can see more of my code at my GitHub here


for y 0 to end
	for x 0 to end
	if (x, y + 1) == (x, y) &&
	   (x, y + 2) == (x, y)
		Match Y ([x, y], [x, y + 1], [x, y + 2])
		
	if (x + 1, y) == (x, y) &&
	   (x + 2, y) == (x, y)
		Match X ([x, y], [x + 1, y], [x + 2, y])
	//don't forget to check bounds on +1 +2 )
	//	if (y >= end || x >= end) return -1

but what if you have:


#####
#####
#####
XXXX#

result will be:


#####
#####
#####
MMMX#

i think you can fix it its little more code ^^

also + one more option


#####
X####
X####
XXX##

But that algorithm of yours only consider 3 match only. I was going to consider 3-match, 4-match and so on.

Anyway, my code is working though. It’s just not detecting match when one of the match piece lies on the end of the row/column. I don’t know what’s wrong :-. Maybe it has to do with that match.clear() part.

Hi,

just a random tought: Do your row and col variables have the right value or are they too low by one?
Given your description it looks like your for-loops end too early.

Maybe try changing

i<row;

in

for(int i=0;i<row;i++) {

into

i<=row;

I’m sure I did it right and also I did try that to solve that but it give me array out of bound exception. I have no clue.

Never mind. I have found the solution. I just need to check the matches length at the last of the while loop instead of the if else.

So, it will look like this


private void check() {
		Map<Candy,ArrayList<Tile>> matches = new HashMap<>();
		ArrayList<Tile> match = new ArrayList<>();

		// Find all horizontal matches
		for(int i=0;i<row;i++) {
			int j=0;
			match.clear();
			while(j < 8) {
				Tile current = tiles[i][j];
				Candy candy = current.getCandy();
				
				if(match.size() == 0 || match.get(0).getCandy().equals(candy)) {
					match.add(current);
				}else if(!match.get(0).getCandy().getType().equals(candy.getType())) {
					match.clear();
					match.add(current);
				}
				
				if(match.size() >= 3) {
					matches.put(candy, match);
					System.out.println(match);
				}
				
				j++;
			}
		}

		// Find all vertical matches
		for(int j=0;j<col;j++) {
			int i=0;
			match.clear();
			while(i < 8) {
				Tile current = tiles[i][j];
				Candy candy = current.getCandy();
				
				if(match.size() == 0 || match.get(0).getCandy().equals(candy)) {
					match.add(current);
				}else if(!match.get(0).getCandy().getType().equals(candy.getType())) {
					match.clear();
					match.add(current);
				}
				
				if(match.size() >= 3) {
					matches.put(candy, match);
					//System.out.println(match);
				}
				
				i++;
			}
		}
	}

Maybe this help)
some test data


=1111=
11=11=
1==122
111222
1111=2


=zzzz=
x1=z1=
x==z2y
xxxyyy
xxxx=y

1 x = 9
2 y = 5
1 z = 6

maybe mark visited cells to skip duplicates
horizontal and vertical match may have same cells )


--i j
++y x

--while(j < 8)
++while(j < col) 
//or
++for(int j=0;j<col;j++)