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

2014/1/19

Determine right or wrong

1
2
3
4
danpost danpost

2014/1/20

#
Blhaar2014 wrote...
Is this the right way of giving the boolean 'matching' the proper value?; Code omitted. In theory the piece you sent makes so much sense, but it's still quite obvious I'm fairly new to all this Greenfoot magic! I really appreciate the time and effort you're willing to put into this!
Either way is fine. If you wish to stretch things out and take 5 lines of code that only require one, that is up to you. But, I tell you what, the cleaner the code is, the much easier it is to look it over and determine what it does.
Blhaar2014 Blhaar2014

2014/1/20

#
danpost wrote...
Blhaar2014 wrote...
Hmm just a couple of questions left, deleting an object/actor from the world, doesn't seem to work through the removeObjects method, is that right? How is it done, then? And should I get a seperate button to start up with the first pair of Picture and ID card, so that from that point on the deny and allow buttons can be used?
The 'removeObjects' method requires a list of objects; the 'removeObject' method requires an Actor (the first is plural). A separate button is not needed. I meant to ask what class the 4 picture and 4 idcard classes extend.
The name of the class that the pictures extend is called "Photo", and the class that the Idcars extend is called "Idcard"
Blhaar2014 Blhaar2014

2014/1/20

#
danpost wrote...
I would just create an array with the four ID cards and an array with the four pics. Something like:
Actor[] pics = { new Dirk(), new Fleur(), new Coen(), new Remco() };
int RandomNum = Greenfoot.getRandomNumber(4);
Actor pic = pics[RandomNum];
Then determine which ID to display using your long method (make sure to use the 'if (RandomNum2 <= 30)' first every time or you may get some wrong results). There are (I am sure) better ways to randomly select an ID to display; like you could do similar to how the picture actor was done with a little modification to get the matching one most of the time. Something like:
Actors[] ids = { new IdcardDirk(), new IdcardFleur(), new IdcardCoen(), new IdcardRemco() };
int RandomNum2 = Greenfoot.getRandomNumber(13);
if (RandomNum2 > 7) RandomNum2 = 0; // giving better chance to match
matching = RandomNum2 < 2; // setting 'matching' field appropriately
Actor id = ids[(RandomNum2/2+RandomNum)%4]; // a little math play to align with pic
This should produce a better than average chance that there will be a match (7 in 13); and when not matching, an even chance for each of the wrong entries (2 in 13 for each of the wrong 3).
Ow geeh, can't even seem to figure out where to add this code right now, this must be placed within the public void act() , right?
Blhaar2014 Blhaar2014

2014/1/20

#
Okay sorry, I've got it right now, but for some reason only the "allow" button works, and no mather what, it looses a heart. In case you're wondering, here's the code with all the data from the game filled in.

public class Controlecentrum extends World  
{  
    // these references help in accessing these objects  
    Actor deny = new Weigeren(), allow = new Toestaan();  
    Heart heart = new Heart();  
    boolean matching; // this holds the answer  
    
  
    public Controlecentrum()  
    {  
        super(760, 760, 1);  
        addObject(new Mute(), 710, 20);  
        addObject(allow, 660, 560);  
        addObject(deny, 500, 560);  
        addObject(heart, 390, 40);
    }  
  
    public void act()  
    {  
        if (getObjects(Idcard.class).isEmpty())  
        {  
            
            Photo[] pics = { new Dirk(), new Fleur(), new Coen(), new Remco() };  
            int RandomNum = Greenfoot.getRandomNumber(4);  
            Actor pic = pics[RandomNum]; 
            
            Idcard[] ids = { new IdcardDirk(), new IdcardFleur(), new IdcardCoen(), new IdcardRemco() };  
            int RandomNum2 = Greenfoot.getRandomNumber(13);  
            if (RandomNum2 > 7) RandomNum2 = 0; // giving better chance to match  
            matching = RandomNum2 < 2; // setting 'matching' field appropriately  
            Actor id = ids[(RandomNum2/2+RandomNum)%4]; // a little math play to align with pic
            
        }  
        else  
        {  
                                if ((Greenfoot.mouseClicked(deny) && matching) || (Greenfoot.mouseClicked(allow) && !matching))  
                    {  
                        heart.adjustValue(-1);  
                        if (heart.getValue() == 0) Greenfoot.stop();  
                        // remove pair
                    }  
            
        }  
    }  
}  

danpost danpost

2014/1/20

#
The arrays I created were put in the same order and the chosen ID was based on the pic chosen. Just like your 'if (Randomnum2 <= 30)', I had 'RandomNum < 2' as my condition for being a match (see line 4 in my code above). What you may not understand is how I determined what Idcard to use. The expression '(RandomNum2/2+RandomNum)%4' is probably a bit foreign. What it does is adds 'RandomNum' (the number of the pic chosen) and adds it to 'RandomNum/2' (the integer part of the division; the result of which will be either '0', '1', '2' or '3' -- note that adding zero to RandomNum will result in the same index for both arrays); the sum is then reduced to only the remainder part of the division after dividing by 4 (by '%4'), the number of actors in the ID array. Something to note: if 'RandomNum < 2' then 'RandomNum/2 = 0'.
danpost danpost

2014/1/20

#
At line 32 of your world class, you need to add the pic and idcard actors into the world, do you not?
addObject(pic, 120, 140);
addObject(id, 120, 620);
Blhaar2014 Blhaar2014

2014/1/20

#
danpost wrote...
At line 32 of your world class, you need to add the pic and idcard actors into the world, do you not?
addObject(pic, 120, 140);
addObject(id, 120, 620);
Ah I see
danpost danpost

2014/1/20

#
Line 40 needs to be dealt with also. Use the 'getObjects' method twice; once with 'Photo.class' and once with 'Idcard.class'.
Blhaar2014 Blhaar2014

2014/1/20

#
Still, only the allow button seems to be working. Any idea of what might be the cause of that? (The "toestaan" and "weigeren" classes are totally empty, I checked just in case!)
danpost danpost

2014/1/20

#
danpost wrote...
Line 40 needs to be dealt with also. Use the 'getObjects' method twice; once with 'Photo.class' and once with 'Idcard.class'.
I meant to say to use 'getObjects' within the 'removeObjects' method here.
danpost danpost

2014/1/20

#
What class do the 'Toestaan' and 'Weigeren' classes extend. If not the Actor class, post the code of that class.
Blhaar2014 Blhaar2014

2014/1/20

#
Like
 removeObject.getObjects(Photo.class); 
?
Blhaar2014 Blhaar2014

2014/1/20

#
Toestaan and Weigeren extend the class named "Button"
danpost danpost

2014/1/20

#
Blhaar2014 wrote...
Like
 removeObject.getObjects(Photo.class); 
?
Now! That does not make any sense! Dot notation applies a method or field to an object (not another method unless that method returns an object which 'removeObject' does not). Also, I said to use 'removeObjects' (plural). Like this:
removeObjects(getObjects(Photo.class));
danpost danpost

2014/1/20

#
Please post the code for your Button class.
There are more replies on the next page.
1
2
3
4