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

2025/4/19

problem with neighbor count

ttt ttt

2025/4/19

#
The Actor method getNeighbours() gives strange results in certain situations.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import greenfoot.*; 
 
public class DotWorld extends World {
 
    public DotWorld(){   
        super( 10, 10, 60 );
     
        for( int y=0; y<getHeight(); y++ ) {
            for( int x=0; x<getWidth(); x++ ) {
                addObject( new Dot(), x, y );
            }
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import greenfoot.*; 
 
public class Dot extends Actor {
     
    public Dot() {
        setImage( "yellow-draught.png" );  // results depend on the image (!)
    }
     
    public void act() {
        int n = getNeighbours( 1, false, null ).size();  // wrong count for certain combinations of world.cellsize and size of actor image
        // int n = getObjectsInRange( 1, null ).size();  // works
        getWorld().showText( Integer.toString( n ), getX(), getY() );
    }
 
}
danpost danpost

2025/4/20

#
ttt wrote...
The Actor method getNeighbours() gives strange results in certain situations. << Code Omitted >>
I do not seem to be getting any strange results. Maybe you can give examples. Like, for what image do you get for what strange results (that is, give image AND results). The results can be given as in the following: For image "yellow-draught.png", I got 2 in the corners, 3 along the edges and 4 in the interior. I got these results regardless of the image size.
Super_Hippo Super_Hippo

2025/4/21

#
I tested this yesterday and got strange results, too. I had version 3.7.1 (I think). I used the standard Greenfoot image and not all objects in the center had a 4. Some had a 3 without any apparent pattern. I just updated to 3.9.0 in order to check if it was potentially fixed in a newer version, but for some reason I can’t create a new project or open any existing project now... I don’t know what’s going on, but I can’t check if it works in 3.9.0 because it doesn’t seem to work at all. But I saw the error in 3.7.1.
ttt_ ttt_

2025/5/1

#
danpost wrote...
ttt wrote...
The Actor method getNeighbours() gives strange results in certain situations. << Code Omitted >>
I do not seem to be getting any strange results. Maybe you can give examples. Like, for what image do you get for what strange results (that is, give image AND results). The results can be given as in the following: For image "yellow-draught.png", I got 2 in the corners, 3 along the edges and 4 in the interior. I got these results regardless of the image size.
Sorry, there was a screenshot attached, but this didn't work. Huh? For the moment there is a small image in my profile. Examples (all with yellow-draught.png) are as follows: cellsize=59 2333333332 3444444443 3444444443 3444444443 3444444443 3444444443 3444444443 3444444443 3444444443 2333333332 (the expected result) cellsize=60 2333233332 3444344443 3444344443 3444344443 2333233332 3444344443 3444444443 3444344443 3444344443 2333233332 (not the expected result) cellsize=61 cellsize=62 correct result cellsize=63 2333333332 3444444343 3443444343 3434334333 3444444343 3443444343 3444444443 2333333232 3444444343 2333333332 (wrong, but different from cellsize 60) cellsize=64 cellsize=65 cellsize=66 correct result cellsize=67 2333333322 3444434433 3323323322 3434434433 3434434433 3323323322 3444434433 3434434433 2323323322 2323323322 ??? Can anyone confirm this? Got the same results on a different machines. Tried Greenfoot 3.7.1., 3.8.x, 3.9.0 - all the same.
danpost danpost

2025/5/2

#
Right. I am getting similar results after further testing. Maybe you should use a different method -- maybe the following for line 10:
1
int n = getObjectsInRange(1, null).size();
I get consistently correct results with this one.
You need to login to post a reply.