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.
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 );
            }
        }
    }
}
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.
PhilipsManor PhilipsManor

2025/4/23

#
Hey coding fam'! So happy to see the gang all together here, united by the force of coding :) I have a problem with my neighbors too, I'm delighted to see I'm not the only one facing this issue. I don't know about you, but I have 3 neighbors, or should I say stalkers. And to list off a few other things, I have 5 restraining orders against my other neighbors, but this isn't too surprising because 2 of them just got bailed out of jail...and that's by me of course. You may wonder, "Why did you bail out people you have a restraining order against? Are you nuts?" Well to answer that, I'm actually allergic to nuts so I don't believe I can classify as one. But honestly, I felt like I should give them a second chance, afterall, they only have a restraining order becase they cut me in line in Costco. I'm a strong believer in 'No Cuts, No buts, No coconuts' and clearly they crossed the line. Additionally, I'm sure they didn't even have a Costco membership card and were able to enter the store by following close behind me (P.S.- I could feel their warm, heavy breathing fanning over the back of my neck.) At first I wasn't so sure I should file a restraining order on them, but as I was waiting patiently in line for the sample for Popcorn Chicken, the aroma of the juicy meat tickling my nose, they swooped in just in time and grabbed the last few free samples before I could even lay my hands on that glorious chicken sample. I was at the verge of breaking down but I knew I had to keep composure. I've heard people say that they saw fumes coming from my head. But me being a good soul, I decided to chill out. On my way out I heard people saying something was given 5 big booms, which I didn't think much of until I started hearing booms throughout the building. I started to panic because I thought the building was going down, so I ran outside without a second glance. This ended up being a terrible idea because the recipt checker called the cops on me for stealing, since I didn't show him my recipt and get that beautiful smiley face drawn on it. But coming back to my 'sweet' neighbors, one of them is named reklats, which I think originates from Mozambique but he's actually 101% white...Welp, not so sure where that came from. The other one is named peerc who I heard was born in the middle of a flight over the Bermuda Triangle, so I'm not sure how he's even here right now but I suppose it explains why he is the way he is. peerc claims that eh is the Great Titan of the Seas so I suppose that's why my cousin never returned after flying over the Bermuda. My cousin said she would be back after 2 or 20 years, I can't really remember specifically, but it's been about 4 months so I'm pretty sure she's a goner. But anyways, these fellows have a hobby of kocking through the windows of my house and run away....Childish much? But if they get caught by me, they make up an excuse by saying that it truly wasn't them who was punding on my windows, it was actually their identical twins! But when their ACTUAL twin comes over and kock as well, THEY say it was their OTHER identical twin....now I'm a little confuzzled on who is actually knocking on my windows. Don't worry though, my other neighbors Oppih_Repus and Tsopnad came to the rescue and saved me. Anyways, I hope your neighbor situation is getting better because mine cenrtainly has no hope of getting better. It was a pleasure talking to you, gang. xoxo, Philip ✌.ʕʘ‿ʘʔ.✌
ttt_ ttt_

16 hours ago

#
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

33 minutes ago

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