private void countMineNeighbors() {
for (int y = 0; y < SIDE; y++) {
for (int x = 0; x < SIDE; x++) {
GameObject gameObject = gameField[y][x];
if (!gameObject.isMine) {
gameObject.countMineNeighbors = Math.toIntExact(getNeighbors(gameObject).stream().filter(neighbor -> neighbor.isMine).count());
}
}
}
}
can anybody explain how countMineNeighbours method works??
Under discussion
Comments (1)
- Popular
- New
- Old
You must be signed in to leave a comment
luca
2 February 2021, 19:21
So from what i understand the first part goes through each object from your playing field
for (int y = 0; y < SIDE; y++) {
for (int x = 0; x < SIDE; x++)
GameObject gameObject = gameField[y][x];
then for each object that isn t a mine you set the int countMineNeighbors (gameObject.countMineNeighbors)
Math.toIntExact converts a long to a int you need this because the count() method returns a long.
Then the getNeighbors(gameObject) returns a Arraylist of object (all the neighbours of the object).
the stream() is used to process the arrayList, the filter() select the objects reference to put in the stream(A stream is a sequence of objects references). Then the count() returns a long with the number of item in the stream.
I m not really sure so here a link for stream
https://www.geeksforgeeks.org/stream-in-java/
0