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

2014/11/29

Assistance with generating 4 actors in a random order.

0890289 0890289

2014/11/29

#
Hello! My name is Robert. I have to create a mechanic that spawns 4 actors, each representing a truck in a certain color. That is simple, but the trick is that I have to randomize the order in which they are spawned... So far I have tried two things:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int randomNummer = 0;
          
         for(int i = 0; i < 1; i++){
         randomNummer = Greenfoot.getRandomNumber(4);
     }
    
         if (randomNummer == 1) {
             getWorld().addObject(new TruckBlauw(), 300, 300);
         }
         else if (randomNummer == 2){
             getWorld().addObject(new TruckGeel(), 400, 400);
         }
         else if (randomNummer == 3) {
           getWorld().addObject(new TruckGroen(), 500, 500);
        }
        else if (randomNummer == 4){
           getWorld().addObject(new TruckRood(), 200, 200);        }
The above code does spawn the trucks neatly, but not in a random order. Then I tried using this line for each color truck I had:
1
getWorld().addObject(new TruckBlauw(), Greenfoot.getRandomNumber(getWorld().getWidth()), 500);
This does spawn a lot of trucks in a random order, but it completely fills the screen with trucks which is not what I want of course.. Can anyone assist me? Thanks!
danpost danpost

2014/11/29

#
One way is to create all four trucks and put them in a list. As you randomly choose them, remove them from the list. Even an array would work provided you set and check for 'null' as choices are made.
0890289 0890289

2014/11/29

#
Can you explain a bit more? Maybe with an example of a piece of code?
danpost danpost

2014/11/29

#
Looking over what you wrote above, I am not quite sure exactly what is needed here. I do not know if you are trying to place four trucks in your world arranged in a random order or have them appear at different times in a random order. Please elaborate.
0890289 0890289

2014/12/1

#
I'm trying to place four different trucks in a random order, but if I use the code underneath (my latest attempt), there is an infinite amount of trucks created.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int randomNummer = 0;
         
        for(int i = 0; i < 1; i++){
        randomNummer = Greenfoot.getRandomNumber(4);
    }
 
        for(int i = 0; i < 1; i++) {
        if(randomNummer == 0){
            getWorld().addObject(new TruckBlauw(), Greenfoot.getRandomNumber(getWorld().getWidth()), 500);
        }
        if(randomNummer == 1){
            getWorld().addObject(new TruckGeel(), Greenfoot.getRandomNumber(getWorld().getWidth()), 500);
        }
        if(randomNummer == 2){
            getWorld().addObject(new TruckGroen(), Greenfoot.getRandomNumber(getWorld().getWidth()), 500);
        }
        if(randomNummer == 3){
            getWorld().addObject(new TruckRood(), Greenfoot.getRandomNumber(getWorld().getWidth()), 500);
        }
    }
danpost danpost

2014/12/1

#
Your code is equivalent to this:
1
2
3
4
5
6
7
int randomNumber = Greenfoot.getRandomNumber(4);
Actor truck = null;
if (randomNumber == 0) actor = new TruckBlauw();
if (randomNumber == 1) actor = new TruckGeel();
if (randomNumber == 2) actor = new TruckGroen();
if (randomNumber == 3) actor = new TruckRood();
getWorld().addObject(actor, Greenfoot.getRandomNumber(getWorld().getwidth()), 500);
Briefly explaining, lines 3 and 7 of your code create loops that iterate through only once (after the first iteration, 'i' is incremented to 1 and the condition fails, causing the loop to terminate. By creating an Actor variable, I made the code more readable by only needing one 'addObject' line instead of four. Since this code, executed once, will only produce one truck, and you say an infinite number of trucks are being produced, it stands to reason that you placed the code in an act method (which is repeatedly called by greenfoot when the scenario is running). Ok, you are trying to randomly place four trucks in your world. I think you are going about this the wrong way. Instead of randomly picking the truck for each location, how about randomly picking the location for each truck. First, create the new trucks, putting them in an Actor array of length 4. Then, for each truck in the array, get a random location (from your original post, you only need to get one value -- 'int rand = 200+100*Greenfoot.getRandomNumber(4);', since both x and y values are the same. Because this value may have been previously picked, we need to see if a truck is already at that location and continue picking until we do not find a truck at the location. This will require a 'while' loop inside the 'for' loop, 'while (!getObjects(n, n, null).isEmpty())'. What do we do, if a truck is already at the location, pick another random location -- duplicate the random number line above (this time, we do not need to declare the variable as an 'int'). The only way we will exit the 'while' loop is if the location was empty, so we add the truck at the current location in the array at that location in the world. When the 'for' loop is completed, we will have all four trucks randomly placed in your world. This code must not be in the act method. It should go in your world constructor or a method it calls (like a 'prepare' method).
You need to login to post a reply.