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

2023/6/21

Question about Minesweeper

katenech katenech

2023/6/21

#
Hi! Explain, please:
public class MyWorld extends World
{

    int number;
    
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(10, 10, 50);         
        prepare();
    }
    
    private void prepare()
    {
        for (int i=0; i<7;i++){
        Bomb bomb = new Bomb();
        bomb.getImage().setTransparency(100);
        addObject(bomb,Greenfoot.getRandomNumber(11),Greenfoot.getRandomNumber(11));
    }
    
    }
    
    public void act(){
        if(Greenfoot.mouseClicked(null)){
            MouseInfo mi = Greenfoot.getMouseInfo();
            int mix = mi.getX();
            int miy = mi.getY();            
            Empty em = new Empty();
            addObject(em,mix,miy);            
            // 
            showText(""+Empty.number, mix, miy);
        }
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;

public class Empty extends Actor
{
    static int number;
    List<Bomb> bList = new ArrayList<>();
    
    public void act()
    {
               
        bList = getNeighbours(1, true, Bomb.class);        
        number = bList.size();
    }
}
After first click number= previous value (after start i'ts 0) After second click number = correct Thanks!
danpost danpost

2023/6/21

#
katenech wrote...
Explain, please: << Code Omitted >> After first click number= previous value (after start i'ts 0) After second click number = correct
If I am not mistaken, number is always the value of what the previous click should have made it. This is because you are displaying the value before the new Empty object gets a chance to act, to put the proper value into the field. The Empty class could simply be the following (at least for now -- to start with):
import greenfoot.*;

public class Empty extends Actor
{
    protected void addedToWorld(World world) {
        // in case of miss click (Empty object already at click location)
        if (isTouching(Empty.class) {
            world.removeObject(this);
            return;
        }
        // in case of Bomb object at click location
        if (isTouching(Bomb.class)) {
            // game over routine
            return;
        }
        // default, show number of neighboring Bomb objects
        int count = getNeighbours(1, true, Bomb.class).size();
        GreenfootImage numImg = new GreenfootImage(""+count, 32, Color.BLACK, new Color(0, 0, 0, 0));
        GreenfootImage image = new GreenfootImage(50, 50);
        image.drawImage(numImg, 25-img.getWidth()/2, 25-img.getHeight()/2);
        setImage(image);
    }
}
Remove the showText line from the MyWorld class act method.
katenech katenech

2023/6/22

#
Thank you! I forgot about addedToWorld() method! But... When "the new Empty object gets a chance to act"? It's about multythreading in Greenfoot? How learn about it?
danpost danpost

2023/6/22

#
katenech wrote...
It's about multythreading in Greenfoot?
It is not about multi-threading. It is about the order in which greenfoot calls the actors to act. Right then, it is in the middle of calling this object (MyWorld/World/Object) to act. However, in this case, you could just as well call act explicitly on the Empty object to force the value to the field before trying to display it. But then, it would unnecessarily continue to put that value in the field every act step following, which is not good programming. Also, since the click could be anywhere (at a bomb location or at a location where an Empty object already exists), you probably don't want to automatically just display the neighboring bomb count. Something else to consider is restricting where the click can be made before acting upon it, making sure that mix and miy are both between 0 and 10, inclusive, before adding the Empty object into the world. This will improve on the "feel" of the game by the user (a click outside the grid should not be acted on).
katenech katenech

2023/6/22

#
" is not good programming" - I agree with you :) It was... for simplicity)) Thank you for explanation
You need to login to post a reply.