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