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

2015/1/9

Generate random objects on specific locations

h35am h35am

2015/1/9

#
Hi All, I need to generate objects in greenfoot world, this must be complitly random but in specific locations. How can I achieve this? This is what I have done so far but I dont know how to complete this.
1
2
3
4
5
6
7
8
9
10
public void spawnContainer()
    {   
       
        ContainerRoodDek1 containerRoodDek1 = new ContainerRoodDek1();
        ContainerGeelDek1 containerGeelDek1 = new ContainerGeelDek1();
        ContainerGroenDek1 containerGroenDek1 = new ContainerGroenDek1();
        ContainerBlauwDek1 containerBlauwDek1 = new ContainerBlauwDek1();
         
        addObject(containerRoodDek1, 255,206);
    }
thnx for helping
Ruvero Ruvero

2015/1/9

#
Make an array of all your specific locations. Use Greenfoot.random() to get the coordinates of a random location.
danpost danpost

2015/1/9

#
Ruvero is on the right track; however, the method is 'Greenfoot.getRandomNumber(int)'. Also, there is the problem of a location being chosen more than once (or having two or more containers end up at the same location). Any ideas, Ruvero?
Ruvero Ruvero

2015/1/9

#
Remove the used location from the array, be sure to use the size of the array as the maximum for Greenfoot.getRandomNumber to avoid errors. Edit: and by the way: use English for all your variables and object names. You will be judged by your teacher on that too ;).
danpost danpost

2015/1/9

#
Ruvero wrote...
Remove the used location from the array, be sure to use the size of the array as the maximum for Greenfoot.getRandomNumber to avoid errors.
Arrays have a set size. You can remove a location from the array by making the element the location was at 'null'; but, you cannot remove the position within the array the location was at. Want to try again?
Ruvero Ruvero

2015/1/9

#
Not very effective: if position == null, try again.
danpost danpost

2015/1/9

#
Ruvero wrote...
Not very effective: if position == null, try again.
More effective would be to track the number of remaining locations and move the last choice to the position of the one used after each choice.
Super_Hippo Super_Hippo

2015/1/9

#
You can also have the code in the 'addedToWorld' method and have a while loop to choose a new location until an empty one is found.
h35am h35am

2015/1/9

#
Thanx a lot people :) I will try to fix this, Thnx Ruvero, Ive got a dutch teacher so he dont mind but I will certainly consider your suggestion ;)
h35am h35am

2015/1/9

#
Ive used this solution and it works fine:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public void spawnContainerDek1(int x, int y)
    {  
        Dek1 containerRoodDek1 = new ContainerRoodDek1();
        Dek1 containerGeelDek1 = new ContainerGeelDek1();
        Dek1 containerGroenDek1 = new ContainerGroenDek1();
        Dek1 containerBlauwDek1 = new ContainerBlauwDek1();
         
        //array maken van containers
        Dek1[] containerArray = new Dek1[4];
         
        containerArray[0] = containerRoodDek1;
        containerArray[1] = containerGeelDek1;
        containerArray[2] = containerGroenDek1;
        containerArray[3] = containerBlauwDek1;
         
        //de identifier van de containerArray
        int i;
         
        //procent kans aanmaken
        int precentage = ((int)(Math.random() * 100));
         
        if(precentage >= 0 && precentage < 25){
        // kans tot 25%
            i=0;
        } else if (precentage >= 25 && precentage < 50){
            i=1;
        } else if (precentage >= 50 && precentage < 75){
            i=2;
        } else{
            i=3;
        }
         
         
        addObject(containerArray[i], x,y);
         
    }
Thnx all for helping me out :)
Ruvero Ruvero

2015/1/9

#
Greenfoot has an own random number generator. For example:
1
Greenfoot.getRandomNumber(3)
Will generate a number between 0 and 3. So if you replace line 17 with:
1
int i = Greenfoot.getRandomNumber(3)
Then you can delete line 19-31.
danpost danpost

2015/1/9

#
Ruvero wrote...
For example:
1
Greenfoot.getRandomNumber(3)
Will generate a number between 0 and 3.
That is not correct. That line of code will only generate a number between 0 and 2, giving the 3 possibilities { 0, 1, 2 }.
You need to login to post a reply.