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

2019/6/25

please can some one help me understand .size

Coltz Coltz

2019/6/25

#
public void YouWinBlue()
    {
        int numberOfBlueCircles = getObjects(BlueCircle.class).size();
        if(numberOfBlueCircles == 0)
        {
            addObject(new Player_1_Wins(), getWidth()/2, getHeight()/2);
        }
    }
my code works im just wondering why .size is needed on this?
danpost danpost

2019/6/25

#
Coltz wrote...
<< Code Omitted >> my code works im just wondering why .size is needed on this?
The getObjects method will return a java.util.List object (this is shown in the API documentation of the World class). This win state is determined by the state that no BlueCircle objects are in the world. So, the size of the list must be zero for the win to happen. Hence, getting the size of the list is achieved by calling the size method on the List object (again, API doc of java.util.List class shows this method returning an int value). As an good alternative, you can use the isEmpty method with a boolean return type:
public void YouWinBlue()
{
    if (getObjects(BlueCircle.class).isEmpty())
    {
        addObject(new Player_1_Wins(), getWidth()/2, getHeight()/2);
    }
}
Coltz Coltz

2019/6/25

#
ok thank you for your help :)
You need to login to post a reply.