Hey, I was creating a replication of a classic game called Minesweeper.
I’m using Java 8 and Stream class to find the number of cells with mine around a cell.
Here’s a bit of the code :-
public void reveal() {
this.revealed = true;
// Reveal current cell
removeAll();
this.value = (hasMine) ? Res.MINE_ICON : Res.EMPTY_LABEL;
add(this.value);
setBackground(Color.WHITE);
repaint();
revalidate();
if(hasMine(this)) return; // Don't need to proceed since you clicked on a mine!
// Reveal neighbors
final List<Object> neighbors = Arrays.asList(grid.getNeighborsAt(row,col));
neighbors.forEach(obj -> {
CellPanel cell = (CellPanel) obj;
if(!hasMine(cell) && !cell.hasRevealed())
cell.reveal();
});
Stream<Object> mineCells = neighbors.stream().filter(CellPanel::hasMine);
int totalMinesNearby = (int) (mineCells.count());
System.out.println(totalMinesNearby + " mines nearby " + this);
System.out.println(mineCells + " nearby " + this);
setValue(totalMinesNearby);
}
I tried that hasMine with the inverse (Exclamation mark) and it worked out just fine.
May I know what’s the problem?