I'm making a Life game and I made this method to find nearby neighbours
private int getNeighbours(LifeBoard board, int row, int col){
if(board.get(row+1, col)){
neighbours++;
}
if(board.get(row-1, col)){
neighbours++;
}
if(board.get(row, col+1)){
neighbours++;
}
if(board.get(row, col-1)){
neighbours++;
}
if(board.get(row+1, col+1)){
neighbours++;
}
if(board.get(row-1, col-1)){
neighbours++;
}
if(board.get(row+1, col-1)){
neighbours++;
}
if(board.get(row-1, col+1)){
neighbours++;
}
return neighbours;
}
I feel as if it's horribly coded and cringe at it, therefore my question is.. Is there a way to make this better? Right now, it "sort-of" works but I'm thinking if I could do this with a loop instead.
Thanks.