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

2012/3/31

Quick questions

1
2
woodkami woodkami

2012/3/31

#
Hello, I just started Greenfoot a week ago, I just started a project of my own and now I have a little query.
public class White extends Actor
{
        private int regionNorth = 1;
        private int regionSouth = 1;
        private int regionWest = 1;
        private int regionEast = 1;
        private int region = 0;

    public void act() 
    {
        checkRegion();
        cut();   
    }    

    public void cut()
    {
        Greenfoot.delay(1);
        region = regionNorth + regionSouth + regionWest + regionEast;
        if (region == 4){
            setLocation(1,1); // this is just to see if it works
        }
    }

    public void checkRegion()
    {
        if( getWorld().getObjectsAt(getX(), getY()-1, Black.class).isEmpty() ){
            regionNorth = 0;
        }
        if( getWorld().getObjectsAt(getX(), getY()+1, Black.class).isEmpty() ){
            regionSouth = 0;
        }
        if( getWorld().getObjectsAt(getX()-1, getY(), Black.class).isEmpty() ){
            regionWest = 0;
        }
        if( getWorld().getObjectsAt(getX()+1, getY(), Black.class).isEmpty() ){
            regionEast = 0;
        }
    }
}
I have two identical actor sub-classes one called White and the other Black. How can I make the opposite one check to see if he can cut() first? http://www.greenfoot.org/scenarios/4701
danpost danpost

2012/3/31

#
As your code is right now, it will only work correctly one the initial act() cycle, because nowhere are you re-setting your regionXxxx variables back to one (1). Also, there are no checks to see if the neighboring regions are outside the world or not. To correct both of these, change your 'checkRegion()' method to
public void checkRegion()
{
    regionNorth = 1;
    if( getY() - 1 < 0 || getWorld().getObjectsAt(getX(), getY()-1, Black.class).isEmpty() ){
        regionNorth = 0;
    }
    regionSouth = 1;
    if( getY() + 1 >=getWorld().getHeight() - 1 || getWorld().getObjectsAt(getX(), getY()+1, Black.class).isEmpty() ){
        regionSouth = 0;
    }
    regionWest = 1;
    if( getX() - 1 < 0 || getWorld().getObjectsAt(getX()-1, getY(), Black.class).isEmpty() ){
        regionWest = 0;
    }
    if(getX() + 1 >= getWorld().getWidth() - 1 || getWorld().getObjectsAt(getX()+1, getY(), Black.class).isEmpty() ){
        regionEast = 0;
    }
}
Finally, to control whose turn it is (which is what I think you were really asking), add a instance static World boolean variable 'whiteToMove' and set it to true (if white goes first in this game). In your sub-class of the World class, after the class declaration but before any methods, put
static boolean whiteToMove = true;
You will change the value of this variable after each move with
whiteToMove = !whiteToMove;
which negates the value of the variable. Now, all that needs done is to enclose the statements in your act methods of your White and Black classes in
if (MyWorld.whiteToMove) // for one, 
 //                   or
if (!MyWorld.whiteToMove) // for the other
//  'MyWorld' needs to be replaced with the name of the sub-class of your World.
woodkami woodkami

2012/3/31

#
Thank you!
woodkami woodkami

2012/3/31

#
I tried fiddling around with the new checkRegion(); method, but I have no idea why it doesn't check to see if the neighboring regions are outside the world or not. I tried changing the operators around, yet that also did not work (the value remains 0).
danpost danpost

2012/3/31

#
OK, I missed a line between lines 14 and 15 above. Add there the following line:
regionEast = 1;
Oh, and the order of the operation is important. Leave them the way I gave them to you, or you will run-time errors.
danpost danpost

2012/3/31

#
For more accurate help, you might want to re-upload the scenario, except this time check the 'Publish source code' checkbox and include in the Text that it does not work yet.
danpost danpost

2012/3/31

#
danpost wrote...
For more accurate help, you might want to re-upload the scenario, except this time check the 'Publish source code' checkbox and include in the Text that it does not work yet.
woodkami woodkami

2012/3/31

#
danpost wrote...
OK, I missed a line between lines 14 and 15 above. Add there the following line:
regionEast = 1;
Oh, and the order of the operation is important. Leave them the way I gave them to you, or you will run-time errors.
Yeah, I noticed that don't worry... I uploaded it. http://www.greenfoot.org/scenarios/4701
danpost danpost

