我试图以最有效的方式将至少3个对象相互匹配。我一直得到一个空指针错误。我尝试了一些不同的方法,似乎无法弄明白。任何帮助将不胜感激。
protected void boardFill(ArrayList<ArrayList<T>> twoDList) {
for (int x = 0; x < COL; x++) {
twoDList.add(new ArrayList<T>());
for(int y = 0; y < ROW; y++) {
twoDList.get(x).add(gems.newGem());
}
}
for (int i = 0; i < COL; i++) {
for (int j = 0; j < ROW; j++) {
getObject(i, j, twoDList.get(i).get(j));
}
}
}
/*
* move pieces down after being removed
*/
public void drop() {
}
/*
* check if position is legal to move/swap
*/
public boolean legal(int row, int col) {
return false;
}
/*
* check if pieces are matching
*/
public boolean matching(int r, int c) {
boolean flag = false;
return flag;
}
public void getObject(int i, int j, T type) {
if (twoDList.get(i).get(j) == type) {
coordinates.add(twoDList.get(i).get(j));
if (i-1 >= 0) getObject(i - 1, j, type);
if (i+1 < ROW) getObject(i + 1, j, type);
if (j-1 >= 0) getObject(i, j - 1, type);
if (j+1 < COL) getObject(i, j + 1, type);
}
}