I have a 2D ArrayList filled with random characters and given column and row index I need to check its surroundings for a chain of matches (need to check the index before and after, and list before and after in same index for matching characters). Then repeat the process to the indexes being checked. If chars are equal add to the counter. If counter is equal or more than threshold, then return true. (Still unsure about the remove, might just "flag" them for removal in another method).
static ArrayList<ArrayList<Character>> board = new ArrayList<ArrayList<Character>>();
public static boolean blockMatch(int rIndex, int cIndex, int treshold) {
int count = 1;
int curR = rIndex;
int curC = cIndex;
char ch = board.get(curR).get(curC);
char chUp = board.get(curR + 1).get(curC);
char chDown = board.get(curR - 1).get(curC);
// char chRight = board.get(curR).get(curC + 1);
// char chLeft = board.get(curR).get(curC - 1);
if (ch == chUp) {
count++;
board.remove(board.get(curR).get(curC));
curR++;
System.out.println(count);
System.out.println(board);
blockMatch(curR, curC, treshold);
if (count >= treshold) {
return true;
}
}
if (ch == chDown) {
count++;
board.remove(board.get(curR).get(curC));
curR--;
System.out.println(count);
System.out.println(board);
blockMatch(curR, curC, treshold);
if (count >= treshold) {
return true;
}
}
return false;
}
This is by no means close to finished because I still need to add a lot more if statements 2 more for the left and right sides,and incase the surroundings are out of bounds and also a hasNext(). Problem is I have too many if-statements and feel like this could be simplified?
Aucun commentaire:
Enregistrer un commentaire