2012/3/31

#
Looking into it now.
danpost danpost

2012/3/31

#
Actually, it appears to be working fine. You just do not have any stones with opposite color stones on all four sides. Remove your setLocation statement and add the statement
System.out.println("Stone at (" + getX() + ", " + getY() + ") has " + region + "opposing neighbors.");
re-compile and click on the 'Act' button, then look at the terminal information.
woodkami woodkami

2012/3/31

#
When its at (0:0) it only has 2 opposing neighbors, however it should also read the corners as opposing neighbors and disappear... Sorry I thought you knew the Go board game...
danpost danpost

2012/3/31

#
Sorry. You are right in that I am not very familiar with 'GO'. Use this then for the 'checkRegion' method
public void checkRegion()
{
    regionNorth = 1;
    if( getY() > 0 && getWorld().getObjectsAt(getX(), getY()-1, Black.class).isEmpty() ){
        regionNorth = 0;
    }
    regionSouth = 1;
    if( getY() < getWorld().getHeight() - 1 && getWorld().getObjectsAt(getX(), getY()+1, Black.class).isEmpty() ){
        regionSouth = 0;
    }
    regionWest = 1;
    if( getX() > 0 && getWorld().getObjectsAt(getX()-1, getY(), Black.class).isEmpty() ){
        regionWest = 0;
    }
    regionEast = 1;
    if(getX() < getWorld().getWidth() - 1 && getWorld().getObjectsAt(getX()+1, getY(), Black.class).isEmpty() ){
        regionEast = 0;
    }
}
woodkami woodkami

2012/3/31

#
It works like a charm, thank you!
danpost danpost

2012/3/31

#
I cleaned up the White and Black classes a bit (I also changed the name of the 'checkRegion' method and made it return an 'int' value instead of nothing and made it more readable). This is what I came up with for the White class (the Black class is basically the same):
import greenfoot.*;

public class White extends Stones
{
    public void act() 
    {
        cut();   
    }    

    public void cut()
    {
        int threatCount = countThreats();
        System.out.println("Regions for (" + getX() + ", " + getY() + ") = " + threatCount);
    }
    
     public int countThreats()  
    {  
        int threats = 0;  
        if (getY() == 0 || !getWorld().getObjectsAt(getX(), getY() - 1, Black.class).isEmpty()) threats++;
        if (getY() == 8 || !getWorld().getObjectsAt(getX(), getY() + 1, Black.class).isEmpty()) threats++;
        if (getX() == 0 || !getWorld().getObjectsAt(getX() - 1, getY(), Black.class).isEmpty()) threats++;
        if (getX() == 8 || !getWorld().getObjectsAt(getX() + 1, getY(), Black.class).isEmpty()) threats++;
        return threats;
    }  
}
Also, now, instead of checking for 'not threats', it checks for 'threats'.
woodkami woodkami

2012/3/31

#
I
danpost wrote...
I cleaned up the White and Black classes a bit (I also changed the name of the 'checkRegion' method and made it return an 'int' value instead of nothing and made it more readable). This is what I came up with for the White class (the Black class is basically the same):
import greenfoot.*;

public class White extends Stones
{
    public void act() 
    {
        cut();   
    }    

    public void cut()
    {
        int threatCount = countThreats();
        System.out.println("Regions for (" + getX() + ", " + getY() + ") = " + threatCount);
    }
    
     public int countThreats()  
    {  
        int threats = 0;  
        if (getY() == 0 || !getWorld().getObjectsAt(getX(), getY() - 1, Black.class).isEmpty()) threats++;
        if (getY() == 8 || !getWorld().getObjectsAt(getX(), getY() + 1, Black.class).isEmpty()) threats++;
        if (getX() == 0 || !getWorld().getObjectsAt(getX() - 1, getY(), Black.class).isEmpty()) threats++;
        if (getX() == 8 || !getWorld().getObjectsAt(getX() + 1, getY(), Black.class).isEmpty()) threats++;
        return threats;
    }  
}
Also, now, instead of checking for 'not threats', it checks for 'threats'.
I believe the previous one is better because this game has three size of boards 9x9 13x13 19x19(official)! However I will change it to check for threats like you did here. Thank you very much!
There are more replies on the next page.
1
2