This site requires JavaScript, please enable it in your browser!
Greenfoot back
MonkeiProtecc
MonkeiProtecc wrote ...

2020/9/4

How to use getNeighbours() to check if any of the neighbours are from a specific class

MonkeiProtecc MonkeiProtecc

2020/9/4

#
So like I said above. I want to use getNeighbours(1,true,Black.class) to check if any of the neighbours are from White.class. The objects in my Scenario are contained in cells.
RcCookie RcCookie

2020/9/4

#
getNeighbours() returns a list of actors. You don’t need to know how lists work or how to access them, simply remember that you can get the length of basically any list with the method .size(). In this case, you want to check weather getNeighbours returns at least one actor, so use: I
boolean hasNeighbours = getNeighbours(1, true, Black.class).size() > 0;
danpost danpost

2020/9/4

#
MonkeiProtecc wrote...
So like I said above. I want to use getNeighbours(1,true,Black.class) to check if any of the neighbours are from White.class. The objects in my Scenario are contained in cells.
Your example code does not match with your statement (Black <> White). Nonetheless, the following should help:
if ( ! getNeighbours(1, true, White.class).isEmpty() )
MonkeiProtecc MonkeiProtecc

2020/9/4

#
danpost wrote...
MonkeiProtecc wrote...
So like I said above. I want to use getNeighbours(1,true,Black.class) to check if any of the neighbours are from White.class. The objects in my Scenario are contained in cells.
Your example code does not match with your statement (Black <> White). Nonetheless, the following should help:
if ( ! getNeighbours(1, true, White.class).isEmpty() )
so what does this exactly do? So i can understand it :). Also what I meant was i want to check all the 8 neighbours of a black cell. And for every white neighbour within the 8 cells, a variable (i) should increase by 1.
MonkeiProtecc MonkeiProtecc

2020/9/4

#
RcCookie wrote...
getNeighbours() returns a list of actors. You don’t need to know how lists work or how to access them, simply remember that you can get the length of basically any list with the method .size(). In this case, you want to check weather getNeighbours returns at least one actor, so use: I
boolean hasNeighbours = getNeighbours(1, true, Black.class).size() > 0;
every cell is an actor in my scenario, it’s either White.class or Black.class. So I don’t think that would work for me.
danpost danpost

2020/9/4

#
MonkeiProtecc wrote...
<< Quote Omitted >> so what does this exactly do? So i can understand it :). Also what I meant was i want to check all the 8 neighbours of a black cell. And for every white neighbour within the 8 cells, a variable (i) should increase by 1.
Well, your title suggested you wanted to know if any White objects were neighbouriing a Black (order of colors not being pertinent). However, now you specify you need a count of them. For number of neighbors:
int neighborCount = getNeighbours(1, true, White.class).size();
This will give the current number of White neighbors.
You need to login to post a reply.